diff options
Diffstat (limited to 'fbconnect')
-rwxr-xr-x | fbconnect/fb.py | 9 | ||||
-rwxr-xr-x | fbconnect/urls.py | 5 | ||||
-rwxr-xr-x | fbconnect/views.py | 33 |
3 files changed, 36 insertions, 11 deletions
diff --git a/fbconnect/fb.py b/fbconnect/fb.py index 99bc0b79..afcd8210 100755 --- a/fbconnect/fb.py +++ b/fbconnect/fb.py @@ -73,17 +73,24 @@ STATES = { def get_user_state(request): API_KEY = settings.FB_API_KEY + logging.debug('') if API_KEY in request.COOKIES: + logging.debug('FB API key is in request cookies') if check_cookies_signature(request.COOKIES): + logging.debug('FB cookie signature is fine') if check_session_expiry(request.COOKIES): + logging.debug('FB session is not expired') try: uassoc = FBAssociation.objects.get(fbuid=request.COOKIES[API_KEY + '_user']) + logging.debug('found existing FB user association') return (STATES['RETURNINGUSER'], uassoc.user) except: + logging.debug('dont have FB association for this user') return (STATES['FIRSTTIMER'], get_user_data(request.COOKIES)) else: + logging.debug('FB session expired') return (STATES['SESSIONEXPIRED'], None) + logging.debug('FB state is INVALID') return (STATES['INVALIDSTATE'], None) - diff --git a/fbconnect/urls.py b/fbconnect/urls.py index 9b4ff0c9..bf2d4364 100755 --- a/fbconnect/urls.py +++ b/fbconnect/urls.py @@ -1,10 +1,13 @@ from django.conf.urls.defaults import * from django.utils.translation import ugettext as _ from django.views.generic.simple import direct_to_template +from django.conf import settings from views import signin, register urlpatterns = patterns('', - url(r'^xd_receiver$', direct_to_template, {'template': 'fbconnect/xd_receiver.html'}, name='xd_receiver'), + url(r'^xd_receiver$', direct_to_template, {'template': 'fbconnect/xd_receiver.html',\ + 'extra_context': {'APP_SHORT_NAME':settings.APP_SHORT_NAME}},\ + name='xd_receiver'), url(r'^%s$' % _('signin/'), signin, name="fb_signin"), url(r'^%s%s$' % (_('signin/'), _('newquestion/')), signin, {'newquestion': True}, name="fb_signin_new_question"), diff --git a/fbconnect/views.py b/fbconnect/views.py index 5c308e45..1781f6bf 100755 --- a/fbconnect/views.py +++ b/fbconnect/views.py @@ -6,7 +6,7 @@ from django.core.urlresolvers import reverse from django.contrib.auth.models import User from django.contrib.auth import login, logout from models import FBAssociation -from forum.forms import EditUserEmailFeedsForm +from forum.forms import SimpleEmailSubscribeForm from django.conf import settings import fb @@ -15,9 +15,11 @@ import forms import logging def signin(request, newquestion = False, newanswer = False): + logging.debug('') state, context = fb.get_user_state(request) if state == fb.STATES['FIRSTTIMER']: + logging.debug('FB state = FIRSTTIMER') if newquestion: register_url = 'fb_user_register_new_question' elif newanswer: @@ -26,8 +28,10 @@ def signin(request, newquestion = False, newanswer = False): register_url = 'fb_user_register' return HttpResponseRedirect(reverse(register_url)) elif state == fb.STATES['RETURNINGUSER']: + logging.debug('FB state = RETURNINGUSER') return login_and_forward(request, context, newquestion, newanswer) elif state == fb.STATES['SESSIONEXPIRED']: + logging.debug('FB state = SESSIONEXPIRED') response = logout(request, next_page=reverse('index')) fb.delete_cookies(response) return response @@ -35,36 +39,41 @@ def signin(request, newquestion = False, newanswer = False): return HttpResponseRedirect(reverse('index')) def register(request, newquestion = False, newanswer = False): + logging.debug('') state, context = fb.get_user_state(request) if state == fb.STATES['FIRSTTIMER']: - - if 'bnewaccount' in request.POST.keys(): + logging.debug('FB FIRSTTIMER - try to register locally') + logging.debug('request method is %s' % request.method) + if request.method == 'POST' and 'bnewaccount' in request.POST: form1 = forms.FBConnectRegisterForm(request.POST) - email_feeds_form = EditUserEmailFeedsForm(request.POST) + email_feeds_form = SimpleEmailSubscribeForm(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) - + user_.set_unusable_password() + logging.debug('created new internal user %s' % form1.cleaned_data['username']) uassoc = FBAssociation(user=user_, fbuid=context['uid']) uassoc.save() + logging.debug('created new user association') email_feeds_form.save(user_) return login_and_forward(request, user_, newquestion, newanswer) + else: + logging.debug('form user input is invalid') else: form1 = forms.FBConnectRegisterForm(initial={ 'next': '/', 'username': context['name'], 'email': '', }) - - email_feeds_form = EditUserEmailFeedsForm() - + email_feeds_form = SimpleEmailSubscribeForm() + return render('authopenid/complete.html', { 'form1': form1, 'email_feeds_form': email_feeds_form, @@ -73,25 +82,31 @@ def register(request, newquestion = False, newanswer = False): 'gravatar_faq_url':reverse('faq') + '#gravatar', }, context_instance=RequestContext(request)) else: + logging.debug('not a FIRSTTIMER --> redirect to index view') 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" + logging.debug('attached auth.backends.ModelBackend to this FB user') login(request, user) + logging.debug('user logged in!') from forum.models import user_logged_in user_logged_in.send(user=user,session_key=old_session,sender=None) + logging.debug('user_logged_in signal sent') if (newquestion): from forum.models import Question question = Question.objects.filter(author=user).order_by('-added_at')[0] + logging.debug('redirecting to newly posted question') return HttpResponseRedirect(question.get_absolute_url()) if (newanswer): from forum.models import Answer answer = Answer.objects.filter(author=user).order_by('-added_at')[0] + logging.debug('redirecting to newly posted answer') return HttpResponseRedirect(answer.get_absolute_url()) + logging.debug('redirecting to front page') return HttpResponseRedirect('/') - |