6485e82af9322d6c2b8ed1409d65bb00b8c20c64
[osm/LW-UI.git] / sf_user / models.py
1 #
2 # Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 from __future__ import unicode_literals
18
19 from django.contrib.auth.models import (
20 AbstractBaseUser, BaseUserManager, PermissionsMixin)
21 from django.utils import timezone
22 from django.utils.translation import ugettext_lazy as _
23 from django.contrib.sessions.base_session import AbstractBaseSession
24 from django.db import models
25
26 class CustomUserManager(BaseUserManager):
27 """Custom manager for CustomUser."""
28
29 def _create_user(self, username, password, is_staff, is_superuser, **extra_fields):
30 """Create and save a CustomUser with the given username and password. """
31 now = timezone.now()
32
33 if not username:
34 raise ValueError('The given username must be set')
35
36 is_active = extra_fields.pop("is_active", True)
37 user = self.model(username=username, is_staff=is_staff, is_active=is_active,
38 is_superuser=is_superuser, last_login=now,
39 date_joined=now, **extra_fields)
40 user.set_password(password)
41 user.save(using=self._db)
42 return user
43
44 """Create and save an CustomUser with the given username and password."""
45 def create_superuser(self, username, password, **extra_fields):
46 return self._create_user(username, password, True, True, is_admin=True,
47 **extra_fields)
48
49 """Create and save an FullOperator with the given email and password. """
50 def create_full_operator(self, username, password=None, **extra_fields):
51 return self._create_user(username, password, False, False, is_full_operator=True,
52 **extra_fields)
53
54 """Create and save an BasicUser with the given email and password. """
55 def create_basic_user(self, username, password=None, **extra_fields):
56 return self._create_user(username, password, False, False, is_basic_user=True,
57 **extra_fields)
58
59 """Create and save an GuestUser with the given email and password. """
60 def create_guest_user(self, username, password="guest", **extra_fields):
61 return self._create_user(username, password, False, False, is_guest_user=True,
62 **extra_fields)
63
64
65 class AbstractCustomUser(AbstractBaseUser, PermissionsMixin):
66 """Abstract User with the same behaviour as Django's default User.
67
68 AbstractCustomUser does not have username field. Uses email as the
69 USERNAME_FIELD for authentication.
70
71 Use this if you need to extend EmailUser.
72
73 Inherits from both the AbstractBaseUser and PermissionMixin.
74
75 The following attributes are inherited from the superclasses:
76 * password
77 * last_login
78 * is_superuser
79
80 """
81 username = models.CharField(_('username'), max_length=255, unique=True, db_index=True)
82
83 email = models.EmailField(_('email address'), max_length=255, unique=True)
84
85 first_name = models.CharField(_('first name'), max_length=255, blank=True)
86 last_name = models.CharField(_('last name'), max_length=255, blank=True)
87 phone = models.CharField(_('phone'), max_length=100, blank=True)
88 # user_groups = models.ManyToManyField(UserGroup, verbose_name=_('user groups'), blank=True)
89
90 is_admin = models.BooleanField(_('admin status'), default=False)
91 is_full_operator = models.BooleanField(_('full_operator status'), default=False)
92 is_basic_user = models.BooleanField(_('basic_user status'), default=False)
93 is_guest_user = models.BooleanField(_('guest_user status'), default=False)
94
95 is_staff = models.BooleanField(
96 _('staff status'), default=False, help_text=_(
97 'Designates whether the user can log into this admin site.'))
98 is_active = models.BooleanField(_('active'), default=True, help_text=_(
99 'Designates whether this user should be treated as '
100 'active. Unselect this instead of deleting accounts.'))
101
102 date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
103
104 objects = CustomUserManager()
105
106 USERNAME_FIELD = 'username'
107 REQUIRED_FIELDS = ['email', 'first_name', 'last_name', 'phone', ]
108
109 class Meta:
110 verbose_name = _('custom user')
111 verbose_name_plural = _('custom users')
112 abstract = True
113
114 def get_full_name(self):
115 """Return the fullname."""
116 return "%s %s" % (self.last_name, self.first_name)
117
118 def get_short_name(self):
119 """Return the firstname."""
120 return self.first_name
121
122
123 class CustomUser(AbstractCustomUser):
124 """
125 Concrete class of AbstractCustomUser.
126
127 Use this if you don't need to extend CustomUser.
128
129 """
130
131 class Meta(AbstractCustomUser.Meta):
132 swappable = 'AUTH_USER_MODEL'
133
134 def count_admins(self):
135 return CustomUser.objects.filter(is_admin=True).count()
136
137 def count_employee(self):
138 return CustomUser.objects.filter(is_full_operator=True).count()
139
140 def count_basic_users(self):
141 return CustomUser.objects.filter(is_basic_user=True).count()
142
143 def count_inactives(self):
144 return CustomUser.objects.filter(is_active=False).count()
145
146 def is_guest(self):
147 return self.is_guest_user
148
149 def get_avatar(self):
150 if self.is_admin:
151 return "assets/img/employer.png"
152 elif self.is_full_operator:
153 return "assets/img/employer.png"
154 elif self.is_basic_user:
155 return "assets/img/employer.png"
156 elif self.is_guest_user:
157 return "assets/img/account_circle.png"
158
159 def get_user_role(self):
160 if self.is_admin:
161 return 0, "Admin"
162 elif self.is_full_operator:
163 return 1, "Full operator"
164 elif self.is_basic_user:
165 return 2, "Basic user"
166 elif self.is_guest_user:
167 return 3, "Guest user"
168
169 def get_user_role_name(self):
170 if self.is_admin:
171 return "Admin"
172 elif self.is_full_operator:
173 return "Full operator"
174 elif self.is_basic_user:
175 return "Basic user"
176 elif self.is_guest_user:
177 return "Guest user"
178
179 def has_perm(self, perm, obj=None):
180 if perm == 'deploymenthandler':
181 if self.is_guest_user:
182 return False
183 else:
184 return True
185 else:
186 super.has_perm(perm, obj)
187
188
189 class CustomSession(AbstractBaseSession):
190 account_id = models.IntegerField(null=True, db_index=True)
191
192 @classmethod
193 def get_session_store_class(cls):
194 return SessionStore
195