From 4f77220aec05a74bb4b2c74c0874bd4134acc543 Mon Sep 17 00:00:00 2001 From: ShraddhaAg Date: Mar 06 2019 10:21:06 +0000 Subject: Add ability to see my sent and received packets This commit adds views, url mapping and templates for the following 2 new changes: 1. A new page that lists all the packets sent by the current user. 2. A new page that lists all the packets received by the current user. --- diff --git a/happinesspackets/messaging/urls.py b/happinesspackets/messaging/urls.py index ada6bfb..e662216 100644 --- a/happinesspackets/messaging/urls.py +++ b/happinesspackets/messaging/urls.py @@ -4,13 +4,15 @@ from django.conf.urls import url from .views import (StartView, MessageSendView, MessageSenderConfirmationSentView, MessageSenderConfirmationView, MessageSenderConfirmedView, MessageRecipientMessageUpdate, FaqView, ArchiveView, InspirationView, - BlacklistEmailView) + BlacklistEmailView, ReceivedMessagesView, SentMessagesView) urlpatterns = [ url(r'^$', StartView.as_view(), name='start'), url(r'^faq/$', FaqView.as_view(), name='faq'), url(r'^archive/$', ArchiveView.as_view(), name='archive'), url(r'^inspiration/$', InspirationView.as_view(), name='inspiration'), + url(r'^received-messages/$', ReceivedMessagesView.as_view(), name='received_messages'), + url(r'^sent-messages/$', SentMessagesView.as_view(), name='sent_messages'), url(r'^blacklist-email/(?P[\w\.@\+-]+)/(?P\w+)/$', BlacklistEmailView.as_view(), name='blacklist_email'), url(r'^send/$', MessageSendView.as_view(), name='send'), url(r'^send/confirmation-sent/$', MessageSenderConfirmationSentView.as_view(), name='sender_confirmation_sent'), diff --git a/happinesspackets/messaging/views.py b/happinesspackets/messaging/views.py index eb7beae..914d182 100644 --- a/happinesspackets/messaging/views.py +++ b/happinesspackets/messaging/views.py @@ -12,6 +12,7 @@ from django.utils.html import format_html from django.views.decorators.debug import sensitive_post_parameters from django.views.generic import FormView, TemplateView, UpdateView, ListView from django.contrib.auth.mixins import LoginRequiredMixin +from django.db.models import Q from .forms import MessageSendForm, MessageRecipientForm from .models import Message, BLACKLIST_HMAC_SALT, BlacklistedEmail, strip_email @@ -147,3 +148,30 @@ class MessageRecipientMessageUpdate(UpdateView): recipient_name = msg.recipient_name if msg.recipient_approved_public_named else "Anonymous" messages.success(self.request, "Your choices have been saved.") return HttpResponseRedirect(self.request.path) + + +class UserMessageView(LoginRequiredMixin, ListView): + login_url = '/oidc/authenticate/' + model = Message + paginate_by = 5 + + def get_queryset(self): + queryset = super(UserMessageView, self).get_queryset() + return queryset.filter(Q(status='sent') | Q(status='read')) + + +class ReceivedMessagesView(UserMessageView): + template_name = 'messaging/received_messages.html' + + def get_queryset(self): + fedoraproject_email = str(self.request.user.username) + '@fedoraproject.org' + queryset = super(ReceivedMessagesView, self).get_queryset() + return queryset.filter(Q(recipient_email=self.request.user.email) | Q(recipient_email=fedoraproject_email)) + + +class SentMessagesView(UserMessageView): + template_name = 'messaging/sent_messages.html' + + def get_queryset(self): + queryset = super(SentMessagesView, self).get_queryset() + return queryset.filter(sender_email=self.request.user.email) diff --git a/templates/base.html b/templates/base.html index df27945..b634224 100644 --- a/templates/base.html +++ b/templates/base.html @@ -37,8 +37,12 @@
  • FAQ
  • {% url 'messaging:archive' as url %}
  • Happiness Archive
  • - {% if user.is_authenticated %} + {% url 'messaging:received_messages' as url %} +
  • My Received Packets
  • + {% url 'messaging:sent_messages' as url %} +
  • My Sent Packets
  • +
    {% csrf_token %}