def process_request(request): '''Get (or create) a user preferences object for a given user.''' userprefs = None try: userprefs = request.user.get_profile() if not settings.LANGUAGE_COOKIE_NAME in request.session: request.session[settings.LANGUAGE_COOKIE_NAME] = userprefs.language except AttributeError: # Occurs when this is called on an anonymous user; ignore pass except UserPref.DoesNotExist: log.debug('Creating a userprefs object for %s' % request.user.username) # Create a preference object for this user userprefs = UserPref(user=request.user) userprefs.save()
def register(request): """ register an openid. If user is already a member he can associate its openid with its account. A new account could also be created and automaticaly associated to the openid. url : /complete/ template : authopenid/complete.html """ is_redirect = False next = request.GET.get('next', '').strip() if not next or not is_valid_next_url(next): next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library')) openid_ = request.session.get('openid', None) if not openid_: return HttpResponseRedirect(reverse('user_signin') + next) nickname = openid_.sreg.get('nickname', '') email = openid_.sreg.get('email', '') form1 = OpenidRegisterForm(initial={ 'next': next, 'username': nickname, 'email': email, }) form2 = OpenidVerifyForm(initial={ 'next': next, 'username': nickname, }) if request.POST: just_completed = False if 'bnewaccount' in request.POST.keys(): form1 = OpenidRegisterForm(request.POST) if form1.is_valid(): next = form1.cleaned_data['next'] if not next: next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library')) is_redirect = True tmp_pwd = User.objects.make_random_password() user_ = User.objects.create_user(form1.cleaned_data['username'], form1.cleaned_data['email'], tmp_pwd) # Save a profile for them profile = UserPref(user=user_) profile.save() # make association with openid uassoc = UserAssociation(openid_url=str(openid_), user_id=user_.id) uassoc.save() # login user_.backend = "django.contrib.auth.backends.ModelBackend" login(request, user_) elif 'bverify' in request.POST.keys(): form2 = OpenidVerifyForm(request.POST) if form2.is_valid(): is_redirect = True next = form2.cleaned_data['next'] if not next: next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library')) user_ = form2.get_user() uassoc = UserAssociation(openid_url=str(openid_), user_id=user_.id) uassoc.save() login(request, user_) # redirect, can redirect only if forms are valid. if is_redirect: return HttpResponseRedirect(next) return render('authopenid/complete.html', { 'form1': form1, 'form2': form2, 'action': reverse('user_register'), 'nickname': nickname, 'email': email }, context_instance=RequestContext(request))