|
1 |
|
2 from django.contrib.auth.models import ( |
|
3 BaseUserManager, AbstractBaseUser |
|
4 ) |
|
5 from django.db import models |
|
6 from django.utils.translation import ugettext_lazy as _ |
|
7 |
|
8 from django.conf import settings |
|
9 |
|
10 |
|
11 class AmmicoUserManager(BaseUserManager): |
|
12 def create_user(self, email, idUser=None, password=None): |
|
13 """ |
|
14 Creates and saves a User with the given email and password. |
|
15 """ |
|
16 if not email: |
|
17 raise ValueError('Users must have an email address') |
|
18 |
|
19 user = self.model( |
|
20 email=self.normalize_email(email), |
|
21 idUser=idUser, |
|
22 ) |
|
23 |
|
24 user.set_password(password) |
|
25 user.save(using=self._db) |
|
26 return user |
|
27 |
|
28 def create_superuser(self, email, password, idUser=None): |
|
29 """ |
|
30 Creates and saves a superuser with the given email and password. |
|
31 """ |
|
32 user = self.create_user(email, |
|
33 password=password, |
|
34 idUser=idUser |
|
35 ) |
|
36 user.is_admin = True |
|
37 user.save(using=self._db) |
|
38 return user |
|
39 |
|
40 |
|
41 class AmmicoUser(AbstractBaseUser): |
|
42 email = models.EmailField(verbose_name='email address', max_length=255, unique=True) |
|
43 idUser = models.IntegerField(_('identifiant utilisateur'), blank=True, null=True) |
|
44 is_active = models.BooleanField(default=True) |
|
45 is_admin = models.BooleanField(default=False) |
|
46 |
|
47 objects = AmmicoUserManager() |
|
48 |
|
49 USERNAME_FIELD = 'email' |
|
50 REQUIRED_FIELDS = [] |
|
51 |
|
52 def get_full_name(self): |
|
53 # The user is identified by their email address |
|
54 return self.email |
|
55 |
|
56 def get_short_name(self): |
|
57 # The user is identified by their email address |
|
58 return self.email |
|
59 |
|
60 def __str__(self): # __unicode__ on Python 2 |
|
61 return self.email |
|
62 |
|
63 def has_perm(self, perm, obj=None): |
|
64 "Does the user have a specific permission?" |
|
65 # Simplest possible answer: Yes, always |
|
66 return True |
|
67 |
|
68 def has_module_perms(self, app_label): |
|
69 "Does the user have permissions to view the app `app_label`?" |
|
70 # Simplest possible answer: Yes, always |
|
71 return True |
|
72 |
|
73 @property |
|
74 def is_staff(self): |
|
75 "Is the user a member of staff?" |
|
76 # Simplest possible answer: All admins are staff |
|
77 return self.is_admin |
|
78 |
|
79 class Profile(models.Model): |
|
80 user = models.OneToOneField(settings.AUTH_USER_MODEL) |
|
81 image = models.URLField(max_length=2048, blank=True) |