Skip to content
This repository was archived by the owner on Nov 11, 2021. It is now read-only.

Commit 982b20b

Browse files
committed
por Jossy
1 parent 6301a5a commit 982b20b

File tree

1 file changed

+82
-19
lines changed

1 file changed

+82
-19
lines changed

src/citasalud/apps/main/models.py

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,101 @@
33
from .constants import TIPO_MEDICO
44
from .customfields import DNIField, PrimaryNumberField
55
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+
647

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)
753

8-
class Perfil(models.Model):
9-
usuario = models.OneToOneField(User, related_name="profile", null=True, blank=True)
10-
dni = DNIField("DNI", unique=True)
1154
fecha_nacimiento = models.DateField("Fecha de Nacimiento")
1255
telefono = models.CharField("Teléfono", max_length=9, blank=True)
1356

1457
ciudad = models.CharField("Ciudad de origen", max_length=128)
58+
59+
email = models.EmailField("E-mail", unique=True, blank=True)
1560
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' ]
1669

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]
2872

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)
3175

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
3497

3598

3699
class Paciente(models.Model):
37-
usuario = models.ForeignKey(User, unique=True, related_name="paciente")
100+
usuario = models.ForeignKey(Usuario, unique=True, related_name="paciente")
38101
nss = PrimaryNumberField("NSS", digits=9)
39102

40103
def __unicode__(self):

0 commit comments

Comments
 (0)