2017-07-06 20:13:42 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2016-01-13 02:43:20 -06:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2017-07-06 20:13:42 -05:00
|
|
|
# Copyright © 2010-2017 Lance Edgar
|
2016-01-13 02:43:20 -06:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2016-01-13 02:43:20 -06:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2016-01-13 02:43:20 -06:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2016-01-13 02:43:20 -06:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Message Views
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
import six
|
2016-02-10 21:59:13 -06:00
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
from rattail.db import model
|
2016-02-10 21:59:13 -06:00
|
|
|
from rattail.time import localtime
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
import colander
|
|
|
|
from deform import widget as dfwidget
|
2016-01-13 02:43:20 -06:00
|
|
|
from pyramid import httpexceptions
|
2017-07-06 20:13:42 -05:00
|
|
|
from webhelpers2.html import tags, HTML
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
from tailbone.db import Session
|
2017-12-04 17:52:25 -06:00
|
|
|
from tailbone.views import MasterView3 as MasterView
|
2017-07-07 09:13:53 -05:00
|
|
|
from tailbone.util import raw_datetime
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
class MessagesView(MasterView):
|
|
|
|
"""
|
|
|
|
Base class for message views.
|
|
|
|
"""
|
|
|
|
model_class = model.Message
|
|
|
|
editable = False
|
|
|
|
deletable = False
|
|
|
|
checkboxes = True
|
2016-02-10 21:59:13 -06:00
|
|
|
replying = False
|
|
|
|
reply_header_sent_format = '%a %d %b %Y at %I:%M %p'
|
2017-12-04 17:52:25 -06:00
|
|
|
|
|
|
|
grid_columns = [
|
|
|
|
'subject',
|
|
|
|
'sender',
|
|
|
|
'recipients',
|
|
|
|
'sent',
|
|
|
|
]
|
|
|
|
|
|
|
|
form_fields = [
|
|
|
|
'sender',
|
|
|
|
'recipients',
|
|
|
|
'sent',
|
|
|
|
'subject',
|
|
|
|
'body',
|
|
|
|
]
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2016-05-01 12:34:15 -05:00
|
|
|
def get_index_title(self):
|
|
|
|
if self.listing:
|
|
|
|
return self.index_title
|
|
|
|
if self.viewing:
|
|
|
|
message = self.get_instance()
|
|
|
|
recipient = self.get_recipient(message)
|
2017-12-04 17:52:25 -06:00
|
|
|
if recipient and recipient.status == self.enum.MESSAGE_STATUS_ARCHIVE:
|
2016-05-01 12:34:15 -05:00
|
|
|
return "Message Archive"
|
|
|
|
elif not recipient:
|
|
|
|
return "Sent Messages"
|
|
|
|
return "Message Inbox"
|
|
|
|
|
2017-04-04 17:43:33 -05:00
|
|
|
def get_index_url(self, **kwargs):
|
2016-01-13 02:43:20 -06:00
|
|
|
# not really used, but necessary to make certain other code happy
|
|
|
|
return self.request.route_url('messages.inbox')
|
|
|
|
|
|
|
|
def index(self):
|
|
|
|
if not self.request.user:
|
|
|
|
raise httpexceptions.HTTPForbidden
|
|
|
|
return super(MessagesView, self).index()
|
|
|
|
|
|
|
|
def get_instance(self):
|
|
|
|
if not self.request.user:
|
|
|
|
raise httpexceptions.HTTPForbidden
|
|
|
|
message = super(MessagesView, self).get_instance()
|
|
|
|
if not self.associated_with(message):
|
|
|
|
raise httpexceptions.HTTPForbidden
|
|
|
|
return message
|
|
|
|
|
|
|
|
def associated_with(self, message):
|
|
|
|
if message.sender is self.request.user:
|
|
|
|
return True
|
|
|
|
for recip in message.recipients:
|
|
|
|
if recip.recipient is self.request.user:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def query(self, session):
|
|
|
|
return session.query(model.Message)\
|
2016-02-11 17:05:56 -06:00
|
|
|
.outerjoin(model.MessageRecipient)\
|
|
|
|
.filter(model.MessageRecipient.recipient == self.request.user)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
def configure_grid(self, g):
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
g.joiners['sender'] = lambda q: q.join(model.User, model.User.uuid == model.Message.sender_uuid).outerjoin(model.Person)
|
|
|
|
g.filters['sender'] = g.make_filter('sender', model.Person.display_name,
|
|
|
|
default_active=True, default_verb='contains')
|
|
|
|
g.sorters['sender'] = g.make_sorter(model.Person.display_name)
|
|
|
|
|
|
|
|
g.filters['subject'].default_active = True
|
|
|
|
g.filters['subject'].default_verb = 'contains'
|
|
|
|
|
|
|
|
g.default_sortkey = 'sent'
|
|
|
|
g.default_sortdir = 'desc'
|
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
g.set_renderer('sent', self.render_sent)
|
|
|
|
g.set_renderer('sender', self.render_sender)
|
|
|
|
g.set_renderer('recipients', self.render_recipients)
|
|
|
|
|
|
|
|
g.set_link('subject')
|
|
|
|
|
|
|
|
g.set_label('sender', "From")
|
|
|
|
g.set_label('recipients', "To")
|
|
|
|
|
|
|
|
def render_sent(self, message, column_name):
|
|
|
|
return raw_datetime(self.rattail_config, message.sent)
|
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
def render_sender(self, message, field):
|
2017-07-07 09:13:53 -05:00
|
|
|
sender = message.sender
|
|
|
|
if sender is self.request.user:
|
|
|
|
return 'you'
|
|
|
|
return six.text_type(sender)
|
|
|
|
|
|
|
|
def render_recipients(self, message, column_name):
|
|
|
|
recipients = message.recipients
|
|
|
|
if recipients:
|
|
|
|
recips = [r for r in recipients if r.recipient is not self.request.user]
|
|
|
|
recips = sorted([r.recipient.display_name for r in recips])
|
|
|
|
if len(recips) < len(recipients) and (
|
|
|
|
message.sender is not self.request.user or not recips):
|
|
|
|
recips.insert(0, "you")
|
|
|
|
if len(recips) < 5:
|
|
|
|
return ", ".join(recips)
|
|
|
|
return "{}, ...".format(', '.join(recips[:4]))
|
|
|
|
return ""
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
def render_recipients_full(self, message, field):
|
|
|
|
recipients = message.recipients
|
|
|
|
if not recipients:
|
|
|
|
return ""
|
|
|
|
recips = [r for r in recipients
|
|
|
|
if r.recipient is not self.request.user]
|
|
|
|
recips = sorted([r.recipient.display_name for r in recips])
|
|
|
|
if len(recips) < len(recipients) and (
|
|
|
|
message.sender is not self.request.user or not recips):
|
|
|
|
recips.insert(0, 'you')
|
|
|
|
max_display = 5
|
|
|
|
if len(recips) > max_display:
|
|
|
|
basic = HTML.literal("{}, ".format(', '.join(recips[:max_display-1])))
|
|
|
|
more = tags.link_to("({} more)".format(len(recips[max_display-1:])), '#', class_='more')
|
|
|
|
everyone = HTML.tag('span', class_='everyone', c=', '.join(recips[max_display-1:]))
|
|
|
|
return basic + more + everyone
|
|
|
|
return ', '.join(recips)
|
|
|
|
|
|
|
|
# TODO!!
|
|
|
|
# def make_form(self, instance, **kwargs):
|
|
|
|
# form = super(MessagesView, self).make_form(instance, **kwargs)
|
|
|
|
# if self.creating:
|
|
|
|
# form.id = 'new-message'
|
|
|
|
# form.cancel_url = self.request.get_referrer(default=self.request.route_url('messages.inbox'))
|
|
|
|
# form.create_label = "Send Message"
|
|
|
|
# return form
|
|
|
|
|
|
|
|
def configure_form(self, f):
|
|
|
|
super(MessagesView, self).configure_form(f)
|
2016-02-10 21:59:13 -06:00
|
|
|
|
2016-02-11 18:47:39 -06:00
|
|
|
# TODO: A fair amount of this still seems hacky...
|
2016-02-10 21:59:13 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
f.set_renderer('sender', self.render_sender)
|
|
|
|
f.set_label('sender', "From")
|
|
|
|
|
|
|
|
f.set_type('sent', 'datetime')
|
|
|
|
|
|
|
|
f.set_renderer('recipients', self.render_recipients_full)
|
|
|
|
f.set_label('recipients', "To")
|
|
|
|
|
|
|
|
f.set_widget('body', dfwidget.TextAreaWidget(cols=50, rows=15))
|
|
|
|
|
2016-02-05 14:42:56 -06:00
|
|
|
if self.creating:
|
2017-12-04 17:52:25 -06:00
|
|
|
f.remove('sender', 'sent')
|
2016-02-11 18:47:39 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
f.insert_after('recipients', 'recipients_')
|
|
|
|
f.remove('recipients')
|
|
|
|
f.set_node('recipients_', colander.SchemaNode(colander.Set()))
|
|
|
|
f.set_widget('recipients_', RecipientsWidget())
|
|
|
|
f.set_label('recipients_', "To")
|
2016-02-10 21:59:13 -06:00
|
|
|
|
|
|
|
if self.replying:
|
|
|
|
old_message = self.get_instance()
|
2017-12-04 17:52:25 -06:00
|
|
|
f.set_default('subject', "Re: {}".format(old_message.subject))
|
|
|
|
f.set_default('body', self.get_reply_body(old_message))
|
2016-02-11 18:47:39 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
# Determine an initial set of recipients, based on reply method.
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# If replying to all, massage the list a little so that the
|
|
|
|
# current user is not listed, and the sender is listed first.
|
2016-02-10 21:59:13 -06:00
|
|
|
if self.replying == 'all':
|
2016-02-11 17:05:56 -06:00
|
|
|
value = [(r.recipient.uuid, r.recipient.person.display_name)
|
2016-02-10 21:59:13 -06:00
|
|
|
for r in old_message.recipients
|
2016-02-11 17:05:56 -06:00
|
|
|
if self.filter_reply_recipient(r.recipient)]
|
2016-02-10 21:59:13 -06:00
|
|
|
value = dict(value)
|
|
|
|
value.pop(self.request.user.uuid, None)
|
2017-12-04 17:52:25 -06:00
|
|
|
value = sorted(value.items(), key=lambda r: r[1])
|
2016-02-10 21:59:13 -06:00
|
|
|
value = [r[0] for r in value]
|
|
|
|
if old_message.sender is not self.request.user and old_message.sender.active:
|
|
|
|
value.insert(0, old_message.sender_uuid)
|
2017-12-04 17:52:25 -06:00
|
|
|
f.set_default('recipients_', ','.join(value))
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# Just a normal reply, to sender only.
|
2016-02-11 17:05:56 -06:00
|
|
|
elif self.filter_reply_recipient(old_message.sender):
|
2017-12-04 17:52:25 -06:00
|
|
|
f.set_default('recipients_', old_message.sender.uuid)
|
2016-02-11 18:47:39 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
# TODO?
|
|
|
|
# # Set focus to message body instead of recipients, when replying.
|
|
|
|
# fs.focus = fs.body
|
2016-02-10 21:59:13 -06:00
|
|
|
|
|
|
|
elif self.viewing:
|
2017-12-04 17:52:25 -06:00
|
|
|
f.remove('body')
|
2016-02-11 18:47:39 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
def objectify(self, form, data):
|
|
|
|
message = super(MessagesView, self).objectify(form, data)
|
2016-02-05 14:42:56 -06:00
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
if self.creating:
|
|
|
|
if self.request.user:
|
|
|
|
message.sender = self.request.user
|
|
|
|
|
|
|
|
for uuid in data['recipients_']:
|
|
|
|
user = self.Session.query(model.User).get(uuid)
|
|
|
|
if user:
|
|
|
|
message.add_recipient(user, status=self.enum.MESSAGE_STATUS_INBOX)
|
|
|
|
|
|
|
|
return message
|
2016-02-11 18:47:39 -06:00
|
|
|
|
2017-02-13 17:37:16 -06:00
|
|
|
def flash_after_create(self, obj):
|
|
|
|
self.request.session.flash("Message has been sent: {}".format(
|
|
|
|
self.get_instance_title(obj)))
|
|
|
|
|
2016-02-11 17:05:56 -06:00
|
|
|
def filter_reply_recipient(self, user):
|
|
|
|
return user.active
|
|
|
|
|
2016-02-10 21:59:13 -06:00
|
|
|
def get_reply_header(self, message):
|
2017-12-04 17:52:25 -06:00
|
|
|
sent = localtime(self.rattail_config, message.sent, from_utc=True)
|
2016-02-10 21:59:13 -06:00
|
|
|
sent = sent.strftime(self.reply_header_sent_format)
|
|
|
|
return "On {}, {} wrote:".format(sent, message.sender.person.display_name)
|
|
|
|
|
|
|
|
def get_reply_body(self, message):
|
|
|
|
"""
|
|
|
|
Given an original message, this method should return the default body
|
|
|
|
value for a "reply" message, i.e. with ">" prefixes etc.
|
|
|
|
"""
|
|
|
|
header = self.get_reply_header(message)
|
|
|
|
lines = message.body.split('\n')
|
|
|
|
if lines and lines[0]:
|
|
|
|
lines.insert(0, '')
|
|
|
|
lines = ['', '', '', header] + ["> {}".format(line) for line in lines]
|
|
|
|
return '\n'.join(lines)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
def get_recipient(self, message):
|
2016-02-11 17:05:56 -06:00
|
|
|
"""
|
|
|
|
Fetch the recipient from the given message, which corresponds to the
|
|
|
|
current (request) user.
|
|
|
|
"""
|
|
|
|
for recipient in message.recipients:
|
|
|
|
if recipient.recipient is self.request.user:
|
|
|
|
return recipient
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2016-02-10 21:59:13 -06:00
|
|
|
def template_kwargs_create(self, **kwargs):
|
2016-02-11 17:05:56 -06:00
|
|
|
kwargs['available_recipients'] = self.get_available_recipients()
|
2016-02-10 21:59:13 -06:00
|
|
|
if self.replying:
|
2016-02-11 17:05:56 -06:00
|
|
|
kwargs['original_message'] = self.get_instance()
|
2016-02-10 21:59:13 -06:00
|
|
|
return kwargs
|
|
|
|
|
2016-02-11 17:05:56 -06:00
|
|
|
def get_available_recipients(self):
|
|
|
|
"""
|
|
|
|
Return the full mapping of recipients which may be included in a
|
|
|
|
message sent by the current user.
|
|
|
|
"""
|
|
|
|
recips = {}
|
|
|
|
users = Session.query(model.User)\
|
|
|
|
.join(model.Person)\
|
|
|
|
.filter(model.User.active == True)
|
|
|
|
for user in users:
|
|
|
|
recips[user.uuid] = user.person.display_name
|
|
|
|
return recips
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
def template_kwargs_view(self, **kwargs):
|
|
|
|
message = kwargs['instance']
|
|
|
|
return {'message': message,
|
|
|
|
'recipient': self.get_recipient(message)}
|
|
|
|
|
2016-02-10 21:59:13 -06:00
|
|
|
def reply(self):
|
|
|
|
"""
|
|
|
|
Reply to a message, i.e. create a new one with first sender as recipient.
|
|
|
|
"""
|
|
|
|
self.replying = True
|
|
|
|
return self.create()
|
|
|
|
|
|
|
|
def reply_all(self):
|
|
|
|
"""
|
|
|
|
Reply-all to a message, i.e. create a new one with all original
|
|
|
|
recipients listed again in the new message.
|
|
|
|
"""
|
|
|
|
self.replying = 'all'
|
|
|
|
return self.create()
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
def move(self):
|
|
|
|
"""
|
|
|
|
Move a message, either to the archive or back to the inbox.
|
|
|
|
"""
|
|
|
|
message = self.get_instance()
|
2016-05-01 12:34:15 -05:00
|
|
|
recipient = self.get_recipient(message)
|
2016-01-13 02:43:20 -06:00
|
|
|
if not recipient:
|
|
|
|
raise httpexceptions.HTTPForbidden
|
|
|
|
|
|
|
|
dest = self.request.GET.get('dest')
|
|
|
|
if dest not in ('inbox', 'archive'):
|
|
|
|
self.request.session.flash("Sorry, I couldn't make sense out of that request.")
|
|
|
|
return self.redirect(self.request.get_referrer(
|
|
|
|
default=self.request.route_url('messages_inbox')))
|
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
new_status = self.enum.MESSAGE_STATUS_INBOX if dest == 'inbox' else self.enum.MESSAGE_STATUS_ARCHIVE
|
2016-01-13 02:43:20 -06:00
|
|
|
if recipient.status != new_status:
|
|
|
|
recipient.status = new_status
|
|
|
|
return self.redirect(self.request.route_url('messages.{}'.format(
|
|
|
|
'archive' if dest == 'inbox' else 'inbox')))
|
|
|
|
|
|
|
|
def move_bulk(self):
|
|
|
|
"""
|
|
|
|
Move messages in bulk, to the archive or back to the inbox.
|
|
|
|
"""
|
|
|
|
dest = self.request.POST.get('destination', 'archive')
|
|
|
|
if self.request.method == 'POST':
|
|
|
|
uuids = self.request.POST.get('uuids', '').split(',')
|
|
|
|
if uuids:
|
2017-12-04 17:52:25 -06:00
|
|
|
new_status = self.enum.MESSAGE_STATUS_INBOX if dest == 'inbox' else self.enum.MESSAGE_STATUS_ARCHIVE
|
2016-01-13 02:43:20 -06:00
|
|
|
for uuid in uuids:
|
2017-07-07 09:13:53 -05:00
|
|
|
recip = self.Session.query(model.MessageRecipient)\
|
|
|
|
.filter(model.MessageRecipient.message_uuid == uuid)\
|
|
|
|
.filter(model.MessageRecipient.recipient_uuid == self.request.user.uuid)\
|
|
|
|
.first()
|
|
|
|
if recip and recip.status != new_status:
|
|
|
|
recip.status = new_status
|
2016-01-13 02:43:20 -06:00
|
|
|
route = 'messages.{}'.format('archive' if dest == 'inbox' else 'inbox')
|
|
|
|
return self.redirect(self.request.route_url(route))
|
|
|
|
|
2016-02-10 21:59:13 -06:00
|
|
|
@classmethod
|
|
|
|
def defaults(cls, config):
|
|
|
|
"""
|
|
|
|
Extra default config for message views.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# reply
|
|
|
|
config.add_route('messages.reply', '/messages/{uuid}/reply')
|
2016-02-10 22:17:49 -06:00
|
|
|
config.add_view(cls, attr='reply', route_name='messages.reply',
|
|
|
|
permission='messages.create')
|
2016-02-10 21:59:13 -06:00
|
|
|
|
|
|
|
# reply-all
|
|
|
|
config.add_route('messages.reply_all', '/messages/{uuid}/reply-all')
|
2016-02-10 22:17:49 -06:00
|
|
|
config.add_view(cls, attr='reply_all', route_name='messages.reply_all',
|
|
|
|
permission='messages.create')
|
2016-02-10 21:59:13 -06:00
|
|
|
|
|
|
|
# move (single)
|
|
|
|
config.add_route('messages.move', '/messages/{uuid}/move')
|
|
|
|
config.add_view(cls, attr='move', route_name='messages.move')
|
|
|
|
|
|
|
|
# move bulk
|
|
|
|
config.add_route('messages.move_bulk', '/messages/move-bulk')
|
|
|
|
config.add_view(cls, attr='move_bulk', route_name='messages.move_bulk')
|
|
|
|
|
|
|
|
cls._defaults(config)
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
class InboxView(MessagesView):
|
|
|
|
"""
|
|
|
|
Inbox message view.
|
|
|
|
"""
|
|
|
|
url_prefix = '/messages/inbox'
|
|
|
|
grid_key = 'messages.inbox'
|
2016-05-01 12:34:15 -05:00
|
|
|
index_title = "Message Inbox"
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-04-04 17:43:33 -05:00
|
|
|
def get_index_url(self, **kwargs):
|
2016-01-13 02:43:20 -06:00
|
|
|
return self.request.route_url('messages.inbox')
|
|
|
|
|
|
|
|
def query(self, session):
|
|
|
|
q = super(InboxView, self).query(session)
|
2017-12-04 17:52:25 -06:00
|
|
|
return q.filter(model.MessageRecipient.status == self.enum.MESSAGE_STATUS_INBOX)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
class ArchiveView(MessagesView):
|
|
|
|
"""
|
|
|
|
Archived message view.
|
|
|
|
"""
|
|
|
|
url_prefix = '/messages/archive'
|
|
|
|
grid_key = 'messages.archive'
|
2016-05-01 12:34:15 -05:00
|
|
|
index_title = "Message Archive"
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-04-04 17:43:33 -05:00
|
|
|
def get_index_url(self, **kwargs):
|
2016-01-13 02:43:20 -06:00
|
|
|
return self.request.route_url('messages.archive')
|
|
|
|
|
|
|
|
def query(self, session):
|
|
|
|
q = super(ArchiveView, self).query(session)
|
2017-12-04 17:52:25 -06:00
|
|
|
return q.filter(model.MessageRecipient.status == self.enum.MESSAGE_STATUS_ARCHIVE)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
class SentView(MessagesView):
|
|
|
|
"""
|
|
|
|
Sent messages view.
|
|
|
|
"""
|
|
|
|
url_prefix = '/messages/sent'
|
|
|
|
grid_key = 'messages.sent'
|
|
|
|
checkboxes = False
|
2016-05-01 12:34:15 -05:00
|
|
|
index_title = "Sent Messages"
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-04-04 17:43:33 -05:00
|
|
|
def get_index_url(self, **kwargs):
|
2016-01-13 02:43:20 -06:00
|
|
|
return self.request.route_url('messages.sent')
|
|
|
|
|
|
|
|
def query(self, session):
|
|
|
|
return session.query(model.Message)\
|
2016-02-11 17:05:56 -06:00
|
|
|
.filter(model.Message.sender == self.request.user)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
def configure_grid(self, g):
|
|
|
|
super(SentView, self).configure_grid(g)
|
2016-01-13 02:43:20 -06:00
|
|
|
g.filters['sender'].default_active = False
|
2017-02-17 14:24:30 -06:00
|
|
|
g.joiners['recipients'] = lambda q: q.join(model.MessageRecipient)\
|
|
|
|
.join(model.User, model.User.uuid == model.MessageRecipient.recipient_uuid)\
|
|
|
|
.join(model.Person)
|
|
|
|
g.filters['recipients'] = g.make_filter('recipients', model.Person.display_name,
|
|
|
|
default_active=True, default_verb='contains')
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
|
2017-12-04 17:52:25 -06:00
|
|
|
class RecipientsWidget(dfwidget.TextInputWidget):
|
|
|
|
|
|
|
|
def deserialize(self, field, pstruct):
|
|
|
|
if pstruct is colander.null:
|
|
|
|
return []
|
|
|
|
elif not isinstance(pstruct, six.string_types):
|
|
|
|
raise colander.Invalid(field.schema, "Pstruct is not a string")
|
|
|
|
if self.strip:
|
|
|
|
pstruct = pstruct.strip()
|
|
|
|
if not pstruct:
|
|
|
|
return []
|
|
|
|
return pstruct.split(',')
|
|
|
|
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
def includeme(config):
|
|
|
|
|
2016-02-01 15:44:10 -06:00
|
|
|
config.add_tailbone_permission('messages', 'messages.list', "List/Search Messages")
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
# inbox
|
|
|
|
config.add_route('messages.inbox', '/messages/inbox/')
|
2016-02-01 15:44:10 -06:00
|
|
|
config.add_view(InboxView, attr='index', route_name='messages.inbox',
|
|
|
|
permission='messages.list')
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
# archive
|
|
|
|
config.add_route('messages.archive', '/messages/archive/')
|
2016-02-01 15:44:10 -06:00
|
|
|
config.add_view(ArchiveView, attr='index', route_name='messages.archive',
|
|
|
|
permission='messages.list')
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
# sent
|
|
|
|
config.add_route('messages.sent', '/messages/sent/')
|
2016-02-01 15:44:10 -06:00
|
|
|
config.add_view(SentView, attr='index', route_name='messages.sent',
|
|
|
|
permission='messages.list')
|
2016-01-13 02:43:20 -06:00
|
|
|
|
2016-02-05 14:42:56 -06:00
|
|
|
MessagesView.defaults(config)
|