Example #1
0
def register(request):
    """Multi-purpose registration view.

    Uses: legacy email url with invite code, first time edit profile
    after browserid_login. Process POST and create new users.
    """
    # Legacy URL shenanigans - A GET to register with invite code
    # is a legal way to start the BrowserID registration flow.
    if 'code' in request.GET:
        request.session['invite-code'] = request.GET['code']
        return redirect('home')

    if request.user.is_authenticated():
        return redirect(reverse('profile', args=[request.user.unique_id]))

    if not 'verified_email' in request.session:
        log.error('Browserid registration, but no verified email in session')
        return redirect('home')

    email = request.session['verified_email']

    intent = 'register'

    # Check for optional invite code
    initial = {}
    if 'invite-code' in request.session:
        code = request.session['invite-code']
        try:
            invite = get_invite(code)
            initial['email'] = invite.recipient
            initial['code'] = invite.code
        except Invite.DoesNotExist:
            log.warning('Bad register code [%s], skipping invite' % code)

    form = forms.RegistrationForm(request.POST or None, initial=initial)

    if request.method == 'POST':
        if form.is_valid():
            try:
                uniq_id = _save_new_user(request, form)
                messages.info(request, _(u'Your account has been created.'))
                return redirect('profile', uniq_id)
            except ldap.CONSTRAINT_VIOLATION:
                log.error("User already exists")
                _set_already_exists_error(form)
    else:
        if 'link' in request.GET:
            intent = request.GET['link']
    anonymous = Anonymous()

    return render(
        request, 'phonebook/edit_profile.html',
        dict(form=form,
             edit_form_action=reverse('register'),
             person=anonymous,
             mode='new',
             email=email,
             intent=intent))
Example #2
0
def register(request):
    """Registers Users.

    Pulls out an invite code if it exists and auto validates the user
    if so. Single-purpose view.
    """
    COUNTRIES = zip(
        product_details.get_regions('en-US').values(),
        product_details.get_regions(request.locale).values())
    COUNTRIES = sorted(COUNTRIES, key=lambda country: country[1])
    COUNTRIES.insert(0, ('', '----'))

    if 'code' in request.GET:
        request.session['invite-code'] = request.GET['code']
        return redirect('home')

    if request.user.is_authenticated():
        return redirect(reverse('profile', args=[request.user.username]))

    authenticated_email = request.session.get('authenticated_email')
    if not authenticated_email:
        log.error('Browserid registration, but no verified email in session')
        return redirect('home')

    user = auth.authenticate(authenticated_email=authenticated_email)
    if not user:
        return redirect('home')

    form = forms.RegistrationForm(request.POST or None,
                                  instance=user.get_profile())

    form.fields['country'].choices = COUNTRIES

    if request.method == 'POST':
        if form.is_valid():
            form.save(user)
            userProfile = user.get_profile()
            for group in request.POST.getlist('groups'):
                userProfile.groups.add(group)
            userProfile.save()
            auth.login(request, user)
            _update_invites(request)
            messages.info(request, _(u'Your account has been created.'))
            return redirect(reverse('profile', args=[request.user.username]))

    # 'user' object must be passed in because we are not logged in
    return render(
        request, 'registration/register.html',
        dict(
            form=form,
            edit_form_action=reverse('register'),
            mode='new',
            profile=user.get_profile(),
            user=user,
        ))
Example #3
0
def register(request):
    """
    Single-purpose view.
    Registers Users. Pulls out an invite code if it exists and auto validates
    the user if so.
    """
    if 'code' in request.GET:
        request.session['invite-code'] = request.GET['code']
        return redirect('home')

    if request.user.is_authenticated():
        return redirect(reverse('profile', args=[request.user.username]))

    authenticated_email = request.session.get('authenticated_email')
    if not authenticated_email:
        log.error('Browserid registration, but no verified email in session')
        return redirect('home')

    user = auth.authenticate(authenticated_email=authenticated_email)
    if not user:
        return redirect('home')

    form = forms.RegistrationForm(request.POST or None,
                                  instance=user.get_profile())

    if request.method == 'POST':
        if form.is_valid():
            form.save(user)
            auth.login(request, user)
            _update_invites(request)
            messages.info(request, _(u'Your account has been created.'))
            return redirect('profile', user.username)

    # When changing this keep in mind that the same view is used for
    # phonebook.edit_profile.
    # 'user' object must be passed in because we are not logged in
    return render(
        request, 'phonebook/edit_profile.html',
        dict(
            form=form,
            edit_form_action=reverse('register'),
            mode='new',
            profile=user.get_profile(),
            user=user,
        ))