Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-registration - some activation

Tags:

python

django

How can I force resending activation e-mail to user? E.g. when he accidentaly deletes the mail, he clicks in link in my website, and django will send him new activation e-mail.

like image 361
DJpython Avatar asked Dec 28 '09 16:12

DJpython


2 Answers

There's an admin action for doing that. From the django-registration docs:

How do I re-send an activation email?

Assuming you’re using the default backend, a custom admin action is provided for this; in the admin for the RegistrationProfile model, simply click the checkbox for the user(s) you’d like to re-send the email for, then select the “Re-send activation emails” action.

There's no built-in automatic way of doing this (how should django-registration figure out that a random visitor is that guy who filled out the registration form 3 days ago and deleted his activation mail?).

like image 122
Benjamin Wohlwend Avatar answered Oct 06 '22 00:10

Benjamin Wohlwend


The following view function will render a form with single email field, then check if there are any users with this email not activated yet, recreate activation code if expired (hashing stuff was copypasted from RegistrationProfile) and finally send activation email.

class ResendActivationEmailForm(forms.Form):
    email = EmailField(required=True)

def resend_activation_email(request):
    context = Context()

    form = None
    if request.method == 'POST':
        form = ResendActivationEmailForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            users = User.objects.filter(email=email, is_active=0)

            if not users.count():
                form._errors["email"] = (_("Account for email address is not registered or already activated."),)

            for user in users:
                for profile in RegistrationProfile.objects.filter(user=user):
                    if profile.activation_key_expired():
                        salt = sha_constructor(str(random())).hexdigest()[:5]
                        profile.activation_key = sha_constructor(salt+user.username).hexdigest()
                        user.date_joined = datetime.now()
                        user.save()
                        profile.save()

                    if Site._meta.installed:
                        site = Site.objects.get_current()
                    else:
                        site = RequestSite(request)

                    profile.send_activation_email(site)

                    context.update({"form" : form})
                    return render_to_response("registration/resend_activation_email_done.html", context)

    if not form:
        form = ResendActivationEmailForm()

    context.update({"form" : form})
    return render_to_response("registration/resend_activation_email_form.html", context)
like image 21
ilya b. Avatar answered Oct 05 '22 22:10

ilya b.