示例#1
0
def password_reset_confirm(request, confirm_code):
    '''
    контрол восстановления пароля
    принмает строку с хешем,
    при валидности отдаёт форму для сброса пароля
    '''
    template = 'accounts_password_change.html'
    data = dict()


    try:
        q_user = CustomUser.objects.get(activation_code=confirm_code)

        if request.method == 'POST':
            form = SetPasswordForm(q_user, request.POST)
            if form.is_valid():
                form.save()
                messages.add_message(request, messages.INFO, 'пароль изменён', fail_silently=True)
                template = 'accounts_password_change_done.html'
            else:
                form = SetPasswordForm(q_user, request.POST)
        else:
            form = SetPasswordForm(q_user)

        data['form'] = form

    except CustomUser.DoesNotExist:
        messages.add_message(request, messages.INFO, 'Что-то не так!', fail_silently=True)

    data['confirm_code'] = confirm_code


    t = loader.get_template(template)
    c = RequestContext(request,data, processors=[custom_proc])
    return HttpResponse(t.render(c))
示例#2
0
def activate(request, activation_key, template_name='user_creation/activate.html'):
    """
    Activates an account if the given key exists and if a password is given through the form.
    """
    activation_key = activation_key.lower() # Normalize before trying anything with it.
    if request.method == 'POST':
        form = SetPasswordForm('', request.POST)
        account = True
        if form.is_valid():
            account = ActivationProfile.objects.activate_user(form.cleaned_data['activation_key'])
            if account:
                form.user = account
                form.save(True)
                user = authenticate(username=account.username, password=form.cleaned_data['new_password1'])
                user.backend = settings.AUTHENTICATION_BACKENDS[0]
                login(request, user)
                messages.success(request, _("Your password has been set, \
                    please remember to remember it. You are now logged in."))
                return HttpResponseRedirect(getattr(settings, 'LOGIN_REDIRECT_URL','/'))
            else:
                form.error_messages={'error': _('Tampered activation key')}
    else:
        account = ActivationProfile.objects.check_key(activation_key)
        form = SetPasswordForm(account, initial={'activation_key':activation_key})
    return TemplateResponse(request, template_name,
                              { 'account': account,
                                'expiration_days': ACCOUNT_ACTIVATION_DAYS,
                                'set_password_form':form })