Commits

Pedro Mourelle  committed fe3ed1a

Added view and URLs to mark messages as (un)read.

  • Participants
  • Parent commits 35cc85f

Comments (0)

Files changed (2)

File postman/urls.py

 
 Otherwise you may customize the behavior by passing extra parameters.
 
-Recipients Max 
+Recipients Max
 --------------
 Views supporting the parameter are: ``write``, ``reply``.
 Example::
 
 """
 try:
-    from django.conf.urls import patterns, include, url # django 1.4
+    from django.conf.urls import patterns, include, url  # django 1.4
 except ImportError:
-    from django.conf.urls.defaults import patterns, include, url # django 1.3
+    from django.conf.urls.defaults import patterns, include, url  # django 1.3
 from django.views.generic.simple import redirect_to
 
 OPTION_MESSAGES = 'm'
     url(r'^reply/(?P<message_id>[\d]+)/$', 'reply', name='postman_reply'),
     url(r'^view/(?P<message_id>[\d]+)/$', 'view', name='postman_view'),
     url(r'^view/t/(?P<thread_id>[\d]+)/$', 'view_conversation', name='postman_view_conversation'),
+    url(r'^markasunread/$', 'mark_as_unread', name='postman_markasunread'),
+    url(r'^markasread/$', 'mark_as_read', name='postman_markasread'),
     url(r'^archive/$', 'archive', name='postman_archive'),
     url(r'^delete/$', 'delete', name='postman_delete'),
     url(r'^undelete/$', 'undelete', name='postman_undelete'),

File postman/views.py

 from postman.urls import OPTION_MESSAGES
 from postman.utils import format_subject, format_body
 
+
 ##########
 # Helpers
 ##########
     """Return the HTTP_REFERER, if existing."""
     if 'HTTP_REFERER' in request.META:
         sr = urlparse.urlsplit(request.META['HTTP_REFERER'])
-        return urlparse.urlunsplit(('','',sr.path,sr.query,sr.fragment))
+        return urlparse.urlunsplit(('', '', sr.path, sr.query, sr.fragment))
+
 
 ########
 # Views
         'by_conversation_url': reverse(view_name),
         'by_message_url': reverse(view_name, args=[OPTION_MESSAGES]),
         'current_url': request.get_full_path(),
-        'gets': request.GET, # useful to postman_order_by template tag
+        'gets': request.GET,  # useful to postman_order_by template tag
         }, context_instance=RequestContext(request))
 
+
 @login_required
 def inbox(request, option=None, template_name='postman/inbox.html'):
     """
     """
     return _folder(request, 'inbox', 'postman_inbox', option, template_name)
 
+
 @login_required
 def sent(request, option=None, template_name='postman/sent.html'):
     """
     """
     return _folder(request, 'sent', 'postman_sent', option, template_name)
 
+
 @login_required
 def archives(request, option=None, template_name='postman/archives.html'):
     """
     """
     return _folder(request, 'archives', 'postman_archives', option, template_name)
 
+
 @login_required
 def trash(request, option=None, template_name='postman/trash.html'):
     """
     """
     return _folder(request, 'trash', 'postman_trash', option, template_name)
 
-def write(request, recipients=None, form_classes=(WriteForm, AnonymousWriteForm), autocomplete_channels=None,
-        template_name='postman/write.html', success_url=None,
-        user_filter=None, exchange_filter=None, max=None, auto_moderators=[]):
+
+def write(request, recipients=None, form_classes=(WriteForm, AnonymousWriteForm),
+          autocomplete_channels=None, template_name='postman/write.html',
+          success_url=None, user_filter=None, exchange_filter=None,
+          max=None, auto_moderators=[]):
     """
     Display a form to compose a message.
 
     next_url = _get_referer(request)
     if request.method == 'POST':
         form = form_class(request.POST, sender=user, channel=channel,
-            user_filter=user_filter,
-            exchange_filter=exchange_filter,
-            max=max)
+                          user_filter=user_filter, exchange_filter=exchange_filter,
+                          max=max)
         #import ipdb; ipdb.set_trace()
         if form.is_valid():
             is_successful = form.save(auto_moderators=auto_moderators)
                 messages.warning(request, _("Message rejected for at least one recipient."), fail_silently=True)
             return redirect(request.GET.get('next', success_url or next_url or 'postman_inbox'))
     else:
