Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python decorators are perfect for this.</p> <p>Define all the validations you are doing within the decorator and decorate all your <code>add_friend</code>, <code>remove_friend</code> etc, with that.</p> <p>Update:</p> <p>For your case it is going to look something like this:</p> <pre><code>def do_friending_validation(fun): def validate_function(*args,**kwargs): try: user = User.objects.get(username=username) except User.DoesNotExist: messages.error(request, 'A user with the username %s does not exist. \ Try searching for the user below.' % username) return HttpResponseRedirect(reverse('friends_find_friend')) if user == request.user: messages.error(request, 'You are already friends with yourself') return HttpResponseRedirect(reverse('friends_find_friend')) if Enemy.objects.is_blocked(request.user, user): messages.error(request, '%s has blocked you from adding them as a friend' % user) return HttpResponseRedirect(reverse('friends_find_friend')) if Enemy.objects.has_blocked(request.user, user): messages.error(request, 'You have blocked %s so you cannot add them as a friend' % user) return HttpResponseRedirect(reverse('profiles_profile_detail', args=[user])) if Friend.objects.are_friends(request.user, user): messages.error(request, 'You are already friends with %s' % user) return HttpResponseRedirect(reverse('profiles_profile_detail', args=[user])) if FriendRequest.objects.invitation_sent(request.user, user): messages.error(request, 'You already sent %s a request. You need to \ wait for them to reply to it.' % user) return HttpResponseRedirect(reverse('friends_pending')) if FriendRequest.objects.invitation_received(request.user, user): messages.error(request, '%s already sent you a request and is waiting \ for you to respond to them.' % user) return HttpResponseRedirect(reverse('friends_pending')) fun(*args,**kwargs) return validate_function @do_friending_validation def add_friend(request, username): #Do your stuff here @do_friending_validation def remove_friend(request, username): #Do your stuff here </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload