server/ammicosrv/authentication/models.py
changeset 149 8e117699857a
parent 143 ea633c8f9bfa
parent 135 27065f8a19d3
child 156 bf4ae7d9a517
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/ammicosrv/authentication/models.py	Wed Jun 10 10:57:27 2015 +0200
@@ -0,0 +1,81 @@
+
+from django.contrib.auth.models import (
+    BaseUserManager, AbstractBaseUser
+)
+from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+from django.conf import settings
+
+
+class AmmicoUserManager(BaseUserManager):
+    def create_user(self, email, idUser=None, password=None):
+        """
+        Creates and saves a User with the given email and password.
+        """
+        if not email:
+            raise ValueError('Users must have an email address')
+        
+        user = self.model(
+            email=self.normalize_email(email),
+            idUser=idUser,
+        )
+
+        user.set_password(password)
+        user.save(using=self._db)
+        return user
+
+    def create_superuser(self, email, password, idUser=None):
+        """
+        Creates and saves a superuser with the given email and password.
+        """
+        user = self.create_user(email,
+            password=password,
+            idUser=idUser
+        )
+        user.is_admin = True
+        user.save(using=self._db)
+        return user
+
+
+class AmmicoUser(AbstractBaseUser):
+    email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
+    idUser = models.IntegerField(_('identifiant utilisateur'), blank=True, null=True)
+    is_active = models.BooleanField(default=True)
+    is_admin = models.BooleanField(default=False)
+
+    objects = AmmicoUserManager()
+
+    USERNAME_FIELD = 'email'
+    REQUIRED_FIELDS = []
+
+    def get_full_name(self):
+        # The user is identified by their email address
+        return self.email
+
+    def get_short_name(self):
+        # The user is identified by their email address
+        return self.email
+
+    def __str__(self):              # __unicode__ on Python 2
+        return self.email
+
+    def has_perm(self, perm, obj=None):
+        "Does the user have a specific permission?"
+        # Simplest possible answer: Yes, always
+        return True
+
+    def has_module_perms(self, app_label):
+        "Does the user have permissions to view the app `app_label`?"
+        # Simplest possible answer: Yes, always
+        return True
+
+    @property
+    def is_staff(self):
+        "Is the user a member of staff?"
+        # Simplest possible answer: All admins are staff
+        return self.is_admin
+    
+class Profile(models.Model):
+    user = models.OneToOneField(settings.AUTH_USER_MODEL)
+    image = models.URLField(max_length=2048, blank=True)
\ No newline at end of file