|
3 | 3 | from .constants import TIPO_MEDICO
|
4 | 4 | from .customfields import DNIField, PrimaryNumberField
|
5 | 5 | from django.contrib.auth.models import User
|
| 6 | +from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin |
| 7 | + |
| 8 | + |
| 9 | +class UsuarioManager(BaseUserManager): |
| 10 | + def create_user(self, dni,fecha_nacimiento, nombres,apellido_paterno,password =None): |
| 11 | + """ |
| 12 | + Creates and saves a User with the given email, date of |
| 13 | + birth and password. |
| 14 | + """ |
| 15 | + if not dni: |
| 16 | + raise ValueError('EL USUARIO DEBE TENER DNI') |
| 17 | + |
| 18 | + user = self.model( |
| 19 | + dni=dni, |
| 20 | + nombres=nombres, |
| 21 | + apellido_paterno=apellido_paterno, |
| 22 | + fecha_nacimiento=fecha_nacimiento |
| 23 | + ) |
| 24 | + |
| 25 | + user.set_password(password) |
| 26 | + user.save(using=self._db) |
| 27 | + return user |
| 28 | + |
| 29 | + def create_superuser(self, dni,fecha_nacimiento, nombres,apellido_paterno,password): |
| 30 | + """ |
| 31 | + Creates and saves a superuser with the given email, date of |
| 32 | + birth and password. |
| 33 | + """ |
| 34 | + user = self.create_user(dni, |
| 35 | + password=password, |
| 36 | + nombres=nombres, |
| 37 | + apellido_paterno=apellido_paterno, |
| 38 | + fecha_nacimiento=fecha_nacimiento |
| 39 | + ) |
| 40 | + user.is_active=True |
| 41 | + user.is_admin = True |
| 42 | + user.save(using=self._db) |
| 43 | + return user |
| 44 | + |
| 45 | +# def normalize_dni (self,dni): |
| 46 | + |
6 | 47 |
|
| 48 | +class Usuario(AbstractBaseUser): |
| 49 | + dni = DNIField("DNI", unique=True , primary_key= True ) |
| 50 | + nombre = models.CharField("Nombre", max_length=32) |
| 51 | + apellido_paterno = models.CharField("Ap. Paterno", max_length=16) |
| 52 | + apellido_materno = models.CharField("Ap. Materno", max_length=16) |
7 | 53 |
|
8 |
| -class Perfil(models.Model): |
9 |
| - usuario = models.OneToOneField(User, related_name="profile", null=True, blank=True) |
10 |
| - dni = DNIField("DNI", unique=True) |
11 | 54 | fecha_nacimiento = models.DateField("Fecha de Nacimiento")
|
12 | 55 | telefono = models.CharField("Teléfono", max_length=9, blank=True)
|
13 | 56 |
|
14 | 57 | ciudad = models.CharField("Ciudad de origen", max_length=128)
|
| 58 | + |
| 59 | + email = models.EmailField("E-mail", unique=True, blank=True) |
15 | 60 | avatar = models.ImageField("Imagen", upload_to="avatar", null=False, default="/media/avatar/sin_imagen.png", blank=True)
|
| 61 | + |
| 62 | + is_active=models.BooleanField(default=False) |
| 63 | + is_admin=models.BooleanField(default = False) |
| 64 | + |
| 65 | + objects=UsuarioManager() |
| 66 | + |
| 67 | + USERNAME_FIELDS= 'dni' |
| 68 | + REQUIRED_FIELDS = [ 'fecha_nacimiento','nombres' ,'apellido_paterno' ] |
16 | 69 |
|
17 |
| -# def get_name(self): |
18 |
| -# return self.nombre.split(" ")[0] |
19 |
| -# |
20 |
| -# def get_full_name(self): |
21 |
| -# return "%s %s" % (self.usuario.first_name, self.usuario.last_name) |
22 |
| -# |
23 |
| -# def get_short_name(self): |
24 |
| -# return "%s %s" % (self.get_name(), self.apellido_paterno) |
25 |
| -# |
26 |
| - def __unicode__(self): |
27 |
| - return "[%s] %s" % (self.dni, self.usuario.get_full_name()) |
| 70 | + def get_name(self): |
| 71 | + return self.nombre.split(" ")[0] |
28 | 72 |
|
29 |
| - # def has_perm(self, perm, obj=None): |
30 |
| - # return True |
| 73 | + def get_full_name(self): |
| 74 | + return "%s %s %s" % (self.nombre, self.apellido_paterno, self.apellido_materno) |
31 | 75 |
|
32 |
| - # def has_module_perms(self, app_label): |
33 |
| - # return True |
| 76 | + def get_short_name(self): |
| 77 | + return "%s %s" % (self.get_name(), self.apellido_paterno) |
| 78 | + |
| 79 | + def __unicode__(self): |
| 80 | + return "[%s] %s" % (self.dni, self.get_full_name()) |
| 81 | + |
| 82 | + @property |
| 83 | + def is_staff(self): |
| 84 | + "Is the user a member of staff?" |
| 85 | + # Simplest possible answer: All admins are staff |
| 86 | + return self.is_admin |
| 87 | + |
| 88 | +# def has_perm(self, perm, obj=None): |
| 89 | +# #"Does the user have a specific permission?" |
| 90 | +# # Simplest possible answer: Yes, always |
| 91 | +# return True |
| 92 | +# |
| 93 | +# def has_module_perms(self, app_label): |
| 94 | +# "Does the user have permissions to view the app `app_label`?" |
| 95 | +# # Simplest possible answer: Yes, always |
| 96 | +# return True |
34 | 97 |
|
35 | 98 |
|
36 | 99 | class Paciente(models.Model):
|
37 |
| - usuario = models.ForeignKey(User, unique=True, related_name="paciente") |
| 100 | + usuario = models.ForeignKey(Usuario, unique=True, related_name="paciente") |
38 | 101 | nss = PrimaryNumberField("NSS", digits=9)
|
39 | 102 |
|
40 | 103 | def __unicode__(self):
|
|
0 commit comments