1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import unicode_literals |
3
|
|
|
""" |
4
|
|
|
Utilities for django-inspectional-registration |
5
|
|
|
""" |
6
|
|
|
__author__ = 'Alisue <[email protected]>' |
7
|
|
|
import random |
8
|
|
|
|
9
|
|
|
from django.utils.encoding import force_text |
10
|
|
|
from django.utils.six.moves import range |
11
|
|
|
from registration.compat import sha1 |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def get_site(request): |
15
|
|
|
"""get current ``django.contrib.Site`` instance |
16
|
|
|
|
17
|
|
|
return ``django.contrib.RequestSite`` instance when the ``Site`` is |
18
|
|
|
not installed. |
19
|
|
|
|
20
|
|
|
""" |
21
|
|
|
try: |
22
|
|
|
from django.contrib.sites.shortcuts import get_current_site |
23
|
|
|
except ImportError: |
24
|
|
|
from django.contrib.sites.models import get_current_site |
25
|
|
|
return get_current_site(request) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def generate_activation_key(username): |
29
|
|
|
"""generate activation key with username |
30
|
|
|
|
31
|
|
|
originally written by ubernostrum in django-registration_ |
32
|
|
|
|
33
|
|
|
.. _django-registration: https://bitbucket.org/ubernostrum/django-registration |
34
|
|
|
""" |
35
|
|
|
username = force_text(username) |
36
|
|
|
seed = force_text(random.random()) |
37
|
|
|
salt = sha1(seed.encode('utf-8')).hexdigest()[:5] |
38
|
|
|
activation_key = sha1((salt+username).encode('utf-8')).hexdigest() |
39
|
|
|
return activation_key |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def generate_random_password(length=10): |
43
|
|
|
"""generate random password with passed length""" |
44
|
|
|
# Without 1, l, O, 0 because those character are hard to tell |
45
|
|
|
# the difference between in same fonts |
46
|
|
|
chars = '23456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' |
47
|
|
|
password = "".join([random.choice(chars) for i in range(length)]) |
48
|
|
|
return password |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def send_mail(subject, message, from_email, recipients): |
52
|
|
|
"""send mail to recipients |
53
|
|
|
|
54
|
|
|
this method use django-mailer_ ``send_mail`` method when |
55
|
|
|
the app is in ``INSTALLED_APPS`` |
56
|
|
|
|
57
|
|
|
.. Note:: |
58
|
|
|
django-mailer_ ``send_mail`` is not used duaring unittest |
59
|
|
|
because it is a little bit difficult to check the number of |
60
|
|
|
mail sent in unittest for both django-mailer and original |
61
|
|
|
django ``send_mail`` |
62
|
|
|
|
63
|
|
|
.. _django-mailer: http://code.google.com/p/django-mailer/ |
64
|
|
|
""" |
65
|
|
|
from django.conf import settings |
66
|
|
|
from django.core.mail import send_mail as django_send_mail |
67
|
|
|
import sys |
68
|
|
|
if 'test' not in sys.argv and 'mailer' in settings.INSTALLED_APPS: |
69
|
|
|
try: |
70
|
|
|
from mailer import send_mail |
71
|
|
|
return send_mail(subject, message, from_email, recipients) |
72
|
|
|
except ImportError: |
73
|
|
|
pass |
74
|
|
|
return django_send_mail(subject, message, from_email, recipients) |
75
|
|
|
|