Esempio n. 1
0
def forgotten_password(request, template="users/forgotten_password.html"):
    if request.method == "POST":
        form = ForgottenPasswordForm(request.POST)
        extra_context = {"form": form}

        if form.is_valid():
            email = form.cleaned_data["email"]
            # getting user who has that e-mail address
            if User.objects.filter(email=email).__len__() > 0:
                site = Site.objects.get_current()
                user = User.objects.get(email=email)
                # changing the user's password
                password = Captcha.generate_random_text()
                user.set_password(password)
                user.save()
                # mailing the new password to the user
                send_mail(
                    settings.EMAIL_SUBJECT_PREFIX + messages.FORGOTTEN_PASSWORD_SUBJECT,
                    messages.FORGOTTEN_PASSWORD_BODY % (site.name, site.name, password),
                    settings.EMAIL_INFO_ADDRESS_TR,
                    [user.email],
                    fail_silently=settings.EMAIL_FAIL_SILENCE,
                    auth_user=settings.EMAIL_HOST_USER,
                    auth_password=settings.EMAIL_HOST_PASSWORD,
                )
                extra_context["status"] = True
            else:
                # can't find such a user
                extra_context["status"] = False
                extra_context["error"] = messages.FRG_CANNOT_FIND_EMAIL
    else:
        form = ForgottenPasswordForm()
        extra_context = {"form": form}

    return render_to_response(template, extra_context, context_instance=RequestContext(request))
Esempio n. 2
0
def register(request, template="users/registration.html"):
    # FIXME: the following code is too long.. shorten its logic..

    # creating a captcha image
    captcha = create_captcha()

    if request.method == "POST":
        # creating a login form instance with post data
        form = RegistrationForm(request.POST)
        extra_context = {"form": form, "captcha": captcha}
        if form.is_valid():
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password"]
            name = form.cleaned_data["name"]
            surname = form.cleaned_data["surname"]
            email = form.cleaned_data["email"]
            if is_username_unique(username) and is_email_unique(email):
                # last, control the captcha and continue if it's correct
                test = lambda x={}, y="": (x.has_key(y) and x[y] != "") or False
                if test(request.POST, "captcha_id") and test(request.POST, "registration_captcha"):
                    cp = Captcha()
                    cp.text = request.POST["registration_captcha"]
                    # validating captcha
                    pattern = re.compile(u"[^a-zA-Z0-9]")
                    if (
                        not pattern.search(cp.text)
                        and cp.generate_hash(settings.SECRET_KEY[:20]) == request.POST["captcha_id"]
                    ):
                        # registering the user
                        try:
                            new_user = User.objects.create_user(username, email, password)
                            new_user.first_name = name
                            new_user.last_name = surname
                            # new user will be passive 'til activation occurs
                            new_user.is_active = False
                            new_user.save()
                            # creating a user profile with an activation key
                            new_user.profile.create()
                            # creating activation key
                            profile = new_user.get_profile()
                            profile.activation_key = create_activation_key()
                            profile.save()
                            # all done, now let's send him a mail for activation
                            site = Site.objects.get_current()
                            send_mail(
                                settings.EMAIL_SUBJECT_PREFIX + messages.REGISTER_SUBJECT,
                                messages.REGISTER_BODY
                                % (site.name, site.domain, new_user.username, profile.activation_key, site.name),
                                settings.EMAIL_INFO_ADDRESS_TR,
                                [email],
                                fail_silently=settings.EMAIL_FAIL_SILENCE,
                                auth_user=settings.EMAIL_HOST_USER,
                                auth_password=settings.EMAIL_HOST_PASSWORD,
                            )
                            # leaving the "anonymous" user a new message..
                            messages.set_user_message(request, messages.REG_SUCCESS)
                            # redirecting the user to blog
                            return HttpResponseRedirect(reverse(settings.REDIRECT_URL))
                        except IntegrityError:
                            # there's already a user with that name
                            # normally this part shouldn't be invoked
                            extra_context["error"] = messages.ALREADY_REGISTERED_USER
                            return render_to_response(template, extra_context, context_instance=RequestContext(request))
                    else:
                        extra_context["error"] = messages.WRONG_CAPTCHA
                        return render_to_response(template, extra_context, context_instance=RequestContext(request))
                else:
                    extra_context["error"] = messages.WRONG_CAPTCHA
                    return render_to_response(template, extra_context, context_instance=RequestContext(request))
            else:
                extra_context["error"] = messages.ALREADY_REGISTERED_USER
                return render_to_response(template, extra_context, context_instance=RequestContext(request))
        else:
            return render_to_response(template, extra_context, context_instance=RequestContext(request))
    else:
        # creating a login form instance
        form = RegistrationForm()
        extra_context = {"form": form, "captcha": captcha}
        return render_to_response(template, extra_context, context_instance=RequestContext(request))