server/ammicosrv/authentication/models.py
author nowmad@23.1.168.192.in-addr.arpa
Wed, 02 Dec 2015 19:39:16 +0100
changeset 203 786cdb826f09
parent 156 bf4ae7d9a517
permissions -rw-r--r--
Increment version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     1
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
     2
from django.contrib.auth.models import (
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
     3
    BaseUserManager, AbstractBaseUser
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
     4
)
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     5
from django.db import models
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
     6
from django.utils.translation import ugettext_lazy as _
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     7
135
27065f8a19d3 correct rename of src
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     8
from django.conf import settings
103
b707607e8a38 refer to the user model through settings.AUTH_USER_MODEL
rougeronj
parents: 63
diff changeset
     9
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    10
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    11
class AmmicoUserManager(BaseUserManager):
156
bf4ae7d9a517 refactor idUser to id_user
rougeronj
parents: 149
diff changeset
    12
    def create_user(self, email, id_user=None, password=None):
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    13
        """
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    14
        Creates and saves a User with the given email and password.
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    15
        """
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    16
        if not email:
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    17
            raise ValueError('Users must have an email address')
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    18
        
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    19
        user = self.model(
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    20
            email=self.normalize_email(email),
156
bf4ae7d9a517 refactor idUser to id_user
rougeronj
parents: 149
diff changeset
    21
            id_user=id_user,
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    22
        )
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    23
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    24
        user.set_password(password)
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    25
        user.save(using=self._db)
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    26
        return user
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    27
156
bf4ae7d9a517 refactor idUser to id_user
rougeronj
parents: 149
diff changeset
    28
    def create_superuser(self, email, password, id_user=None):
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    29
        """
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    30
        Creates and saves a superuser with the given email and password.
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    31
        """
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    32
        user = self.create_user(email,
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    33
            password=password,
156
bf4ae7d9a517 refactor idUser to id_user
rougeronj
parents: 149
diff changeset
    34
            id_user=id_user
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    35
        )
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    36
        user.is_admin = True
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    37
        user.save(using=self._db)
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    38
        return user
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    39
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    40
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    41
class AmmicoUser(AbstractBaseUser):
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    42
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
156
bf4ae7d9a517 refactor idUser to id_user
rougeronj
parents: 149
diff changeset
    43
    id_user = models.IntegerField(_('identifiant utilisateur'), blank=True, null=True)
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    44
    is_active = models.BooleanField(default=True)
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    45
    is_admin = models.BooleanField(default=False)
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    46
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    47
    objects = AmmicoUserManager()
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    48
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    49
    USERNAME_FIELD = 'email'
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    50
    REQUIRED_FIELDS = []
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    51
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    52
    def get_full_name(self):
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    53
        # The user is identified by their email address
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    54
        return self.email
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    55
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    56
    def get_short_name(self):
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    57
        # The user is identified by their email address
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    58
        return self.email
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    59
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    60
    def __str__(self):              # __unicode__ on Python 2
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    61
        return self.email
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    62
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    63
    def has_perm(self, perm, obj=None):
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    64
        "Does the user have a specific permission?"
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    65
        # Simplest possible answer: Yes, always
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    66
        return True
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    67
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    68
    def has_module_perms(self, app_label):
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    69
        "Does the user have permissions to view the app `app_label`?"
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    70
        # Simplest possible answer: Yes, always
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    71
        return True
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    72
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    73
    @property
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    74
    def is_staff(self):
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    75
        "Is the user a member of staff?"
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    76
        # Simplest possible answer: All admins are staff
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 103
diff changeset
    77
        return self.is_admin
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    78
    
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    79
class Profile(models.Model):
103
b707607e8a38 refer to the user model through settings.AUTH_USER_MODEL
rougeronj
parents: 63
diff changeset
    80
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    81
    image = models.URLField(max_length=2048, blank=True)