Mercurial > public > sg101
changeset 801:6bbd1473d48e
Work in progress: rework private messages.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 30 Aug 2014 17:25:38 -0500 |
parents | 689db80068c8 |
children | dbc389a409f5 |
files | messages/urls.py messages/views.py sg101/templates/messages/inbox.html sg101/templates/messages/inbox_tab.html sg101/templates/messages/messages_base.html sg101/templates/messages/outbox.html sg101/templates/messages/outbox_tab.html sg101/templates/messages/pagination.html sg101/templates/messages/tabbed_base.html |
diffstat | 9 files changed, 185 insertions(+), 221 deletions(-) [+] |
line wrap: on
line diff
--- 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'), )
--- 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.
--- /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 %} +<h3>Inbox <small>(Page {{ page.number }} of {{ page.paginator.num_pages }})</small></h3> +<p> +This is your inbox. It contains messages sent to you by others. Messages in <strong>bold</strong> are unread. +Messages in <em>italics</em> have been replied to. +</p> +{% if page.object_list %} + <form action="." onsubmit="return bulkMsgAction(this, 'delete');"> + <table class="messages"> + <tr> + <th>From</th> + <th>Subject</th> + <th>Date</th> + <th><input type="checkbox" onclick="tabMasterCheckClick(this, 'inbox_ids');" /></th> + </tr> + {% for msg in page.object_list %} + <tr> + <td><a href="{% url 'bio.views.view_profile' msg.sender.username %}"> + {{ msg.sender.username }}</a></td> + <td> + <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;" + class="{% if msg.unread %}unread {% endif %}{% if msg.replied_to %}replied_to{% endif %}">{{ msg.subject }}</a> + </td> + <td>{{ msg.send_date|date:"M j, Y g:i A" }}</td> + <td><input type="checkbox" name="inbox_ids" value="{{ msg.id }}" /></td> + </tr> + {% endfor %} + <tr><td colspan="4"><input type="submit" value="Delete Checked Messages" /></td></tr> + </table> + </form> + {% include "messages/pagination.html" %} +{% else %} + <p><em>Your Inbox is empty.</em></p> +{% endif %} +{% include 'messages/usage_graph.html' with pct_used=inbox_pct box_name='inbox' %} +{% endblock %}
--- 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 %} -<ul class="user-messages"> - {% for msg in messages %} - <li{% if msg.tags %} class="{{ msg.tags }}"{% endif %}>{{ msg }}</li> - {% endfor %} -</ul> -{% endif %} -{% if msgs.object_list %} - <form action="." onsubmit="return bulkMsgAction(this, 'delete');"> - <table class="messages"> - <tr> - <th>From</th> - <th>Subject</th> - <th>Date</th> - <th><input type="checkbox" onclick="tabMasterCheckClick(this, 'inbox_ids');" /></th> - </tr> - {% for msg in msgs.object_list %} - <tr> - <td><a href="{% url 'bio.views.view_profile' msg.sender.username %}"> - {{ msg.sender.username }}</a></td> - <td> - <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;" - class="{% if msg.unread %}unread {% endif %}{% if msg.replied_to %}replied_to{% endif %}">{{ msg.subject }}</a> - </td> - <td>{{ msg.send_date|date:"M j, Y g:i A" }}</td> - <td><input type="checkbox" name="inbox_ids" value="{{ msg.id }}" /></td> - </tr> - {% endfor %} - <tr><td colspan="4"><input type="submit" value="Delete Checked Messages" /></td></tr> - </table> - </form> - {% include "messages/pagination.html" %} - <ul> - <li>Messages in <strong>bold</strong> are unread.</li> - <li>Messages in <em>italics</em> have been replied to.</li> - </ul> -{% else %} - <p><em>Your Inbox is empty.</em></p> -{% endif %} -{% include 'messages/usage_graph.html' with box_name='inbox' %}
--- /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 %} +<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/messages.css" /> +{% endblock %} +{% block content %} +<h2>Your Private Messages</h2> + +{% if messages %} +<ul class="user-messages"> +{% for message in messages %} + <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> +{% endfor %} +</ul> +{% endif %} + +<p>Private messages menu:</p> +<ul> + <li>{% if tab != 'inbox' %}<a href="{% url 'messages-inbox' %}">{% endif %}Inbox{% if tab != 'inbox' %}</a>{% endif %} + {% if inbox_pct %}({{ inbox_pct }}% used){% endif %}</li> +{% comment %} + <li>{% if tab != 'compose' %}<a href="{% url 'messages-compose' %}">{% endif %}Compose new message{% if tab != 'compose' %}</a>{% endif %}</li> +{% endcomment %} + <li>{% if tab != 'outbox' %}<a href="{% url 'messages-outbox' %}">{% endif %}Outbox{% if tab != 'outbox' %}</a>{% endif %} + {% if outbox_pct %}({{ outbox_pct }}% used){% endif %}</li> +{% comment %} + <li>{% if tab != 'trash' %}<a href="{% url 'messages-trash' %}">{% endif %}Trash{% if tab != 'trash' %}</a>{% endif %}</li> + <li>{% if tab != 'options' %}<a href="{% url 'messages-options' %}">{% endif %}Options{% if tab != 'options' %}</a>{% endif %}</li> +{% endcomment %} +</ul> + +{% block messages_content %}{% endblock %} + +{% endblock %}
--- /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 %} +<h3>Outbox <small>(Page {{ page.number }} of {{ page.paginator.num_pages }})</small></h3> +<p> +This is your outbox. It contains messages you have sent to other members. Messages in <strong>bold</strong> are unread +by the recipient. Messages in <em>italics</em> have been replied to. +</p> +{% if page.object_list %} + <form action="." onsubmit="return bulkMsgAction(this, 'delete');"> + <table class="messages"> + <tr> + <th>To</th> + <th>Subject</th> + <th>Sent</th> + <th>Received</th> + <th><input type="checkbox" onclick="tabMasterCheckClick(this, 'outbox_ids');" /></th> + </tr> + {% for msg in page.object_list %} + <tr> + <td><a href="{% url 'bio.views.view_profile' msg.receiver.username %}"> + {{ msg.receiver.username }}</a></td> + <td> + <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;" + class="{% if msg.unread %}unread {% endif %}{% if msg.replied_to %}replied_to{% endif %}">{{ msg.subject }}</a> + </td> + <td>{{ msg.send_date|date:"M j, Y g:i A" }}</td> + <td>{% if msg.unread %}<em>Unread</em>{% else %}{{ msg.read_date|date:"M j, Y g:i A" }}{% endif %}</td> + <td><input type="checkbox" name="outbox_ids" value="{{ msg.id }}" /></td> + </tr> + {% endfor %} + <tr><td colspan="5"><input type="submit" value="Delete Checked Messages" /></td></tr> + </table> + </form> + {% include "messages/pagination.html" %} +{% else %} + <p><em>Your Outbox is empty.</em></p> +{% endif %} +{% include 'messages/usage_graph.html' with pct_used=outbox_pct box_name='outbox' %} +{% endblock %}
--- 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 %} -<ul class="user-messages"> - {% for msg in messages %} - <li{% if msg.tags %} class="{{ msg.tags }}"{% endif %}>{{ msg }}</li> - {% endfor %} -</ul> -{% endif %} -{% if msgs.object_list %} - <form action="." onsubmit="return bulkMsgAction(this, 'delete');"> - <table class="messages"> - <tr> - <th>To</th> - <th>Subject</th> - <th>Sent</th> - <th>Received</th> - <th><input type="checkbox" onclick="tabMasterCheckClick(this, 'outbox_ids');" /></th> - </tr> - {% for msg in msgs.object_list %} - <tr> - <td><a href="{% url 'bio.views.view_profile' msg.receiver.username %}"> - {{ msg.receiver.username }}</a></td> - <td> - <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;" - class="{% if msg.unread %}unread {% endif %}{% if msg.replied_to %}replied_to{% endif %}">{{ msg.subject }}</a> - </td> - <td>{{ msg.send_date|date:"M j, Y g:i A" }}</td> - <td>{% if msg.unread %}<em>Unread</em>{% else %}{{ msg.read_date|date:"M j, Y g:i A" }}{% endif %}</td> - <td><input type="checkbox" name="outbox_ids" value="{{ msg.id }}" /></td> - </tr> - {% endfor %} - <tr><td colspan="5"><input type="submit" value="Delete Checked Messages" /></td></tr> - </table> - </form> - {% include "messages/pagination.html" %} - <ul> - <li>Messages in <strong>bold</strong> are unread.</li> - <li>Messages in <em>italics</em> have been replied to.</li> - </ul> -{% else %} - <p><em>Your Outbox is empty.</em></p> -{% endif %} -{% include 'messages/usage_graph.html' with box_name='outbox' %}
--- 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 @@ <div class="pagination"> <span class="step-links"> -{% if msgs.has_previous %} - <a href="{{ url }}?page={{ msgs.previous_page_number }}" onclick="return ajaxPageFetch(this);">« Previous</a> +{% if page.has_previous %} + <a href="{{ url }}?page={{ page.previous_page_number }}" onclick="return ajaxPageFetch(this);">« Previous</a> {% endif %} <span class="current"> - Page {{ msgs.number }} of {{ msgs.paginator.num_pages }}. + Page {{ page.number }} of {{ page.paginator.num_pages }}. </span> -{% if msgs.has_next %} - <a href="{{ url }}?page={{ msgs.next_page_number }}" onclick="return ajaxPageFetch(this);">Next »</a> +{% if page.has_next %} + <a href="{{ url }}?page={{ page.next_page_number }}" onclick="return ajaxPageFetch(this);">Next »</a> {% endif %} </span> </div>
--- 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 %} -<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/messages.css" /> -{% endblock %} -{% block custom_js %} -{% script_tags 'jquery-ui' %} -<script type="text/javascript"> -//<![CDATA[ - var initialTab = {{ tab }}; - var username = "{{ request.user.username }}"; -{% if receiver %} - var receiver = "{{ receiver }}"; -{% else %} - var receiver = ""; -{% endif %} - var unreadMsgCount = {{ unread_count }}; -//]]> -</script> -<script type="text/javascript" src="{{ STATIC_URL }}js/tabbed_messages.js"></script> -{% endblock %} -{% block content %} -<h2>Your Private Messages</h2> - -<div id="tabs"> - <ul> - <li><a href="{% url 'messages-inbox' %}">Inbox</a></li> - <li><a href="{% url 'messages-compose' %}">Compose</a></li> - <li><a href="{% url 'messages-outbox' %}">Outbox</a></li> - <li><a href="{% url 'messages-trash' %}">Trash</a></li> - <li><a href="{% url 'messages-options' %}">Options</a></li> - </ul> -</div> - -<div id="msgDialog"></div> -{% endblock %}