diff options
author | hrcerqueira <hrcerqueira@gmail.com> | 2010-01-22 20:55:07 +0000 |
---|---|---|
committer | hrcerqueira <hrcerqueira@gmail.com> | 2010-01-22 20:55:07 +0000 |
commit | c4ed047a3b2248aa7544df02f9d1bceaeb8d633c (patch) | |
tree | adae918a2b74fbce5b25be614234727c93cf431d /fbconnect/views.py | |
parent | d305ad98090558eb872c97f69addd2163f8ba5bd (diff) | |
download | askbot-c4ed047a3b2248aa7544df02f9d1bceaeb8d633c.tar.gz askbot-c4ed047a3b2248aa7544df02f9d1bceaeb8d633c.tar.bz2 askbot-c4ed047a3b2248aa7544df02f9d1bceaeb8d633c.zip |
Handled the returning user case, and the proper redirects in the case of an anonymous question or answer.
Diffstat (limited to 'fbconnect/views.py')
-rwxr-xr-x | fbconnect/views.py | 90 |
1 files changed, 62 insertions, 28 deletions
diff --git a/fbconnect/views.py b/fbconnect/views.py index 2acb1dc0..5c308e45 100755 --- a/fbconnect/views.py +++ b/fbconnect/views.py @@ -1,10 +1,10 @@ -from django.shortcuts import render_to_response as render
+from django.shortcuts import render_to_response as render from django.template import RequestContext from django.http import HttpResponseRedirect from django.utils.safestring import mark_safe from django.core.urlresolvers import reverse from django.contrib.auth.models import User -from django.contrib.auth import login +from django.contrib.auth import login, logout from models import FBAssociation from forum.forms import EditUserEmailFeedsForm from django.conf import settings @@ -14,50 +14,84 @@ import forms import logging -def signin(request): - user_state = fb.get_user_state(request) +def signin(request, newquestion = False, newanswer = False): + state, context = fb.get_user_state(request) - if user_state == fb.STATES['FIRSTTIMER']: - return HttpResponseRedirect(reverse('fb_user_register')) + if state == fb.STATES['FIRSTTIMER']: + if newquestion: + register_url = 'fb_user_register_new_question' + elif newanswer: + register_url = 'fb_user_register_new_answer' + else: + register_url = 'fb_user_register' + return HttpResponseRedirect(reverse(register_url)) + elif state == fb.STATES['RETURNINGUSER']: + return login_and_forward(request, context, newquestion, newanswer) + elif state == fb.STATES['SESSIONEXPIRED']: + response = logout(request, next_page=reverse('index')) + fb.delete_cookies(response) + return response + + return HttpResponseRedirect(reverse('index')) - return HttpResponseRedirect('/') +def register(request, newquestion = False, newanswer = False): + state, context = fb.get_user_state(request) -def register(request): - if fb.get_user_state(request) == fb.STATES['FIRSTTIMER']: - user_data = fb.get_user_data(request.COOKIES) + if state == fb.STATES['FIRSTTIMER']: if 'bnewaccount' in request.POST.keys(): form1 = forms.FBConnectRegisterForm(request.POST) email_feeds_form = EditUserEmailFeedsForm(request.POST) if (form1.is_valid() and email_feeds_form.is_valid()): - tmp_pwd = User.objects.make_random_password()
- user_ = User.objects.create_user(form1.cleaned_data['username'],
- form1.cleaned_data['email'], tmp_pwd)
-
+ tmp_pwd = User.objects.make_random_password() + user_ = User.objects.create_user(form1.cleaned_data['username'], + form1.cleaned_data['email'], tmp_pwd) + user_.set_unusable_password() - uassoc = FBAssociation(user=user_, fbuid=user_data['uid']) + uassoc = FBAssociation(user=user_, fbuid=context['uid']) uassoc.save() - user_.backend = "django.contrib.auth.backends.ModelBackend"
- login(request, user_)
email_feeds_form.save(user_) - return HttpResponseRedirect('/') + return login_and_forward(request, user_, newquestion, newanswer) else: - form1 = forms.FBConnectRegisterForm(initial={
- 'next': '/',
- 'username': user_data['name'],
- 'email': '',
+ form1 = forms.FBConnectRegisterForm(initial={ + 'next': '/', + 'username': context['name'], + 'email': '', }) email_feeds_form = EditUserEmailFeedsForm() - return render('authopenid/complete.html', {
- 'form1': form1,
- 'email_feeds_form': email_feeds_form,
- 'provider':mark_safe('facebook'),
- 'login_type':'facebook',
- 'gravatar_faq_url':reverse('faq') + '#gravatar',
+ return render('authopenid/complete.html', { + 'form1': form1, + 'email_feeds_form': email_feeds_form, + 'provider':mark_safe('facebook'), + 'login_type':'facebook', + 'gravatar_faq_url':reverse('faq') + '#gravatar', }, context_instance=RequestContext(request)) + else: + return HttpResponseRedirect(reverse('index')) + +def login_and_forward(request, user, newquestion = False, newanswer = False): + old_session = request.session.session_key + user.backend = "django.contrib.auth.backends.ModelBackend" + login(request, user) + + from forum.models import user_logged_in + user_logged_in.send(user=user,session_key=old_session,sender=None) + + if (newquestion): + from forum.models import Question + question = Question.objects.filter(author=user).order_by('-added_at')[0] + return HttpResponseRedirect(question.get_absolute_url()) + + if (newanswer): + from forum.models import Answer + answer = Answer.objects.filter(author=user).order_by('-added_at')[0] + return HttpResponseRedirect(answer.get_absolute_url()) + + return HttpResponseRedirect('/') + |