2016-01-13 02:43:20 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
|
|
|
# Copyright © 2010-2016 Lance Edgar
|
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Message Views
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
2016-02-11 17:05:56 -06:00
|
|
|
import json
|
2016-02-10 21:59:13 -06:00
|
|
|
import pytz
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
from rattail import enum
|
|
|
|
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
|
|
|
|
|
|
|
import formalchemy
|
2016-02-10 21:59:13 -06:00
|
|
|
from formalchemy.helpers import text_field
|
2016-01-13 02:43:20 -06:00
|
|
|
from pyramid import httpexceptions
|
2016-02-05 14:42:56 -06:00
|
|
|
from webhelpers.html import tags, HTML
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
from tailbone import forms
|
|
|
|
from tailbone.db import Session
|
2016-02-11 17:05:56 -06:00
|
|
|
from tailbone.views import MasterView
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
class SubjectFieldRenderer(formalchemy.FieldRenderer):
|
|
|
|
|
|
|
|
def render_readonly(self, **kwargs):
|
|
|
|
subject = self.raw_value
|
|
|
|
if not subject:
|
|
|
|
return ''
|
|
|
|
return tags.link_to(subject, self.request.route_url('messages.view', uuid=self.field.parent.model.uuid))
|
|
|
|
|
|
|
|
|
|
|
|
class SenderFieldRenderer(forms.renderers.UserFieldRenderer):
|
|
|
|
|
|
|
|
def render_readonly(self, **kwargs):
|
|
|
|
sender = self.raw_value
|
|
|
|
if sender is self.request.user:
|
|
|
|
return 'you'
|
|
|
|
return super(SenderFieldRenderer, self).render_readonly(**kwargs)
|
|
|
|
|
|
|
|
|
2016-02-05 14:42:56 -06:00
|
|
|
class RecipientsField(formalchemy.Field):
|
|
|
|
"""
|
|
|
|
Custom field for recipients, used when sending new messages.
|
|
|
|
"""
|
|
|
|
is_collection = True
|
|
|
|
|
|
|
|
def sync(self):
|
|
|
|
if not self.is_readonly():
|
|
|
|
message = self.parent.model
|
2016-02-10 21:59:13 -06:00
|
|
|
for uuid in self._deserialize():
|
2016-02-05 14:42:56 -06:00
|
|
|
user = Session.query(model.User).get(uuid)
|
|
|
|
if user:
|
|
|
|
message.add_recipient(user, status=enum.MESSAGE_STATUS_INBOX)
|
|
|
|
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
class RecipientsFieldRenderer(formalchemy.FieldRenderer):
|
|
|
|
|
2016-02-05 14:42:56 -06:00
|
|
|
def render(self, **kwargs):
|
2016-02-10 21:59:13 -06:00
|
|
|
uuids = self.value
|
|
|
|
value = ','.join(uuids) if uuids else ''
|
2016-02-11 17:05:56 -06:00
|
|
|
return text_field(self.name, value=value, **kwargs)
|
2016-02-10 21:59:13 -06:00
|
|
|
|
|
|
|
def deserialize(self):
|
|
|
|
value = self.params.getone(self.name).split(',')
|
|
|
|
value = [uuid.strip() for uuid in value]
|
|
|
|
value = set([uuid for uuid in value if uuid])
|
|
|
|
return value
|
2016-02-05 14:42:56 -06:00
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
def render_readonly(self, **kwargs):
|
|
|
|
recipients = self.raw_value
|
|
|
|
if not recipients:
|
|
|
|
return ''
|
2016-02-05 14:42:56 -06:00
|
|
|
recips = [r for r in recipients if r.recipient is not self.request.user]
|
2016-01-13 02:43:20 -06:00
|
|
|
recips = sorted([r.recipient.display_name for r in recips])
|
|
|
|
if len(recips) < len(recipients):
|
|
|
|
recips.insert(0, 'you')
|
2016-05-01 14:57:41 -05:00
|
|
|
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
|
2016-01-13 02:43:20 -06:00
|
|
|
return ', '.join(recips)
|
|
|
|
|
|
|
|
|
|
|
|
class TerseRecipientsFieldRenderer(formalchemy.FieldRenderer):
|
|
|
|
|
|
|
|
def render_readonly(self, **kwargs):
|
|
|
|
recipients = self.raw_value
|
|
|
|
if not recipients:
|
|
|
|
return ''
|
2016-02-12 20:44:41 -06:00
|
|
|
message = self.field.parent.model
|
|
|
|
recips = [r for r in recipients if r.recipient is not self.request.user]
|
2016-01-13 02:43:20 -06:00
|
|
|
recips = sorted([r.recipient.display_name for r in recips])
|
2016-02-12 20:44:41 -06:00
|
|
|
if len(recips) < len(recipients) and (
|
|
|
|
message.sender is not self.request.user or not recips):
|
|
|
|
recips.insert(0, 'you')
|
2016-01-13 02:43:20 -06:00
|
|
|
if len(recips) < 5:
|
|
|
|
return ', '.join(recips)
|
|
|
|
return "{}, ...".format(', '.join(recips[:4]))
|
|
|
|
|
|
|
|
|
|
|
|
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'
|
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)
|
|
|
|
if recipient and recipient.status == enum.MESSAGE_STATUS_ARCHIVE:
|
|
|
|
return "Message Archive"
|
|
|
|
elif not recipient:
|
|
|
|
return "Sent Messages"
|
|
|
|
return "Message Inbox"
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
def get_index_url(self):
|
|
|
|
# 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-02-17 14:24:30 -06:00
|
|
|
def _preconfigure_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)
|
2017-02-17 14:24:30 -06:00
|
|
|
g.sender.set(label="From", renderer=SenderFieldRenderer)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
g.filters['subject'].default_active = True
|
|
|
|
g.filters['subject'].default_verb = 'contains'
|
2017-02-17 14:24:30 -06:00
|
|
|
g.subject.set(renderer=SubjectFieldRenderer)
|
|
|
|
|
|
|
|
g.recipients.set(label="To", renderer=TerseRecipientsFieldRenderer)
|
2016-01-13 02:43:20 -06:00
|
|
|
|
|
|
|
g.default_sortkey = 'sent'
|
|
|
|
g.default_sortdir = 'desc'
|
|
|
|
|
2017-02-17 14:24:30 -06:00
|
|
|
def configure_grid(self, g):
|
2016-01-13 02:43:20 -06:00
|
|
|
g.configure(
|
|
|
|
include=[
|
2017-02-17 14:24:30 -06:00
|
|
|
g.subject,
|
|
|
|
g.sender,
|
|
|
|
g.recipients,
|
2016-02-14 16:34:39 -06:00
|
|
|
g.sent,
|
2016-01-13 02:43:20 -06:00
|
|
|
],
|
|
|
|
readonly=True)
|
|
|
|
|
|
|
|
def row_attrs(self, row, i):
|
|
|
|
recip = self.get_recipient(row)
|
|
|
|
if recip:
|
|
|
|
return {'data-uuid': recip.uuid}
|
|
|
|
return {}
|
|
|
|
|
2016-02-10 21:59:13 -06:00
|
|
|
def make_form(self, instance, **kwargs):
|
|
|
|
form = super(MessagesView, self).make_form(instance, **kwargs)
|
|
|
|
if self.creating:
|
2016-09-13 20:23:51 -05:00
|
|
|
form.id = 'new-message'
|
2016-02-10 21:59:13 -06:00
|
|
|
form.cancel_url = self.request.get_referrer(default=self.request.route_url('messages.inbox'))
|
2017-02-13 17:37:16 -06:00
|
|
|
form.create_label = "Send Message"
|
2016-02-10 21:59:13 -06:00
|
|
|
return form
|
|
|
|
|
2016-01-13 02:43:20 -06:00
|
|
|
def configure_fieldset(self, fs):
|
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
|
|
|
|
2016-02-05 14:42:56 -06:00
|
|
|
if self.creating:
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# Must create a new 'sender' field so that we can feed it the
|
|
|
|
# current user as default value, but prevent attaching user to the
|
|
|
|
# (new) underlying message instance...ugh
|
|
|
|
fs.append(formalchemy.Field('sender', value=self.request.user,
|
|
|
|
renderer=forms.renderers.UserFieldRenderer,
|
|
|
|
label="From", readonly=True))
|
|
|
|
|
|
|
|
# Sort of the same thing for recipients, although most of that logic is below.
|
2016-02-05 14:42:56 -06:00
|
|
|
fs.append(RecipientsField('recipients', label="To", renderer=RecipientsFieldRenderer))
|
2016-02-11 18:47:39 -06:00
|
|
|
|
2016-02-05 14:42:56 -06:00
|
|
|
fs.configure(include=[
|
2016-02-11 18:47:39 -06:00
|
|
|
fs.sender,
|
2016-02-05 14:42:56 -06:00
|
|
|
fs.recipients,
|
2016-01-13 02:43:20 -06:00
|
|
|
fs.subject,
|
2016-02-10 21:59:13 -06:00
|
|
|
fs.body.textarea(size='50x15'),
|
2016-02-05 14:42:56 -06:00
|
|
|
])
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# We'll assign some properties directly on the new message;
|
|
|
|
# apparently that's safe and won't cause it to be committed.
|
|
|
|
# Notably, we can't assign the sender yet. Also the actual
|
|
|
|
# recipients assignment is handled by that field's sync().
|
2016-02-10 21:59:13 -06:00
|
|
|
message = fs.model
|
|
|
|
|
|
|
|
if self.replying:
|
|
|
|
old_message = self.get_instance()
|
|
|
|
message.subject = "Re: {}".format(old_message.subject)
|
|
|
|
message.body = self.get_reply_body(old_message)
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# Determine an initial set of recipients, based on reply
|
|
|
|
# method. This value will be set to a 'pseudo' field to avoid
|
|
|
|
# touching the new model instance and causing a crap commit.
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
value = sorted(value.iteritems(), key=lambda r: r[1])
|
|
|
|
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)
|
|
|
|
fs.recipients.set(value=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):
|
|
|
|
fs.recipients.set(value=[old_message.sender.uuid])
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# Set focus to message body instead of recipients, when replying.
|
2016-02-10 21:59:13 -06:00
|
|
|
fs.focus = fs.body
|
|
|
|
|
|
|
|
elif self.viewing:
|
2016-02-11 18:47:39 -06:00
|
|
|
|
|
|
|
# Viewing an existing message is a heck of a lot easier...
|
2016-02-05 14:42:56 -06:00
|
|
|
fs.configure(include=[
|
2016-02-11 18:47:39 -06:00
|
|
|
fs.sender.with_renderer(SenderFieldRenderer).label("From"),
|
2016-01-13 02:43:20 -06:00
|
|
|
fs.recipients.with_renderer(RecipientsFieldRenderer).label("To"),
|
2016-02-14 16:34:39 -06:00
|
|
|
fs.sent,
|
2016-02-05 14:42:56 -06:00
|
|
|
fs.subject,
|
2016-01-13 02:43:20 -06:00
|
|
|
])
|
2016-02-05 14:42:56 -06:00
|
|
|
|
2016-02-11 18:47:39 -06:00
|
|
|
def before_create(self, form):
|
|
|
|
"""
|
|
|
|
This is where we must assign the current user as sender for new
|
|
|
|
messages, for now. I'm still not quite happy with this...
|
|
|
|
"""
|
|
|
|
super(MessagesView, self).before_create(form)
|
|
|
|
message = form.fieldset.model
|
|
|
|
message.sender = self.request.user
|
|
|
|
|
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):
|
|
|
|
sent = pytz.utc.localize(message.sent)
|
|
|
|
sent = localtime(self.rattail_config, sent)
|
|
|
|
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()
|
|
|
|
kwargs['json'] = json
|
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')))
|
|
|
|
|
|
|
|
new_status = enum.MESSAGE_STATUS_INBOX if dest == 'inbox' else enum.MESSAGE_STATUS_ARCHIVE
|
|
|
|
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:
|
|
|
|
new_status = enum.MESSAGE_STATUS_INBOX if dest == 'inbox' else enum.MESSAGE_STATUS_ARCHIVE
|
|
|
|
for uuid in uuids:
|
|
|
|
recip = Session.query(model.MessageRecipient).get(uuid) if uuid else None
|
|
|
|
if recip and recip.recipient is self.request.user:
|
|
|
|
if recip.status != new_status:
|
|
|
|
recip.status = new_status
|
|
|
|
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
|
|
|
|
|
|
|
def get_index_url(self):
|
|
|
|
return self.request.route_url('messages.inbox')
|
|
|
|
|
|
|
|
def query(self, session):
|
|
|
|
q = super(InboxView, self).query(session)
|
|
|
|
return q.filter(model.MessageRecipient.status == enum.MESSAGE_STATUS_INBOX)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def get_index_url(self):
|
|
|
|
return self.request.route_url('messages.archive')
|
|
|
|
|
|
|
|
def query(self, session):
|
|
|
|
q = super(ArchiveView, self).query(session)
|
|
|
|
return q.filter(model.MessageRecipient.status == enum.MESSAGE_STATUS_ARCHIVE)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def get_index_url(self):
|
|
|
|
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-02-17 14:24:30 -06:00
|
|
|
def _preconfigure_grid(self, g):
|
|
|
|
super(SentView, self)._preconfigure_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
|
|
|
|
|
|
|
|
|
|
|
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)
|