def register(request): '''This view displays the registration page. **Templates** `"authentication/register/register.html"` Template of the page. **Context** ``form`` An :class:`authentication.forms.RegistrationForm`. `"authentication/register/email_*.txt"` Templates for subject and body of account activation emails. **Context** ``username`` Username. ``sitename`` Site name (set in admin). ``activation_key`` Activation key. ``expiry_date`` Activation key expiry date. `"authentication/register/email_*.html"` Templates of pages displayed after an account activation email was sent or if an error occured during it. **Context** ``next`` The link for previous page tracking or ``''`` if it is unknown (i.e., was not passed); ``error`` (only for `"authentication/register/email_error.html"` template) description of the error occured. ''' from registration.models import RegistrationProfile from registration.views import register as registration_register from forms import RegistrationForm form_class = RegistrationForm template_name = 'authentication/register/register.html' redirect_to = next = request.GET.get('next') if not redirect_to or '://' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL next = '' if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): user = form.save() # Send email activation_key = RegistrationProfile.objects.get(user=user).activation_key expiry_date = datetime.date.today() + datetime.timedelta( days=settings.ACCOUNT_ACTIVATION_DAYS-1) templatename = 'email_sent' context = {'next': next} try: send_mail(user, request, subject_template='authentication/register/email_subject.txt', message_template='authentication/register/email.txt', context={ 'username': user.username, 'sitename': Site.objects.get_current().name, 'activation_key': activation_key, 'expiry_date': expiry_date, }) except NoEmailException, e: # This must never occur raise e except SMTPException, e: templatename = 'email_error' context['error'] = unicode(e) templatename = 'authentication/register/' + templatename + '.html' return render_to_response2(request, templatename, context)
def login(request): '''Replacement for :meth:`django_authopenid.views.signin` written to supply the ``forget-password`` form to the template. This view displays the login page containing * OpenID login form; * traditional login form; * forget-password form; * a link to the registration page. **Templates** `"authentication/login/login.html"` Template of the login page. **Context** ``msg`` Form errors for ``form1`` (see below). ``form1`` An :class:`authentication.forms.OpenidLoginForm`. ``form2`` An :class:`django.contrib.auth.forms.AuthenticationForm`. ``form_forget_password`` An :class:`authentication.forms.RegisteredUserForm` to enter username. ``next`` The link for previous page tracking or ``""`` if it is unknown (i.e., was not passed). `"authentication/login/forget_password/email_*.txt"` Templates for subject and body of forget-password emails. **Context** ``username`` Username. ``sitename`` Site name (set in admin). ``activation_key`` Activation key. ``expiry_date`` Activation key expiry date. `"authentication/login/forget_password/email_*.html"` Templates of pages displayed after a login email was sent or if an error occured during it. **Context** ``next`` The link for previous page tracking or ``''`` if it is unknown (i.e., was not passed); ``error`` (only for `"authentication/login/forget_password/email_error.html"` template) description of the error occured. ''' from django.contrib.auth.forms import AuthenticationForm from forms import OpenidLoginForm, RegisteredUserForm template_name = 'authentication/login/login.html' openid_form = OpenidLoginForm auth_form = AuthenticationForm redirect_to = next = request.GET.get('next') if not redirect_to or '://' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL next = '' form1_msg = '' form1 = openid_form() form2 = auth_form() form_forget_password = RegisteredUserForm() if request.POST: if 'openid_url' in request.POST.keys(): form1 = openid_form(data=request.POST) if form1.is_valid(): openid_url = form1.cleaned_data['openid_url'] # # Unfortunately, Django does not provide a way to assign form # errors here. That's why we have to pass errors from OpenID # processing to separate ``msg`` template context variable. # # Another solution would be to move the calls below # to the ``clean`` method of the class ``form1`` belongs to. # The calls are not related to form processing at all, so # this solution is even more ugly. Also, it would require # much of monkey patching inside :mod:`django_authopenid`. # redirect_url = "%s?%s" % ( # # Here we send ``openid_url`` GET parameter in order # to be able to restore ``openid_url`` form field value # if an error occurs and we'll render the same forms again. # # As we pass URL of this page to OpenID provider as # ``complete_url`` (see ``ask_openid`` call below), we will # get in this view function again after the user confirms his # identity on his OpenID provider's page. In order to know # whether to check GET parameters the provider sent back to us, # we must know whether we get to this view function at user's # will or from an OpenID provider. This is the second purpose # of the ``openid_url`` GET parameter: if it is present, # we got from a provider. # request.build_absolute_uri(request.path), urllib.urlencode({'next': redirect_to, 'openid_url': openid_url}) ) try: return Overrides.Authopenid.ask_openid(request, openid_url, redirect_url) except Overrides.Authopenid.OpenidError, e: form1_msg = unicode(e) elif 'password' in request.POST.keys(): # perform normal Django authentification form2 = auth_form(data=request.POST) if form2.is_valid(): auth.login(request, form2.get_user()) if request.session.test_cookie_worked(): request.session.delete_test_cookie() return HttpResponseRedirect(redirect_to) else: # forget password form_forget_password = RegisteredUserForm(request.POST) if form_forget_password.is_valid(): user = form_forget_password.user() email = LoginEmail.objects.create_email(user) templatename = 'email_sent' context = {'next': next} try: send_mail(user, request, subject_template='authentication/login/forget_password/email_subject.txt', message_template='authentication/login/forget_password/email.txt', context=email.prepare_send()) except NoEmailException: templatename = 'email_none' except SMTPException, e: templatename = 'email_error' context['error'] = unicode(e) templatename = 'authentication/login/forget_password/' + templatename + '.html' return render_to_response2(request, templatename, context)