def friend_add(request): if 'username' in request.GET: friend = get_object_or_404(User, username=request.GET['username']) friendship = Friendship( from_friend=request.user, to_friend=friend ) try: friendship.save() request.user.message_set.create( message=u'%s was added to your friend list.' % friend.username ) except: request.user.message_set.create( message=u'%s is already one of your friends.' % friend.username ) # To add the reverse friendship directly, uncomment the lines below # friendship = Friendship( # from_friend=friend, # to_friend=request.user # ) # friendship.save() return HttpResponseRedirect('/friends/%s/' % request.user.username) else: raise Http404
def friend_add(request): if request.GET.has_key('username'): friend = get_object_or_404(User, username=request.GET['username']) friendship = Friendship( from_friend = request.user, to_friend = friend ) try: friendship.save() request.user.message_set.create(message='%s has been added as friend.' % friend.username) except: request.user.message_set.create(message='%s is already a friend.' % friend.username) return HttpResponseRedirect('/friends/%s/' % (request.user.username) ) else: raise Http404
def register_page(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): user = User.objects.create_user( username=form.cleaned_data['username'], email=form.cleaned_data['email'], password=form.cleaned_data['password1'] ) if 'invitation' in request.session: # Retrieve the invitation object invitation = Invitation.objects.get(id = request.session['invitation']) # Create the friendship friendship = Friendship( from_friend = user, to_friend = invitation.sender ) friendship.save() # Create reverse friendship friendship = Friendship( from_friend = invitation.sender, to_friend = user ) friendship.save() # Delete invitation in DB invitation.delete() # Clean session object del request.session['invitation'] return HttpResponseRedirect('/register/success/') else: form = RegistrationForm() variables = RequestContext(request, { 'form': form }) return render_to_response('registration/register.html', variables)