Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PinaxMessageBaseAuthMixin proposed #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pinax/messages/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib.auth.mixins import LoginRequiredMixin


class PinaxMessageBaseAuthMixin(LoginRequiredMixin):
"""
This mixin is a replacement for the login_required decorator used.
Pinax Messages uses as a base to apply possible common functionalities
across views in the project and to make the code consistent and
maintainable.
"""
pass
20 changes: 20 additions & 0 deletions pinax/messages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ def get_absolute_url(self):
def first_message(self):
return self.messages.all()[0]

def earliest_message(self, user_to_exclude=None):
"""
:param user_to_exclude: Exclude the messages from a given user
Returns the earliest message sent
"""
try:
return self.messages.exclude(sender=user_to_exclude).earliest('sent_at')
except Message.DoesNotExist:
return

def last_message(self, user_to_exclude=None):
"""
:param user_to_exclude: Exclude the messages from a given user
Returns the earliest message of the thread
"""
try:
return self.messages.exclude(sender=user_to_exclude).latest('sent_at')
except Message.DoesNotExist:
return

@property
@cached_attribute
def latest_message(self):
Expand Down
31 changes: 5 additions & 26 deletions pinax/messages/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic import (
CreateView,
DeleteView,
Expand All @@ -10,23 +9,15 @@

from .forms import MessageReplyForm, NewMessageForm, NewMessageFormMultiple
from .models import Thread
from .mixins import PinaxMessageBaseAuthMixin

try:
from account.decorators import login_required
except: # noqa
from django.contrib.auth.decorators import login_required


class InboxView(TemplateView):
class InboxView(PinaxMessageBaseAuthMixin, TemplateView):
"""
View inbox thread list.
"""
template_name = "pinax/messages/inbox.html"

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.kwargs.get("deleted", None):
Expand All @@ -44,7 +35,7 @@ def get_context_data(self, **kwargs):
return context


class ThreadView(UpdateView):
class ThreadView(PinaxMessageBaseAuthMixin, UpdateView):
"""
View a single Thread or POST a reply.
"""
Expand All @@ -54,10 +45,6 @@ class ThreadView(UpdateView):
template_name = "pinax/messages/thread_detail.html"
success_url = reverse_lazy("pinax_messages:inbox")

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def get_queryset(self):
qs = super().get_queryset()
qs = qs.filter(userthread__user=self.request.user).distinct()
Expand All @@ -77,16 +64,12 @@ def get(self, request, *args, **kwargs):
return response


class MessageCreateView(CreateView):
class MessageCreateView(PinaxMessageBaseAuthMixin, CreateView):
"""
Create a new thread message.
"""
template_name = "pinax/messages/message_create.html"

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def get_form_class(self):
if self.form_class is None:
if self.kwargs.get("multiple", False):
Expand All @@ -111,18 +94,14 @@ def get_form_kwargs(self):
return kwargs


class ThreadDeleteView(DeleteView):
class ThreadDeleteView(PinaxMessageBaseAuthMixin, DeleteView):
"""
Delete a thread.
"""
model = Thread
success_url = reverse_lazy("pinax_messages:inbox")
template_name = "pinax/messages/thread_confirm_delete.html"

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def delete(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
Expand Down