Login

Reset / Send account details email

Author:
Ciantic
Posted:
August 4, 2010
Language:
Python
Version:
1.2
Tags:
users reset-password
Score:
0 (after 0 ratings)

I needed to create a feature for administrators to create accounts and email the new account was created email using a button. Remember to change the default template and subject to match your needs.

The code is directly taken from django.contrib.auth.forms.PasswordResetForm.save().

Notice that the function raises ValueError if the user is missing an email address.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template import loader
from django.template.context import Context
from django.utils.http import int_to_base36
from django.utils.translation import ugettext_lazy as _

def reset(user, domain_override=None, email_template_name='registration/password_reset_email.html',
             use_https=False, token_generator=default_token_generator):
    """Reset users password"""
    if not user.email:
        raise ValueError('Email address is required to send an email')

    if not domain_override:
        current_site = Site.objects.get_current()
        site_name = current_site.name
        domain = current_site.domain
    else:
        site_name = domain = domain_override
    t = loader.get_template(email_template_name)
    c = {
        'email': user.email,
        'domain': domain,
        'site_name': site_name,
        'uid': int_to_base36(user.id),
        'user': user,
        'token': token_generator.make_token(user),
        'protocol': use_https and 'https' or 'http',
    }
    send_mail(_("Your account for %s") % site_name,
              t.render(Context(c)), None, [user.email])

More like this

  1. User activation codes without additional database tables or fields by badzong 2 years, 8 months ago
  2. Using manager methods by ubernostrum 8 years, 4 months ago
  3. Restrict Middleware by clint 7 years ago
  4. Simple E-mail registration by bthomas 6 years, 7 months ago
  5. Modify fields created by form_for_model by grahamu 8 years, 4 months ago

Comments

Please login first before commenting.