Advertisement
Darkolius

Untitled

Jul 10th, 2021
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.core.validators import MinLengthValidator
  4. from django.utils.translation import  gettext_lazy as _
  5.  
  6. #https://docs.djangoproject.com/en/3.1/ref/forms/widgets/
  7. from crispy_forms.helper import FormHelper
  8. from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field, Column
  9. from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
  10.  
  11. class UserSignUpForm(forms.ModelForm):
  12.     def __init__(self, *args, **kwargs):
  13.         super(UserSignUpForm, self).__init__(*args, **kwargs)
  14.         self.fields['first_name'].required = True
  15.         self.fields['last_name'].required = True
  16.  
  17.     who = forms.ChoiceField(
  18.         choices=[('student', 'Student'), ('teacher', 'Teacher')],
  19.         label="",
  20.         required=True,
  21.         widget=forms.RadioSelect(
  22.             attrs={'style':'max-width: 20em; ', 'autocomplete':'off', })
  23.     )
  24.     password = forms.CharField(
  25.         label="Password",
  26.         validators=[MinLengthValidator(8, message="Minimum 8 characters")],
  27.         widget=forms.PasswordInput(attrs={'autocomplete':'off'}))
  28.     confirm_password = forms.CharField(
  29.         label="Confirm password",
  30.         validators=[MinLengthValidator(8, message="Minimum 8 characters"), ],
  31.         widget=forms.PasswordInput(attrs={'autocomplete':'off'}))
  32.  
  33.     class Meta:
  34.         model = User
  35.         fields = ('who', "username", 'first_name', 'last_name', "password", )
  36.         help_texts = {"username": None}
  37.         widgets = {
  38.             'username': forms.TextInput(attrs={}),
  39.             'first_name': forms.TextInput(attrs={}),
  40.             'last_name': forms.TextInput(attrs={}),
  41.  
  42.         }
  43.  
  44.     def clean(self):
  45.         cleaned_data = super(UserSignUpForm, self).clean()
  46.         password = cleaned_data.get("password")
  47.         confirm_password = cleaned_data.get("confirm_password")
  48.         if password != confirm_password:
  49.             msg = _(f'Password and confirm password does not match')
  50.             self.add_error('password', msg)
  51.             self.add_error('confirm_password', msg)
  52.  
  53.     helper = FormHelper()
  54.     helper.form_tag = 'false'
  55.     helper.attrs = {"novalidate": True, 'autocomplete':'off'}
  56.     helper.form_class = 'form-horizontal'
  57.     helper.field_class = 'col-md-8 '
  58.     helper.label_class = 'col-md-4'
  59.     helper.layout = Layout(
  60.         Row(
  61.             Column(
  62.                 Field('who', css_class='form-group', style='margin-left:200px'),
  63.                 Field('username', css_class='form-group ', style=''),
  64.                 Field('first_name', css_class='form-group'),
  65.                 Field('last_name', css_class='form-group'),
  66.                 Field('password', css_class='form-group'),
  67.                 Field('confirm_password', css_class='form-group'),
  68.  
  69.                 FormActions(
  70.                     Submit('save', 'Sign up', css_class="btn-primary"),
  71.                     Submit('cancel', 'Cancel'),
  72.                     ),
  73.  
  74.             )
  75.         )
  76.     )
  77.  
  78. class UserLoginForm(forms.Form):
  79.     username = forms.CharField(widget=forms.TextInput(
  80.         attrs={
  81.             "class": "form-control"
  82.         }))
  83.     password = forms.CharField(
  84.         widget=forms.PasswordInput(
  85.             attrs={
  86.                 "class": "form-control",
  87.                 "id": "user-password"
  88.             }
  89.         )
  90.     )
  91.     def clean_username(self):
  92.         username = self.cleaned_data.get("username")
  93.         qs = User.objects.filter(username__iexact=username) # thisIsMyUsername == thisismyusername
  94.         if not qs.exists():
  95.             raise forms.ValidationError("This is an invalid user.")
  96.         if qs.count() != 1:
  97.             raise forms.ValidationError("This is an invalid user.")
  98.         return username
  99.  
  100.  
  101.     helper = FormHelper()
  102.     helper.form_tag = 'false'
  103.     helper.attrs = {'autocomplete':'off'}
  104.     helper.form_class = 'form-horizontal'
  105.     helper.field_class = 'col-md-8 '
  106.     helper.label_class = 'col-md-4'
  107.     helper.layout = Layout(
  108.         Row(
  109.             Column(
  110.                 Field('username', css_class='form-group ', style=''),
  111.                 Field('password', css_class='form-group'),
  112.  
  113.                 FormActions(
  114.                     Submit('submit', 'Sign in', css_class="btn-primary"),
  115.                     Submit('cancel', 'Cancel'),
  116.                     ),
  117.  
  118.             )
  119.         )
  120.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement