c48f7382ceba1e2cc600090444e3e04c23484ce8
[osm/LW-UI.git] / authosm / models.py
1 #
2 # Copyright 2018 EveryUP Srl
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 from __future__ import unicode_literals
17 from django.db import models
18 from django.utils.translation import ugettext_lazy as _
19 from django.contrib.auth.models import User, AbstractBaseUser, BaseUserManager, PermissionsMixin
20 from django.utils import timezone
21 from lib.osm.osmclient.client import Client
22 import uuid
23
24
25 class OsmUserManager(BaseUserManager):
26 """Custom manager for OsmUser."""
27
28 def _create_user(self, username, password, is_staff, is_superuser, **extra_fields):
29 """Create and save a CustomUser with the given username and password. """
30 now = timezone.now()
31
32 if not username:
33 raise ValueError('The given username must be set')
34
35 is_active = extra_fields.pop("is_active", True)
36 user = self.model(username=username, is_staff=is_staff, is_active=is_active,
37 is_superuser=is_superuser, last_login=now,
38 date_joined=now, **extra_fields)
39 user.set_password(password)
40 user.save(using=self._db)
41 return user
42
43 """Create and save an OsmUser with the given username and password."""
44 def create_superuser(self, username, password, **extra_fields):
45 return self._create_user(username, password, True, True, is_admin=True,
46 **extra_fields)
47
48
49 class AbstractOsmUser(AbstractBaseUser, PermissionsMixin):
50 """Abstract User with the same behaviour as Django's default User.
51
52
53 Inherits from both the AbstractBaseUser and PermissionMixin.
54
55 The following attributes are inherited from the superclasses:
56 * password
57 * last_login
58 * is_superuser
59
60 """
61 username = models.CharField(_('username'), max_length=255, unique=True, db_index=True)
62 current_project = models.CharField(_('project_id'), max_length=255)
63 token_project = models.CharField(_('token'), max_length=36)
64 is_admin = models.BooleanField(_('admin status'), default=False)
65 is_basic_user = models.BooleanField(_('basic_user status'), default=False)
66
67 objects = OsmUserManager()
68
69 USERNAME_FIELD = 'username'
70 REQUIRED_FIELDS = []
71
72 class Meta:
73 verbose_name = _('custom user')
74 verbose_name_plural = _('custom users')
75 abstract = True
76
77
78
79 class OsmUser(AbstractOsmUser):
80 """
81 Concrete class of AbstractCustomUser.
82
83 Use this if you don't need to extend CustomUser.
84
85 """
86
87 class Meta(AbstractOsmUser.Meta):
88 swappable = 'AUTH_USER_MODEL'
89
90 def get_projects(self):
91 client = Client()
92 return []