Example #1
0
def register(request, ticket = None, form_class = RegistrationFormUniqueEmail, profile_callback = None, template_name = 'accounts/register/register.html'):
    """ Allow a new user to register an account

    By default, 'registration.forms.RegistrationForm' will be used as the registration form;
    to change this, pass a different form class as the 'form_class' keyword argument. The form
    class you specify must have a method 'save' which will create and return the new 'User',
    and that method must accept the keyword argument 'profile_callback' (see below).

    To enable creation of a site-specific user profile object for the new user, pass a function
    which will create the profile object as the keyword argument 'profile_callback'. See
    'RegistrationManager.create_inactive_user' in the file 'models.py' for details on how to
    write this function.
    """

    # Sleep for a moment
    if settings.TICKET_CHECK_SLEEP is not None:
        sleep(settings.TICKET_CHECK_SLEEP)
    # Check the Registration Ticket
    if not RegistrationTicket.objects.check_ticket(ticket):
        return page_forbidden(request, message = gettext('You need a valid Registration Ticket to access the Registration page'))

    # Check the request
    if request.method == 'POST':
        # The user has submitted registration data
        form = form_class(request.POST)
        # Check the data validity
        if form.is_valid():
            # Create the user
            new_user = form.save(profile_callback = profile_callback)
            # Do not display the form again
            form = None
        else:
            # The data is not valid, new captcha
            #request.session['captcha'] = captcha.gen_captcha_key()
            #form.clean_data['captchadigest'] = captcha.captcha_digest(request.session['captcha'])
            #form.clean_data['captcha'] = ''
            pass
    else:
        # Display the registration form
        request.session['captcha'] = captcha.gen_captcha_key()
        form_class.base_fields['captchadigest'].initial = captcha.captcha_digest(request.session['captcha'])
        form = form_class(ticket = ticket)

    return render_to_response(template_name,
        {
            'form': form,
            'site': Site.objects.get_current(),
        },
        context_instance = RequestContext(request)
        )
Example #2
0
 def clean_captcha(self):
     """ Validate that the captcha digest and the digest of the captcha input match """
     if captcha_digest(self.clean_data['captcha']) == self.clean_data['captchadigest']:
         return self.clean_data['captcha']
     raise forms.ValidationError(str(_('You must type the text from the image below.')))