This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
models.py
146 lines (116 loc) · 5.25 KB
/
models.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from datetime import datetime
import hashlib
import random
import re
import string
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User as DjangoUser
from django.db import models
import amo
import amo.models
from translations.fields import PurifiedField
def get_hexdigest(algorithm, salt, raw_password):
return hashlib.new(algorithm, salt + raw_password).hexdigest()
def rand_string(length):
return ''.join(random.choice(string.letters) for i in xrange(length))
def create_password(algorithm, raw_password):
salt = get_hexdigest(algorithm, rand_string(12), rand_string(12))[:64]
hsh = get_hexdigest(algorithm, salt, raw_password)
return '$'.join([algorithm, salt, hsh])
class UserProfile(amo.models.ModelBase):
nickname = models.CharField(max_length=255, unique=True, default='')
firstname = models.CharField(max_length=255, default='')
lastname = models.CharField(max_length=255, default='')
password = models.CharField(max_length=255, default='')
email = models.EmailField(unique=True)
averagerating = models.CharField(max_length=255, blank=True)
bio = PurifiedField()
confirmationcode = models.CharField(max_length=255, default='',
blank=True)
deleted = models.BooleanField(default=True)
display_collections = models.BooleanField(default=False)
display_collections_fav = models.BooleanField(default=False)
emailhidden = models.BooleanField(default=False)
homepage = models.CharField(max_length=765, blank=True, default='')
location = models.CharField(max_length=765, blank=True, default='')
notes = models.TextField(blank=True)
notifycompat = models.BooleanField(default=True)
notifyevents = models.BooleanField(default=True)
occupation = models.CharField(max_length=765, default='', blank=True)
picture_type = models.CharField(max_length=75, default='', blank=True)
resetcode = models.CharField(max_length=255, default='', blank=True)
resetcode_expires = models.DateTimeField(default=datetime.now,
blank=True)
sandboxshown = models.BooleanField(default=False)
user = models.ForeignKey(DjangoUser, null=True, editable=False, blank=True)
class Meta:
db_table = 'users'
def __unicode__(self):
return '%s: %s' % (self.id, self.display_name)
def get_absolute_url(self):
return reverse('users.profile', args=[self.id])
@amo.cached_property
def addons_listed(self):
"""public add-ons this user is listed as author of"""
return self.addons.valid().filter(addonuser__listed=True)
@property
def picture_url(self):
# TODO this used to be /user/1234/picture, and the regex stuff was
# in htaccess. Should we let the web server take care of it again?
split_id = re.match(r'((\d*?)(\d{0,3}?))\d{1,3}$', str(self.id))
return (settings.MEDIA_URL + 'img/uploads/userpics/%s/%s/%s.jpg' % (
split_id.group(2) or 0, split_id.group(1) or 0, self.id))
@amo.cached_property
def is_developer(self):
return bool(self.addons.filter(authors=self,
addonuser__listed=True)[:1])
@property
def display_name(self):
if not self.nickname:
return '%s %s' % (self.firstname, self.lastname)
else:
return self.nickname
@property
def welcome_name(self):
if self.firstname:
return self.firstname
elif self.nickname:
return self.nickname
elif self.lastname:
return self.lastname
return ''
def save(self, force_insert=False, force_update=False, using=None):
# we have to fix stupid things that we defined poorly in remora
if self.resetcode_expires is None:
self.resetcode_expires = datetime.now()
super(UserProfile, self).save(force_insert, force_update, using)
def check_password(self, raw_password):
if '$' not in self.password:
valid = (get_hexdigest('md5', '', raw_password) == self.password)
if valid:
# Upgrade an old password.
self.set_password(raw_password)
self.save()
return valid
algo, salt, hsh = self.password.split('$')
return hsh == get_hexdigest(algo, salt, raw_password)
def set_password(self, raw_password, algorithm='sha512'):
self.password = create_password(algorithm, raw_password)
def create_django_user(self):
"""Make a django.contrib.auth.User for this UserProfile."""
# Reusing the id will make our life easier, because we can use the
# OneToOneField as pk for Profile linked back to the auth.user
# in the future.
self.user = DjangoUser(id=self.pk)
self.user.first_name = self.firstname
self.user.last_name = self.lastname
self.user.username = self.email
self.user.email = self.email
self.user.password = self.password
self.user.date_joined = self.created
if self.group_set.filter(rules='*:*').count():
self.user.is_superuser = self.user.is_staff = True
self.user.save()
self.save()
return self.user