server/src/metaeducation/models.py
author durandn
Tue, 12 Apr 2016 11:59:32 +0200
changeset 66 0cbcfc82b5dc
parent 29 23de98e32b3b
permissions -rw-r--r--
remove default django validator on username
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     1
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     2
from django.db import models
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     3
from django.core import validators
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     4
from django.utils.translation import ugettext_lazy as _
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     5
from django.utils import timezone
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     6
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     7
import uuid
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     8
        
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     9
class UserManager(BaseUserManager):
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    10
    
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    11
    def create_user(self, external_id, username, password=None, **extra_fields):
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    12
        """
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    13
        Creates and saves a User with the given username, email and password.
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    14
        """
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    15
        now = timezone.now()
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    16
        user = self.model(external_id=external_id, username=username,
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    17
                          is_staff=False, is_active=True, is_superuser=False,
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    18
                          date_joined=now, **extra_fields)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    19
        user.set_password(password)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    20
        user.save(using=self._db)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    21
        return user
9
fdbc47f06361 adding custom user model + corrected provider to correctly create user according to new model
durandn
parents:
diff changeset
    22
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    23
    def create_superuser(self, external_id, username, password=None, **extra_fields):
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    24
        superuser = self.create_user(external_id, username, password, **extra_fields)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    25
        superuser.is_staff = True
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    26
        superuser.is_active = True
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    27
        superuser.is_superuser = True
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    28
        superuser.save(using=self._db)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    29
        return superuser
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    30
    
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    31
class User(AbstractBaseUser, PermissionsMixin):
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    32
    external_id = models.CharField(max_length=256, unique=True, default=uuid.uuid4)
66
0cbcfc82b5dc remove default django validator on username
durandn
parents: 29
diff changeset
    33
    username = models.CharField(max_length=150)
29
23de98e32b3b added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
durandn
parents: 11
diff changeset
    34
    uai = models.CharField(max_length=40, blank=True)
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    35
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    36
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    37
    date_joined = models.DateTimeField(default=timezone.now)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    38
    is_active = models.BooleanField(default=True)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    39
    is_staff = models.BooleanField(default=False)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    40
    
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    41
    objects = UserManager()
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    42
    
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    43
    USERNAME_FIELD = 'external_id'
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    44
    REQUIRED_FIELDS = ['username']
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    45
    
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    46
    def get_full_name(self):
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    47
        """
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    48
        Returns the first_name plus the last_name, with a space in between.
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    49
        """
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    50
        full_name = '%s %s' % (self.first_name, self.last_name)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    51
        return full_name.strip()
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    52
    
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    53
    def get_short_name(self):
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    54
        return self.first_name
9
fdbc47f06361 adding custom user model + corrected provider to correctly create user according to new model
durandn
parents:
diff changeset
    55
    
fdbc47f06361 adding custom user model + corrected provider to correctly create user according to new model
durandn
parents:
diff changeset
    56
    def __unicode__(self):
fdbc47f06361 adding custom user model + corrected provider to correctly create user according to new model
durandn
parents:
diff changeset
    57
        return self.external_id