-        initial = dict(request.GET.items()) # allow optional initializations by query string
+        initial = dict(request.GET.items())  # allow optional initializations by query string
         if recipients:
             # order_by() is not mandatory, but: a) it doesn't hurt; b) it eases the test suite
             # and anyway the original ordering cannot be respected.
 if getattr(settings, 'POSTMAN_DISALLOW_ANONYMOUS', False):
     write = login_required(write)
 
+
 @login_required
-def reply(request, message_id, form_class=FullReplyForm, formatters=(format_subject,format_body), autocomplete_channel=None,
-        template_name='postman/reply.html', success_url=None,
-        user_filter=None, exchange_filter=None, max=None, auto_moderators=[]):
+def reply(request, message_id, form_class=FullReplyForm,
+          formatters=(format_subject, format_body), autocomplete_channel=None,
+          template_name='postman/reply.html', success_url=None, user_filter=None,
+          exchange_filter=None, max=None, auto_moderators=[]):
     """
     Display a form to compose a reply.
 
     next_url = _get_referer(request)
     if request.method == 'POST':
         post = request.POST.copy()
-        if 'subject' not in post: # case of the quick reply form
+        if 'subject' not in post:  # case of the quick reply form
             post['subject'] = initial['subject']
         form = form_class(post, sender=user, recipient=parent.sender or parent.email,
-            channel=autocomplete_channel,
-            user_filter=user_filter,
-            exchange_filter=exchange_filter,
-            max=max)
+                          channel=autocomplete_channel, user_filter=user_filter,
+                          exchange_filter=exchange_filter, max=max)
         if form.is_valid():
             is_successful = form.save(parent=parent, auto_moderators=auto_moderators)
             if is_successful:
             'pm_messages': msgs,
             'archived': archived,
             'reply_to_pk': received.pk if received else None,
-            'form' : form_class(initial=received.quote(*formatters)) if received else None,
+            'form': form_class(initial=received.quote(*formatters)) if received else None,
             'next_url': request.GET.get('next', reverse('postman_inbox')),
             }, context_instance=RequestContext(request))
     raise Http404
 
+
 @login_required
 def view(request, message_id, *args, **kwargs):
     """Display one specific message."""
     return _view(request, Q(pk=message_id), *args, **kwargs)
 
+
 @login_required
 def view_conversation(request, thread_id, *args, **kwargs):
     """Display a conversation."""
     return _view(request, Q(thread=thread_id), *args, **kwargs)
 
+
 def _update(request, field_bit, success_msg, field_value=None, success_url=None):
     """
     Code common to the archive/delete/undelete actions.
         recipient_rows = Message.objects.as_recipient(user, filter).update(**{'recipient_{0}'.format(field_bit): field_value})
         sender_rows = Message.objects.as_sender(user, filter).update(**{'sender_{0}'.format(field_bit): field_value})
         if not (recipient_rows or sender_rows):
-            raise Http404 # abnormal enough, like forged ids
+            raise Http404  # abnormal enough, like forged ids
         messages.success(request, success_msg, fail_silently=True)
         return redirect(request.GET.get('next', success_url or next_url))
     else:
         messages.warning(request, _("Select at least one object."), fail_silently=True)
         return redirect(next_url)
 
+
 @login_required
 def archive(request, *args, **kwargs):
     """Mark messages/conversations as archived."""
     return _update(request, 'archived', _("Messages or conversations successfully archived."), True, *args, **kwargs)
 
+
 @login_required
 def delete(request, *args, **kwargs):
     """Mark messages/conversations as deleted."""
     return _update(request, 'deleted_at', _("Messages or conversations successfully deleted."), now(), *args, **kwargs)
 
+
 @login_required
 def undelete(request, *args, **kwargs):
     """Revert messages/conversations from marked as deleted."""
     return _update(request, 'deleted_at', _("Messages or conversations successfully recovered."), *args, **kwargs)
+
+
+@login_required
+def mark_as_read(request, *args, **kwargs):
+    """Mark messages/conversations as readed."""
+    return _update(request, 'read_at', _("Messages or conversations successfully marked as read."), now(), *args, **kwargs)
+
+
+@login_required
+def mark_as_unread(request, *args, **kwargs):
+    """Mark messages/conversations as unreaded."""
+    return _update(request, 'read_at', _("Messages or conversations successfully marked as read."), None, *args, **kwargs)