Guest User

Untitled

a guest
Jan 29th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. django.db.utils.IntegrityError: NOT NULL constraint failed: account_estudianteperfil.anno_id
  2.  
  3. from django.db import models
  4. from django.contrib.auth.models import (
  5. BaseUserManager, AbstractBaseUser
  6. )
  7.  
  8.  
  9. # Create your models here.
  10.  
  11.  
  12.  
  13. class UserManager(BaseUserManager):
  14. def create_user(self, email, password=None, **extra_fields):
  15. """
  16. Creates and saves a User with the given email and password.
  17. """
  18. if not email:
  19. raise ValueError('El usuario debe tener un email')
  20.  
  21. user = self.model(
  22. email=self.normalize_email(email), **extra_fields
  23. )
  24.  
  25. user.set_password(password)
  26. user.save(using=self._db)
  27. return user
  28.  
  29. def create_staffuser(self, email, password):
  30. """
  31. Creates and saves a staff user with the given email and password.
  32. """
  33. user = self.create_user(
  34. email,
  35. password=password,
  36. )
  37. user.staff = True
  38. user.save(using=self._db)
  39. return user
  40.  
  41. def create_superuser(self, email, password):
  42. """
  43. Creates and saves a superuser with the given email and password.
  44. """
  45. user = self.create_user(
  46. email,
  47. password=password,
  48. )
  49. user.staff = True
  50. user.admin = True
  51. user.save(using=self._db)
  52. return user
  53.  
  54.  
  55. class User(AbstractBaseUser):
  56. email = models.EmailField(
  57. verbose_name='email address',
  58. max_length=255,
  59. unique=True,
  60. )
  61. active = models.BooleanField(default=True)
  62. staff = models.BooleanField(default=False) # a admin user; non super-user
  63. admin = models.BooleanField(default=False) # a superuser
  64. is_profesor = models.BooleanField(verbose_name='Profesor', default=False)
  65. is_estudiante = models.BooleanField(verbose_name='Estudiante', default=False)
  66. first_name = models.CharField(verbose_name='first name', max_length=30, blank=True)
  67. last_name = models.CharField(verbose_name='last name', max_length=30, blank=True)
  68. date_joined = models.DateTimeField(verbose_name='fecha de union', auto_now_add=True)
  69.  
  70. # notice the absence of a "Password field", that's built in.
  71.  
  72. USERNAME_FIELD = 'email'
  73. REQUIRED_FIELDS = [] # Email & Password are required by default.
  74.  
  75. objects = UserManager()
  76.  
  77. class Meta:
  78. db_table = 'auth_user'
  79.  
  80. def get_full_name(self):
  81. # The user is identified by their email address
  82. return self.email
  83.  
  84. def get_short_name(self):
  85. # The user is identified by their email address
  86. return self.email
  87.  
  88. def __str__(self): # __unicode__ on Python 2
  89. return self.email
  90.  
  91. def has_perm(self, perm, obj=None):
  92. "Does the user have a specific permission?"
  93. # Simplest possible answer: Yes, always
  94. return True
  95.  
  96. def has_module_perms(self, app_label):
  97. "Does the user have permissions to view the app `app_label`?"
  98. # Simplest possible answer: Yes, always
  99. return True
  100.  
  101. @property
  102. def is_staff(self):
  103. "Is the user a member of staff?"
  104. return self.staff
  105.  
  106. @property
  107. def is_admin(self):
  108. "Is the user a admin member?"
  109. return self.admin
  110.  
  111. @property
  112. def is_active(self):
  113. "Is the user active?"
  114. return self.active
  115.  
  116.  
  117. class Carrera(models.Model):
  118. carrera = models.CharField(max_length=50, verbose_name='Carrera')
  119.  
  120.  
  121. class Anno(models.Model):
  122. anno = models.CharField(max_length=50, verbose_name='Año')
  123.  
  124.  
  125. class EstudiantePerfil(models.Model):
  126. user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
  127. carrera = models.ForeignKey(Carrera, on_delete=models.CASCADE, related_name='estudiantes')
  128. anno = models.ForeignKey(Anno, on_delete=models.CASCADE, related_name='estudiantes')
  129.  
  130. from django.contrib.auth import login
  131. from django.shortcuts import redirect
  132. from django.views.generic import CreateView
  133.  
  134. from ..forms import StudentSignUpForm
  135. from ..models import User
  136.  
  137. class StudentSignUpView(CreateView):
  138. model = User
  139. form_class = StudentSignUpForm
  140. template_name = 'registro/signup_form.html'
  141.  
  142. def get_context_data(self, **kwargs):
  143. kwargs['user_type'] = 'Estudiante'
  144. return super().get_context_data(**kwargs)
  145.  
  146. def form_valid(self, form):
  147. user = form.save()
  148. login(self.request, user)
  149. return redirect('students:quiz_list')
  150.  
  151. from django import forms
  152. from django.contrib.auth.forms import ReadOnlyPasswordHashField, UserCreationForm
  153. from django.db import transaction
  154.  
  155. from .models import User, EstudiantePerfil, Carrera, Anno
  156.  
  157.  
  158. class RegisterForm(forms.ModelForm):
  159. password = forms.CharField(widget=forms.PasswordInput)
  160. password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
  161.  
  162. class Meta:
  163. model = User
  164. fields = ('email',)
  165.  
  166. def clean_email(self):
  167. email = self.cleaned_data.get('email')
  168. qs = User.objects.filter(email=email)
  169. if qs.exists():
  170. raise forms.ValidationError("email is taken")
  171. return email
  172.  
  173. def clean_password2(self):
  174. # Check that the two password entries match
  175. password1 = self.cleaned_data.get("password1")
  176. password2 = self.cleaned_data.get("password2")
  177. if password1 and password2 and password1 != password2:
  178. raise forms.ValidationError("Passwords don't match")
  179. return password2
  180.  
  181.  
  182. class UserAdminCreationForm(forms.ModelForm):
  183. """A form for creating new users. Includes all the required
  184. fields, plus a repeated password."""
  185. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  186. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  187.  
  188. class Meta:
  189. model = User
  190. fields = ('email', 'active', 'staff', 'is_estudiante', 'is_profesor')
  191.  
  192. def clean_password2(self):
  193. # Check that the two password entries match
  194. password1 = self.cleaned_data.get("password1")
  195. password2 = self.cleaned_data.get("password2")
  196. if password1 and password2 and password1 != password2:
  197. raise forms.ValidationError("Passwords don't match")
  198. return password2
  199.  
  200. def save(self, commit=True):
  201. # Save the provided password in hashed format
  202. user = super(UserAdminCreationForm, self).save(commit=False)
  203. user.set_password(self.cleaned_data["password1"])
  204. if commit:
  205. user.save()
  206. return user
  207.  
  208.  
  209. class UserAdminChangeForm(forms.ModelForm):
  210. """A form for updating users. Includes all the fields on
  211. the user, but replaces the password field with admin's
  212. password hash display field.
  213. """
  214. password = ReadOnlyPasswordHashField()
  215.  
  216. class Meta:
  217. model = User
  218. fields = ('email', 'password', 'active', 'admin')
  219.  
  220. def clean_password(self):
  221. # Regardless of what the user provides, return the initial value.
  222. # This is done here, rather than on the field, because the
  223. # field does not have access to the initial value
  224. return self.initial["password"]
  225.  
  226.  
  227.  
  228.  
  229.  
  230. class StudentSignUpForm(RegisterForm):
  231. carrera=forms.ModelChoiceField(queryset=Carrera.objects.all())
  232. anno=forms.ModelChoiceField(queryset=Anno.objects.all())
  233.  
  234. class Meta:
  235. model = User
  236. fields=('email',)
  237.  
  238. @transaction.atomic
  239. def save(self):
  240. user = super().save(commit=False)
  241. user.is_estudiante = True
  242. user.save()
  243. estudiante = EstudiantePerfil.objects.create(user=user)
  244. estudiante.carrera = 'carrera'
  245. estudiante.anno = 'anno'
  246. estudiante.save()
  247. return user
Add Comment
Please, Sign In to add comment