2 # Copyright 2018 EveryUP Srl
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 class OsmUserManager(BaseUserManager
):
26 """Custom manager for OsmUser."""
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. """
33 raise ValueError('The given username must be set')
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
)
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,
49 class AbstractOsmUser(AbstractBaseUser
, PermissionsMixin
):
50 """Abstract User with the same behaviour as Django's default User.
53 Inherits from both the AbstractBaseUser and PermissionMixin.
55 The following attributes are inherited from the superclasses:
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)
67 objects
= OsmUserManager()
69 USERNAME_FIELD
= 'username'
73 verbose_name
= _('custom user')
74 verbose_name_plural
= _('custom users')
79 class OsmUser(AbstractOsmUser
):
81 Concrete class of AbstractCustomUser.
83 Use this if you don't need to extend CustomUser.
87 class Meta(AbstractOsmUser
.Meta
):
88 swappable
= 'AUTH_USER_MODEL'
90 def get_projects(self
):