# HG changeset patch # User Brian Neal # Date 1409437538 18000 # Node ID 6bbd1473d48e04b1cfc041db32a5e61d13f89033 # Parent 689db80068c840e3538da4ae6493dfdd82e74ed7 Work in progress: rework private messages. diff -r 689db80068c8 -r 6bbd1473d48e messages/urls.py --- a/messages/urls.py Sat Aug 23 14:03:09 2014 -0500 +++ b/messages/urls.py Sat Aug 30 17:25:38 2014 -0500 @@ -1,39 +1,36 @@ """urls for the Messages application""" from django.conf.urls import patterns, url +from django.views.generic.base import RedirectView -urlpatterns = patterns('messages.views', +urlpatterns = patterns('', url(r'^$', - 'index', + RedirectView.as_view(permanent=True, url='/messages/inbox/'), name='messages-index'), - url(r'^(inbox|compose|outbox|trash|options)/$', - 'index', - name='messages-index_named'), - url(r'^options/$', - 'index', - kwargs={'tab': 'options'}, - name='messages-options_tab'), - url(r'^compose/([\w.@+-]{1,30})/$', - 'compose_to', - name='messages-compose_to'), - url(r'^inbox-tab/$', - 'inbox', + url(r'^inbox/$', + 'messages.views.inbox', name='messages-inbox'), - url(r'^outbox-tab/$', - 'outbox', +# url(r'^compose/$', +# 'messages.views.compose', +# name='messages-compose'), + url(r'^outbox/$', + 'messages.views.outbox', name='messages-outbox'), - url(r'^trash-tab/$', - 'trash', - name='messages-trash'), - url(r'^message/$', - 'message', - name='messages-message'), - url(r'^options-tab/$', - 'options', - name='messages-options'), - url(r'^compose-tab/$', - 'compose', - name='messages-compose'), - url(r'^bulk/$', - 'bulk', - name='messages-bulk'), +# url(r'^trash/$', +# 'messages.views.trash', +# name='messages-trash'), +# url(r'^options/$', +# 'messages.views.options', +# name='messages-options'), +# url(r'^delete/$', +# 'messages.views.delete', +# name='messages-delete'), +# url(r'^undelete/$', +# 'messages.views.undelete', +# name='messages-undelete'), +# url(r'^view/(\d+)/$', +# 'messages.views.view', +# name='messages-view'), +# url(r'^report/(\d+)/$', +# 'messages.views.report', +# name='messages-report'), ) diff -r 689db80068c8 -r 6bbd1473d48e messages/views.py --- a/messages/views.py Sat Aug 23 14:03:09 2014 -0500 +++ b/messages/views.py Sat Aug 30 17:25:38 2014 -0500 @@ -8,7 +8,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.contrib import messages as django_messages -from django.core.paginator import Paginator, EmptyPage, InvalidPage +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.urlresolvers import reverse from django.http import HttpResponse from django.http import HttpResponseForbidden @@ -35,14 +35,6 @@ } -def _get_page(request): - try: - n = int(request.GET.get('page', '1')) - except ValueError: - n = 1 - return n - - def _quota_check(box_name, count, request): """ Checks the message box count against MSG_BOX_LIMIT. @@ -58,16 +50,49 @@ return 100 * count / MSG_BOX_LIMIT +def _get_page(request, qs): + """Paginates the given queryset and returns a page object""" + paginator = Paginator(qs, MSGS_PER_PAGE) + try: + page = paginator.page(request.GET.get('page', '1')) + except PageNotAnInteger: + page = paginator.page(1) + except EmptyPage: + page = paginator.page(paginator.num_pages) + return page + + @login_required -def index(request, tab=None): - """ - This function displays the base tabbed private messages view. +def inbox(request): - """ - tab_index = TAB_INDICES[tab] if tab else 0 - return render(request, 'messages/tabbed_base.html', { - 'tab': tab_index, - 'unread_count': Message.objects.unread_count(request.user), + msg_list = Message.objects.inbox(request.user) + msg_count = msg_list.count() + pct_used = _quota_check('inbox', msg_count, request) + + page = _get_page(request, msg_list) + + return render(request, 'messages/inbox.html', { + 'tab': 'inbox', + 'page': page, + 'inbox_pct': pct_used, + 'outbox_pct': None, + }) + + +@login_required +def outbox(request): + + msg_list = Message.objects.outbox(request.user) + msg_count = msg_list.count() + pct_used = _quota_check('outbox', msg_count, request) + + page = _get_page(request, msg_list) + + return render(request, 'messages/outbox.html', { + 'tab': 'outbox', + 'page': page, + 'inbox_pct': None, + 'outbox_pct': pct_used, }) @@ -88,56 +113,6 @@ }) -def inbox(request): - """ - Returns the inbox for the user. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - msg_list = Message.objects.inbox(request.user) - msg_count = msg_list.count() - pct_used = _quota_check('inbox', msg_count, request) - - paginator = Paginator(msg_list, MSGS_PER_PAGE) - try: - msgs = paginator.page(_get_page(request)) - except (EmptyPage, InvalidPage): - msgs = paginator.page(paginator.num_pages) - - return render(request, 'messages/inbox_tab.html', { - 'msgs': msgs, - 'url': reverse('messages-inbox'), - 'pct_used': pct_used, - }) - - -def outbox(request): - """ - Returns the outbox for the user. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - msg_list = Message.objects.outbox(request.user) - msg_count = msg_list.count() - pct_used = _quota_check('outbox', msg_count, request) - - paginator = Paginator(msg_list, MSGS_PER_PAGE) - try: - msgs = paginator.page(_get_page(request)) - except (EmptyPage, InvalidPage): - msgs = paginator.page(paginator.num_pages) - - return render(request, 'messages/outbox_tab.html', { - 'msgs': msgs, - 'url': reverse('messages-outbox'), - 'pct_used': pct_used, - }) - - def trash(request): """ Returns the trash for the user. diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/inbox.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/messages/inbox.html Sat Aug 30 17:25:38 2014 -0500 @@ -0,0 +1,37 @@ +{% extends 'messages/messages_base.html' %} +{% block messages_content %} +

