1 | (testenv)kondou:project/ (master) $ cat project/models/user_model.py
|
---|
2 | from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, Group, PermissionsMixin
|
---|
3 | from django.core import mail, validators
|
---|
4 | from django.conf import settings
|
---|
5 | from django.core.urlresolvers import reverse
|
---|
6 | from django.db import models
|
---|
7 | from django.utils import timezone, translation
|
---|
8 | from django.utils.translation import ugettext_lazy
|
---|
9 | import re
|
---|
10 |
|
---|
11 | from project.dbutilities import date_in_one_month
|
---|
12 |
|
---|
13 |
|
---|
14 | class UserManager(BaseUserManager):
|
---|
15 | def create_user(self, username, email, password=None, to_activate=False, languagecode="en-us"):
|
---|
16 | pass
|
---|
17 |
|
---|
18 | def create_superuser(self, username, email, password):
|
---|
19 | pass
|
---|
20 |
|
---|
21 | class User(PermissionsMixin, AbstractBaseUser):
|
---|
22 | class Meta:
|
---|
23 | get_latest_by = "date_joined"
|
---|
24 |
|
---|
25 | username = models.CharField(
|
---|
26 | ugettext_lazy('username'), max_length=30, unique=True,
|
---|
27 | help_text=ugettext_lazy('30 characters or less. Letters, numbers and ./+/-/_ characters are allowed'),
|
---|
28 | validators=[
|
---|
29 | validators.RegexValidator(re.compile('^[\w.+-]+$'), ugettext_lazy('Invalid username.'), 'invalid')
|
---|
30 | ]
|
---|
31 | )
|
---|
32 | displayname = models.CharField(max_length=150, blank=True)
|
---|
33 | email = models.EmailField()
|
---|
34 | email_bounced = models.BooleanField(
|
---|
35 | default=False,
|
---|
36 | help_text="Whether a mail to this email bounced, and no more mail should be sent to this"
|
---|
37 | )
|
---|
38 | email_complained = models.BooleanField(
|
---|
39 | default=False,
|
---|
40 | help_text="Whether this email complained, meaning no mail, other than pwreset mails should be sent"
|
---|
41 | )
|
---|
42 | banned = models.BooleanField(default=False)
|
---|
43 | active = models.BooleanField(default=True)
|
---|
44 | date_joined = models.DateField(auto_now_add=True)
|
---|
45 | lastpwchange = models.DateTimeField(auto_now_add=True)
|
---|
46 | follows = models.ManyToManyField('self', symmetrical=False, related_name="following")
|
---|
47 | ignores = models.ManyToManyField(
|
---|
48 | 'self', symmetrical=False, related_name="ignoring",
|
---|
49 | help_text=ugettext_lazy(
|
---|
50 | "Ignored means you don't get messages anymore from this user, but for them their messages seem to arrive."
|
---|
51 | )
|
---|
52 | )
|
---|
53 | blocks = models.ManyToManyField(
|
---|
54 | 'self', symmetrical=False, related_name="blocking",
|
---|
55 | help_text=ugettext_lazy(
|
---|
56 | "Blocked means you don't get messages anymore from this user and they will see a \"You're blocked\" "
|
---|
57 | "message on trying to message you. They also can't comment on your gallerys either."
|
---|
58 | )
|
---|
59 | )
|
---|
60 | monetization_enabled = models.BooleanField(default=False)
|
---|
61 | monetization_in_queue = models.BooleanField(default=False)
|
---|
62 | countrycode = models.CharField(max_length=10, blank=True)
|
---|
63 | languagecode = models.CharField(max_length=10, blank=True)
|
---|
64 | payment_credit = models.FloatField(default=0)
|
---|
65 | avatar = models.CharField(max_length=10, unique=True, blank=True, null=True)
|
---|
66 | default_moderators = models.ManyToManyField('self', related_name="default_moderators")
|
---|
67 | uploader = models.BooleanField(default=False)
|
---|
68 | unread_messages = models.IntegerField(default=0)
|
---|
69 |
|
---|
70 | USERNAME_FIELD = 'username'
|
---|
71 | REQUIRED_FIELDS = ['email']
|
---|
72 | objects = UserManager()
|
---|
73 |
|
---|
74 | # Required methods
|
---|
75 |
|
---|
76 | def get_full_name(self):
|
---|
77 | if self.displayname != '':
|
---|
78 | return self.displayname
|
---|
79 | else:
|
---|
80 | return self.username
|
---|
81 |
|
---|
82 | def get_short_name(self):
|
---|
83 | return self.username
|
---|
84 |
|
---|
85 | def is_active(self):
|
---|
86 | return self.active
|
---|
87 |
|
---|
88 | # -- Own methods --
|
---|
89 |
|
---|
90 | # Administrative
|
---|
91 |
|
---|
92 | def is_banned(self):
|
---|
93 | return self.banned
|
---|
94 |
|
---|
95 | def set_password(self, raw_password):
|
---|
96 | self.lastpwchange = timezone.now()
|
---|
97 | super().set_password(raw_password)
|
---|
98 |
|
---|
99 | def set_countrycode(self, code):
|
---|
100 | self.countrycode = code
|
---|
101 | self.save()
|
---|
102 |
|
---|
103 | def set_languagecode(self, code):
|
---|
104 | self.languagecode = code
|
---|
105 | self.save()
|
---|
106 |
|
---|
107 | # Other
|
---|
108 |
|
---|
109 | def get_url(self):
|
---|
110 | return reverse('userview', kwargs={'username': self.username})
|
---|
111 |
|
---|
112 |
|
---|
113 | class AccountActivation(models.Model):
|
---|
114 | user = models.OneToOneField(User, primary_key=True)
|
---|
115 | token = models.CharField(
|
---|
116 | max_length=10, unique=True,
|
---|
117 | validators=[validators.RegexValidator(
|
---|
118 | re.compile('^[abcdefghijklmnopqrstuvwxyz\d]{10}$'), 'Invalid key', 'invalid'
|
---|
119 | )]
|
---|
120 | )
|
---|
121 | used = models.BooleanField(default=False)
|
---|
122 | expiry = models.DateField(default=date_in_one_month)
|
---|