models.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. from django.db import models
  2. from django.contrib.auth.models import AbstractUser, BaseUserManager
  3. from django.core import validators
  4. from django.utils.translation import ugettext_lazy as _
  5. # Create your models here.
  6. ## para tabla personalizada de usuario e django
  7. class MyUserManager(BaseUserManager):
  8. def create_user(self, email, username, password=None):
  9. """
  10. Creates and saves a User with the given email, date of
  11. birth and password.
  12. """
  13. # if not email:
  14. # raise ValueError('Users must have an email address')
  15. user = self.model(
  16. username=username,
  17. email=self.normalize_email(email),
  18. is_superuser=False, # True?
  19. )
  20. user.set_password(password)
  21. user.save(using=self._db)
  22. return user
  23. def create_superuser(self, email, username, password, *kwargs):
  24. """
  25. Creates and saves a superuser with the given email, date of
  26. birth and password.
  27. """
  28. user = self.create_user(
  29. email,
  30. username,
  31. password=password,
  32. )
  33. user.is_admin = True
  34. user.save(using=self._db)
  35. return user
  36. # tabla user personalzida
  37. class Usuario(AbstractUser):
  38. objects = MyUserManager()
  39. username = models.CharField(
  40. _('username'),
  41. max_length=254,
  42. unique=True,
  43. help_text=_('Required. 254 characters or fewer. Letters, digits and @/./+/-/_ only.'),
  44. validators=[
  45. validators.RegexValidator(
  46. r'^[\w.@+-]+$',
  47. _('Enter a valid username. This value may contain only '
  48. 'letters, numbers ' 'and @/./+/-/_ characters.')
  49. ),
  50. ],
  51. error_messages={
  52. 'unique': _("A user with that username already exists."),
  53. },
  54. db_index=True,
  55. )
  56. first_name = models.CharField(
  57. verbose_name= _('nombres'),
  58. max_length=125,
  59. blank=False
  60. )
  61. last_name = models.CharField(
  62. verbose_name= _('apellidos'),
  63. max_length=125,
  64. blank=False
  65. )
  66. is_admin = models.BooleanField(
  67. verbose_name = _("es admin"),
  68. default=False
  69. )
  70. ci = models.IntegerField(
  71. verbose_name=_("C.I."),
  72. default=0
  73. )
  74. ru = models.IntegerField(
  75. verbose_name=_("Registro universitario"),
  76. default=0,
  77. )
  78. tipo = models.IntegerField(
  79. verbose_name=_("Tipo"),
  80. default=1,
  81. )
  82. # requeridos por AbstractUser
  83. USERNAME_FIELD = 'username'
  84. REQUIERED_FIELDS = ['first_name', 'last_name']
  85. def __str__(self):
  86. return ("("+str(self.id) +") " + self.username)
  87. def get_full_name(self):
  88. return (self.first_name + " " + self.last_name)
  89. def change_password(self, new_password):
  90. self.set_password(new_password)
  91. self.save()
  92. def send_email(self, subject, message, from_email=None, **kwargs):
  93. """
  94. Sends an email to this User.
  95. """
  96. send_mail(subject, message, from_email, [self.email], **kwargs)
  97. def has_perm(self, perm, obj=None):
  98. "Does the user have a specific permission?"
  99. # Simplest possible answer: Yes, always
  100. return True
  101. # def save(self, *args, **kwargs):
  102. ''' asegurando que no haya otro usuario con los first_name , last_name'''
  103. # modificar lo siguiente
  104. # if not self.pk and self.has_usable_password() is False:
  105. # self.set_password(self.password)
  106. # super(User, self).save(*args, **kwargs)
  107. @property
  108. def is_staff(self):
  109. "Is the user a member of staff?"
  110. # Simplest possible answer: All admins are staff
  111. return self.is_admin
  112. # class Meta:
  113. # ordering = ('created',)
  114. ####
  115. class Noticia(models.Model):
  116. tipo = models.IntegerField(
  117. verbose_name = _("Tipo"),
  118. default=1
  119. )
  120. para = models.IntegerField(
  121. verbose_name = _("Dirigido a quienes?"),
  122. default=1
  123. )
  124. titulo = models.CharField(
  125. verbose_name = _("Título"),
  126. max_length = 600,
  127. blank=False,
  128. )
  129. descripcion = models.CharField(
  130. verbose_name = _("Descripción"),
  131. max_length=9000,
  132. blank = False,
  133. )
  134. url = models.CharField(
  135. verbose_name = _("URL externa"),
  136. max_length=2000,
  137. default = "",
  138. blank = True,
  139. )
  140. # metodos
  141. def __str__(self):
  142. return ("("+ str(self.id) + ") "+ self.titulo)
  143. class NotificacionNoticia(models.Model):
  144. usuario = models.ForeignKey(
  145. Usuario,
  146. verbose_name = _("Usuario a la que va dirigida"),
  147. )
  148. tipo = models.IntegerField(
  149. verbose_name=_("tipo"),
  150. default=1
  151. )
  152. visto = models.BooleanField(
  153. verbose_name = _("Visto?"),
  154. default = False
  155. )
  156. suscrito = models.BooleanField(
  157. verbose_name = _("El usuario se ha suscrito?"),
  158. default = False
  159. )
  160. titulo = models.CharField(
  161. verbose_name = _("Título de la notificación"),
  162. max_length=500,
  163. )
  164. descripcion = models.CharField(
  165. verbose_name = _("Descripción"),
  166. max_length=5000,
  167. )
  168. url = models.CharField(
  169. verbose_name = _("URL externa (opcional)"),
  170. max_length=2000,
  171. default="",
  172. blank = True
  173. )
  174. # metodos
  175. def __str__(self):
  176. return ("(" + str(self.id) + ") " + self.titulo + ", usuario:" +
  177. str(self.usuario))
  178. # class Meta:
  179. # ordering = ('created',)
  180. class Suscripcion(models.Model):
  181. noticia = models.ForeignKey(
  182. Noticia,
  183. verbose_name = _("Noticia en la que se basa (opcional)"),
  184. blank=True
  185. )
  186. nombre_actividad = models.CharField(
  187. verbose_name = _("Nombre de la actividad"),
  188. max_length = 500,
  189. )
  190. descripcion = models.CharField(
  191. verbose_name = _("Descripción de la actividad"),
  192. max_length = 5000,
  193. )
  194. cupo_maximo = models.IntegerField(
  195. verbose_name = _("Cupo máximo"),
  196. )
  197. para = models.IntegerField(
  198. verbose_name = _("Dirigido a"),
  199. default = 1,
  200. )
  201. # metodos
  202. def __str__(self):
  203. return ("(" + str(self.id) + ") " + self.nombre_actividad)
  204. class NotificacionSuscripcion(models.Model):
  205. usuario = models.ForeignKey(
  206. Usuario,
  207. verbose_name = _("Usuario que se ha suscrito"),
  208. )
  209. suscripcion = models.ForeignKey(
  210. Suscripcion,
  211. verbose_name = _("Suscripcion a la que se ha suscrito"),
  212. )
  213. # metodos
  214. def __str__(self):
  215. return ("(" + str(self.id) + ") usuario:" + \
  216. str(self.usuario) + \
  217. str(self.suscripcion)
  218. )
  219. # class Meta:
  220. # ordering = ('created',)
  221. class PreMateria(models.Model):
  222. sigla = models.CharField(
  223. verbose_name=_("Sigla de la materia"),
  224. max_length = 200,
  225. )
  226. nombre = models.CharField(
  227. verbose_name = _("Nombre de la materia"),
  228. max_length = 300,
  229. )
  230. descripcion = models.CharField(
  231. verbose_name = _("Descripción detallada"),
  232. max_length = 1200,
  233. )
  234. cupos = models.IntegerField(
  235. verbose_name = _("Cupo máximo"),
  236. )
  237. nombre_docente = models.CharField(
  238. verbose_name = _("Nombre del docente (opcional)"),
  239. max_length = 200,
  240. default = "",
  241. )
  242. horario = models.CharField(
  243. verbose_name = _("Horario (opcional)"),
  244. max_length = 500,
  245. default = "",
  246. )
  247. aula = models.CharField(
  248. verbose_name = _("Aula (opcional)"),
  249. max_length = 200,
  250. default = "",
  251. )
  252. gestion = models.CharField(
  253. verbose_name = _("Gestión"),
  254. max_length = 100,
  255. )
  256. semestre = models.IntegerField(
  257. verbose_name = _("Semestre (1 al 10)"),
  258. )
  259. pre_requisitos = models.CharField(
  260. verbose_name = _("Pre requsitos (siglas materias)"),
  261. max_length = 200,
  262. default = ""
  263. )
  264. def __str__(self):
  265. return ("("+str(self.id)+") "+self.sigla + " - " + self.gestion)
  266. class PreInscripcionEstudiante(models.Model):
  267. usuario = models.ForeignKey(
  268. Usuario,
  269. verbose_name = _("Estudiante pre inscrito"),
  270. )
  271. pre_materia = models.ForeignKey(
  272. PreMateria,
  273. verbose_name = _("Materia pre inscrita"),
  274. )
  275. def __str__(self):
  276. return ("("+str(self.id)+")" + "Estudiante:"+self.usuario + " Materia:"+self.pre_materia)