Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 104 lines (88 sloc) 3.617 kB
a72c4ca @alexkay Email activation
authored
1 import random
2
b14d6ea @alexkay Sign up / sign in /sign out
authored
3 from django.contrib.auth.models import User
a72c4ca @alexkay Email activation
authored
4 from django.core.mail import EmailMultiAlternatives
c0ebc69 @alexkay Add an empty app
authored
5 from django.db import models
6bbd228 @alexkay PRAGMA foreign_keys=1
authored
6 from django.db.backends.signals import connection_created
47c91b7 @alexkay Add UserProfile
authored
7 from django.db.models.signals import post_save
6bbd228 @alexkay PRAGMA foreign_keys=1
authored
8 from django.dispatch import receiver
a72c4ca @alexkay Email activation
authored
9 from django.template.loader import render_to_string
6bbd228 @alexkay PRAGMA foreign_keys=1
authored
10
11 @receiver(connection_created)
12 def activate_foreign_keys(sender, connection, **kwargs):
13 if connection.vendor == 'sqlite':
14 cursor = connection.cursor()
15 cursor.execute('PRAGMA foreign_keys=1;')
c0ebc69 @alexkay Add an empty app
authored
16
a72c4ca @alexkay Email activation
authored
17 @receiver(post_save, sender=User)
18 def user_post_save(sender, instance, created, **kwargs):
19 if created:
20 p = UserProfile()
21 p.user = instance
22 p.save()
23
b14d6ea @alexkay Sign up / sign in /sign out
authored
24 User.__unicode__ = lambda x: x.email
47c91b7 @alexkay Add UserProfile
authored
25
26 class UserProfile(models.Model):
27
a72c4ca @alexkay Email activation
authored
28 code_length = 16
29
47c91b7 @alexkay Add UserProfile
authored
30 user = models.OneToOneField(User)
31
32 notify = models.BooleanField(default=True)
33 notify_album = models.BooleanField(default=True)
34 notify_single = models.BooleanField(default=True)
35 notify_ep = models.BooleanField(default=True)
36 notify_live = models.BooleanField(default=True)
37 notify_compilation = models.BooleanField(default=True)
38 notify_remix = models.BooleanField(default=True)
39 notify_other = models.BooleanField(default=True)
40 email_activated = models.BooleanField(default=False)
a72c4ca @alexkay Email activation
authored
41 activation_code = models.CharField(max_length=code_length)
42 reset_code = models.CharField(max_length=code_length)
47c91b7 @alexkay Add UserProfile
authored
43
a72c4ca @alexkay Email activation
authored
44 def generate_code(self):
45 code_chars = '23456789abcdefghijkmnpqrstuvwxyz'
46 return ''.join(random.choice(code_chars) for i in xrange(UserProfile.code_length))
47
48 def send_email(self, subject, text_template, html_template, **kwds):
49 sender = 'info@muspy.com'
50 text = render_to_string(text_template, kwds)
51 msg = EmailMultiAlternatives(subject, text, sender, [self.user.email])
52 if html_template:
53 html = render_to_string(html_template, kwds)
54 msg.attach_alternative(html_content, "text/html")
55 msg.send()
56
57 def send_activation_email(self):
58 code = self.generate_code()
59 self.activation_code = code
60 self.save()
61 self.send_email(subject='Email Activation',
62 text_template='email/activate.txt',
63 html_template=None,
64 code=code)
65
66 def send_reset_email(self):
67 code = self.generate_code()
68 self.reset_code = code
69 self.save()
70 self.send_email(subject='Password Reset Confirmation',
71 text_template='email/reset.txt',
72 html_template=None,
73 code=code)
74
75 @classmethod
76 def activate(cls, code):
77 profiles = UserProfile.objects.filter(activation_code__exact=code)
78 if not profiles:
79 return False
80 profile = profiles[0]
81 profile.activation_code = ''
82 profile.email_activated = True
83 profile.save()
84 return True
85
86 @classmethod
87 def reset(cls, code):
e4e6368 @alexkay Password reset fixes
authored
88 profiles = UserProfile.objects.filter(reset_code__exact=code)
89 if not profiles:
a72c4ca @alexkay Email activation
authored
90 return None, None
e4e6368 @alexkay Password reset fixes
authored
91 profile = profiles[0]
92 password = User.objects.make_random_password(length=16)
a72c4ca @alexkay Email activation
authored
93 profile.reset_code = ''
94 profile.user.set_password(password)
95 # TODO: transaction
96 profile.user.save()
97 profile.save()
98 return profile.user.email, password
99
100 @classmethod
101 def find(cls, email):
102 users = User.objects.filter(email__exact=email)
103 return users[0].get_profile() if users else None
Something went wrong with that request. Please try again.