Inbox (Page {{ page.number }} of {{ page.paginator.num_pages }})

+

+This is your inbox. It contains messages sent to you by others. Messages in bold are unread. +Messages in italics have been replied to. +

+{% if page.object_list %} +
+ + + + + + + + {% for msg in page.object_list %} + + + + + + + {% endfor %} + +
FromSubjectDate
+ {{ msg.sender.username }} + {{ msg.subject }} + {{ msg.send_date|date:"M j, Y g:i A" }}
+
+ {% include "messages/pagination.html" %} +{% else %} +

Your Inbox is empty.

+{% endif %} +{% include 'messages/usage_graph.html' with pct_used=inbox_pct box_name='inbox' %} +{% endblock %} diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/inbox_tab.html --- a/sg101/templates/messages/inbox_tab.html Sat Aug 23 14:03:09 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -{% if messages %} - -{% endif %} -{% if msgs.object_list %} -
- - - - - - - - {% for msg in msgs.object_list %} - - - - - - - {% endfor %} - -
FromSubjectDate
- {{ msg.sender.username }} - {{ msg.subject }} - {{ msg.send_date|date:"M j, Y g:i A" }}
-
- {% include "messages/pagination.html" %} - -{% else %} -

Your Inbox is empty.

-{% endif %} -{% include 'messages/usage_graph.html' with box_name='inbox' %} diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/messages_base.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/messages/messages_base.html Sat Aug 30 17:25:38 2014 -0500 @@ -0,0 +1,34 @@ +{% extends 'base.html' %} +{% load script_tags %} +{% block custom_css %} + +{% endblock %} +{% block content %} +

Your Private Messages

+ +{% if messages %} + +{% endif %} + +

Private messages menu:

+ + +{% block messages_content %}{% endblock %} + +{% endblock %} diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/outbox.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/messages/outbox.html Sat Aug 30 17:25:38 2014 -0500 @@ -0,0 +1,39 @@ +{% extends 'messages/messages_base.html' %} +{% block messages_content %} +

Outbox (Page {{ page.number }} of {{ page.paginator.num_pages }})

+

+This is your outbox. It contains messages you have sent to other members. Messages in bold are unread +by the recipient. Messages in italics have been replied to. +

+{% if page.object_list %} +
+ + + + + + + + + {% for msg in page.object_list %} + + + + + + + + {% endfor %} + +
ToSubjectSentReceived
+ {{ msg.receiver.username }} + {{ msg.subject }} + {{ msg.send_date|date:"M j, Y g:i A" }}{% if msg.unread %}Unread{% else %}{{ msg.read_date|date:"M j, Y g:i A" }}{% endif %}
+
+ {% include "messages/pagination.html" %} +{% else %} +

Your Outbox is empty.

+{% endif %} +{% include 'messages/usage_graph.html' with pct_used=outbox_pct box_name='outbox' %} +{% endblock %} diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/outbox_tab.html --- a/sg101/templates/messages/outbox_tab.html Sat Aug 23 14:03:09 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -{% if messages %} - -{% endif %} -{% if msgs.object_list %} -
- - - - - - - - - {% for msg in msgs.object_list %} - - - - - - - - {% endfor %} - -
ToSubjectSentReceived
- {{ msg.receiver.username }} - {{ msg.subject }} - {{ msg.send_date|date:"M j, Y g:i A" }}{% if msg.unread %}Unread{% else %}{{ msg.read_date|date:"M j, Y g:i A" }}{% endif %}
-
- {% include "messages/pagination.html" %} - -{% else %} -

Your Outbox is empty.

-{% endif %} -{% include 'messages/usage_graph.html' with box_name='outbox' %} diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/pagination.html --- a/sg101/templates/messages/pagination.html Sat Aug 23 14:03:09 2014 -0500 +++ b/sg101/templates/messages/pagination.html Sat Aug 30 17:25:38 2014 -0500 @@ -1,15 +1,15 @@ diff -r 689db80068c8 -r 6bbd1473d48e sg101/templates/messages/tabbed_base.html --- a/sg101/templates/messages/tabbed_base.html Sat Aug 23 14:03:09 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -{% extends 'base.html' %} -{% load script_tags %} -{% block custom_css %} - -{% endblock %} -{% block custom_js %} -{% script_tags 'jquery-ui' %} - - -{% endblock %} -{% block content %} -

Your Private Messages

- -
- -
- -
-{% endblock %}