Skip to content
Browse files

Converted friendship action views to class based views

  • Loading branch information...
1 parent 78327a7 commit c2742f55bb405f8d2b78e9d98a313ab8e395c17b @muhuk committed Dec 11, 2011
Showing with 150 additions and 174 deletions.
  1. +150 −174 friends/views.py
View
324 friends/views.py
@@ -16,8 +16,9 @@
.. autofunction:: unblock_user
"""
-from django.http import HttpResponseRedirect, HttpResponseBadRequest, Http404
+from django.http import HttpResponseBadRequest, Http404
from django.db import IntegrityError, transaction
+from django.views.generic.base import RedirectView
from django.views.generic.list_detail import object_list
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext
@@ -27,47 +28,6 @@
from app_settings import REDIRECT_FALLBACK_TO_PROFILE
-def limit_to_others(f):
- def _f(request, username, *args, **kwargs):
- if request.user.username == username:
- return HttpResponseBadRequest(ugettext(u'You can\'t befriend ' \
- u'yourself.'))
- return f(request, username, *args, **kwargs)
- return _f
-
-
-def apply_redirect_to(f):
- def _f(request, *args, **kwargs):
- if 'redirect_to' in kwargs:
- return f(request, *args, **kwargs)
- elif 'redirect_to_param' in kwargs and \
- kwargs['redirect_to_param'] in request.REQUEST:
- redirect_to = request.REQUEST[kwargs['redirect_to_param']]
- elif 'redirect_to' in request.REQUEST:
- redirect_to = request.REQUEST['next']
- else:
- if REDIRECT_FALLBACK_TO_PROFILE:
- redirect_to = request.user.get_profile().get_absolute_url()
- else:
- redirect_to = request.META.get('HTTP_REFERER', '/')
- return f(request, redirect_to=redirect_to, *args, **kwargs)
- return _f
-
-
-def apply_other_user(f):
- def _f(request, username, redirect_to, *args, **kwargs):
- other_user = get_object_or_404(User, username=username)
- if Friendship.objects.are_friends(request.user, other_user):
- message = ugettext(u'You are already friends with %(user)s')
- request.user.message_set.create(message=message % {
- 'user': other_user.get_full_name() or other_user.username})
- return HttpResponseRedirect(redirect_to)
- else:
- return f(request, username, redirect_to, other_user, *args,
- **kwargs)
- return _f
-
-
@login_required
def friend_list(request,
username=None,
@@ -108,138 +68,154 @@ def friend_list(request,
template_object_name=template_object_name)
-@login_required
-@limit_to_others
-@apply_redirect_to
-@apply_other_user
-@transaction.commit_manually
-def friendship_request(request, username, redirect_to, other_user, **kwargs):
- try:
- # Check if the other user have already requested friendship
- FriendshipRequest.objects.get(from_user=other_user,
- to_user=request.user)
- except FriendshipRequest.DoesNotExist:
- pass
- else:
- result = _friendship_accept(other_user, request.user, redirect_to)
- transaction.commit()
- return result
- request_message = request.REQUEST.get('message', u'')
- try:
- FriendshipRequest.objects.create(from_user=request.user,
- to_user=other_user,
- message=request_message)
- except IntegrityError:
- transaction.rollback()
- message = ugettext(u'You already have an active friend ' \
- u'invitation for %(user)s.')
- else:
- message = ugettext(u'You have requested to be friends with %(user)s.')
- request.user.message_set.create(message=message % {
- 'user': other_user.get_full_name() or other_user.username})
- transaction.commit()
- return HttpResponseRedirect(redirect_to)
-
-
-@login_required
-@limit_to_others
-@apply_redirect_to
-@apply_other_user
-def friendship_accept(request, username, redirect_to, other_user, **kwargs):
- return _friendship_accept(other_user, request.user, redirect_to)
-
-
-def _friendship_accept(from_user, to_user, redirect_to):
- get_object_or_404(FriendshipRequest,
- from_user=from_user,
- to_user=to_user).accept()
- message = ugettext(u'You are now friends with %(user)s.')
- to_user.message_set.create(message=message % {
- 'user': from_user.get_full_name() or from_user.username})
- from_user.message_set.create(message=message % {
- 'user': to_user.get_full_name() or to_user.username})
- return HttpResponseRedirect(redirect_to)
-
-
-@login_required
-@limit_to_others
-@apply_redirect_to
-@apply_other_user
-def friendship_decline(request, username, redirect_to, other_user, **kwargs):
- get_object_or_404(FriendshipRequest,
- from_user=other_user,
- to_user=request.user).decline()
- message = ugettext(u'You declined friendship request of %(user)s.')
- request.user.message_set.create(message=message % {
- 'user': other_user.get_full_name() or other_user.username})
- message = ugettext(u'%(user)s has declined your friendship request.')
- other_user.message_set.create(message=message % {
- 'user': request.user.get_full_name() or request.user.username})
- return HttpResponseRedirect(redirect_to)
-
-
-@login_required
-@limit_to_others
-@apply_redirect_to
-@apply_other_user
-def friendship_cancel(request, username, redirect_to, other_user, **kwargs):
- get_object_or_404(FriendshipRequest,
- from_user=request.user,
- to_user=other_user).cancel()
- message = ugettext(u'You cancelled your request to be friends ' \
- u'with %(user)s.')
- request.user.message_set.create(message=message % {
- 'user': other_user.get_full_name() or other_user.username})
- return HttpResponseRedirect(redirect_to)
-
-
-@login_required
-@limit_to_others
-@apply_redirect_to
-def friendship_delete(request, username, redirect_to, **kwargs):
- other_user = get_object_or_404(User, username=username)
- if Friendship.objects.are_friends(request.user, other_user) is False:
- raise Http404('You are not friends with %s.' % \
- (other_user.get_full_name() or other_user.username,))
- Friendship.objects.unfriend(request.user, other_user)
- message = ugettext(u'You are no longer friends with %(user)s.')
- request.user.message_set.create(message=message % {
- 'user': other_user.get_full_name() or other_user.username})
- message = ugettext(u'%(user)s has removed you as a friend.')
- other_user.message_set.create(message=message % {
- 'user': request.user.get_full_name() or request.user.username})
- return HttpResponseRedirect(redirect_to)
-
-
-@login_required
-@limit_to_others
-@apply_redirect_to
-def block_user(request, username, redirect_to, **kwargs):
- other_user = get_object_or_404(User, username=username)
- try:
- request.user.user_blocks.blocks.get(pk=other_user.pk)
- except User.DoesNotExist:
- request.user.user_blocks.blocks.add(other_user)
- message = ugettext(u'You have blocked %(user)s.')
- else:
- message = ugettext(u'%(user)s is already blocked.')
- request.user.message_set.create(message=message % {
- 'user': other_user.get_full_name() or other_user.username})
- return HttpResponseRedirect(redirect_to)
+class BaseFriendshipActionView(RedirectView):
+ http_method_names = ['get', 'post']
+ permanent = False
+ def set_url(self, request, **kwargs):
+ if 'redirect_to' in kwargs:
+ self.url = kwargs['redirect_to']
+ elif 'redirect_to_param' in kwargs and \
+ kwargs['redirect_to_param'] in request.REQUEST:
+ self.url = request.REQUEST[kwargs['redirect_to_param']]
+ elif 'redirect_to' in request.REQUEST:
+ self.url = request.REQUEST['next']
+ elif REDIRECT_FALLBACK_TO_PROFILE:
+ self.url = request.user.get_profile().get_absolute_url()
+ else:
+ self.url = request.META.get('HTTP_REFERER', '/')
-@login_required
-@limit_to_others
-@apply_redirect_to
-def unblock_user(request, username, redirect_to, **kwargs):
- other_user = get_object_or_404(User, username=username)
- try:
- request.user.user_blocks.blocks.get(pk=other_user.pk)
- except User.DoesNotExist:
- message = ugettext(u'%(user)s was not blocked.')
- else:
- request.user.user_blocks.blocks.remove(other_user)
- message = ugettext(u'You have unblocked %(user)s.')
- request.user.message_set.create(message=message % {
+ def get(self, request, username, *args, **kwargs):
+ if request.user.username == username:
+ return HttpResponseBadRequest(ugettext(u'You can\'t befriend ' \
+ u'yourself.'))
+ user = get_object_or_404(User, username=username)
+ self.action(request, user, *args, **kwargs)
+ self.set_url(request, **kwargs)
+ return super(BaseFriendshipActionView, self).get(request, **kwargs)
+
+
+class FriendshipAcceptView(BaseFriendshipActionView):
+ @transaction.commit_on_success
+ def accept_friendship(self, from_user, to_user):
+ get_object_or_404(FriendshipRequest,
+ from_user=from_user,
+ to_user=to_user).accept()
+ message = ugettext(u'You are now friends with %(user)s.')
+ to_user.message_set.create(message=message % {
+ 'user': from_user.get_full_name() or from_user.username})
+ from_user.message_set.create(message=message % {
+ 'user': to_user.get_full_name() or to_user.username})
+
+ def action(self, request, user, **kwargs):
+ other_user = user
+ self.accept_friendship(other_user, request.user)
+
+
+class FriendshipRequestView(FriendshipAcceptView):
+ @transaction.commit_on_success
+ def action(self, request, user, **kwargs):
+ other_user = user
+ if Friendship.objects.are_friends(request.user, other_user):
+ message = ugettext(u'You are already friends with %(user)s')
+ request.user.message_set.create(message=message % {
'user': other_user.get_full_name() or other_user.username})
- return HttpResponseRedirect(redirect_to)
+ raise RuntimeError
+ try:
+ # Check if the other user have already requested friendship
+ self.accept_friendship(other_user, request.user)
+ except Http404:
+ pass
+
+ request_message = request.REQUEST.get('message', u'')
+ try:
+ FriendshipRequest.objects.create(from_user=request.user,
+ to_user=other_user,
+ message=request_message)
+ except IntegrityError:
+ transaction.rollback()
+ message = ugettext(u'You already have an active friend ' \
+ u'invitation for %(user)s.')
+ else:
+ message = ugettext(u'You have requested to be ' \
+ u'friends with %(user)s.')
+ request.user.message_set.create(message=message % {
+ 'user': other_user.get_full_name() or other_user.username})
+
+
+class FriendshipDeclineView(BaseFriendshipActionView):
+ def action(self, request, user, **kwargs):
+ other_user = user
+ get_object_or_404(FriendshipRequest,
+ from_user=other_user,
+ to_user=request.user).decline()
+ message = ugettext(u'You declined friendship request of %(user)s.')
+ request.user.message_set.create(message=message % {
+ 'user': other_user.get_full_name() or other_user.username})
+ message = ugettext(u'%(user)s has declined your friendship request.')
+ other_user.message_set.create(message=message % {
+ 'user': request.user.get_full_name() or request.user.username})
+
+
+class FriendshipCancelView(BaseFriendshipActionView):
+ def action(self, request, user, **kwargs):
+ other_user = user
+ get_object_or_404(FriendshipRequest,
+ from_user=request.user,
+ to_user=other_user).cancel()
+ message = ugettext(u'You cancelled your request to be friends ' \
+ u'with %(user)s.')
+ request.user.message_set.create(message=message % {
+ 'user': other_user.get_full_name() or other_user.username})
+
+
+class FriendshipDeleteView(BaseFriendshipActionView):
+ def action(self, request, user, **kwargs):
+ other_user = user
+ if Friendship.objects.are_friends(request.user, other_user) is False:
+ raise Http404('You are not friends with %s.' % \
+ (other_user.get_full_name() or other_user.username,))
+ Friendship.objects.unfriend(request.user, other_user)
+ message = ugettext(u'You are no longer friends with %(user)s.')
+ request.user.message_set.create(message=message % {
+ 'user': other_user.get_full_name() or other_user.username})
+ message = ugettext(u'%(user)s has removed you as a friend.')
+ other_user.message_set.create(message=message % {
+ 'user': request.user.get_full_name() or request.user.username})
+
+
+class FriendshipBlockView(BaseFriendshipActionView):
+ def action(self, request, user, **kwargs):
+ other_user = user
+ try:
+ request.user.user_blocks.blocks.get(pk=other_user.pk)
+ except User.DoesNotExist:
+ request.user.user_blocks.blocks.add(other_user)
+ message = ugettext(u'You have blocked %(user)s.')
+ else:
+ message = ugettext(u'%(user)s is already blocked.')
+ request.user.message_set.create(message=message % {
+ 'user': other_user.get_full_name() or other_user.username})
+
+
+class FriendshipUnblockView(BaseFriendshipActionView):
+ def action(self, request, user, **kwargs):
+ other_user = user
+ try:
+ request.user.user_blocks.blocks.get(pk=other_user.pk)
+ except User.DoesNotExist:
+ message = ugettext(u'%(user)s was not blocked.')
+ else:
+ request.user.user_blocks.blocks.remove(other_user)
+ message = ugettext(u'You have unblocked %(user)s.')
+ request.user.message_set.create(message=message % {
+ 'user': other_user.get_full_name() or other_user.username})
+
+
+friendship_request = login_required(FriendshipRequestView.as_view())
+friendship_accept = login_required(FriendshipAcceptView.as_view())
+friendship_decline = login_required(FriendshipDeclineView.as_view())
+friendship_cancel = login_required(FriendshipCancelView.as_view())
+friendship_delete = login_required(FriendshipDeleteView.as_view())
+block_user = login_required(FriendshipBlockView.as_view())
+unblock_user = login_required(FriendshipUnblockView.as_view())

0 comments on commit c2742f5

Please sign in to comment.
Something went wrong with that request. Please try again.