Guest User

Untitled

a guest
Aug 24th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.83 KB | None | 0 0
  1. from django.contrib.auth.models import User
  2. from django import forms
  3. from models import Facility_master, Facility_availability, Book_Facility
  4. from django.contrib.auth import (
  5.     authenticate,
  6.     login,
  7.     get_user_model,
  8.     logout,
  9.     )
  10. from django.contrib.auth.forms import UserChangeForm
  11. import datetime
  12. from django.forms.extras.widgets import SelectDateWidget
  13. from datetimewidget.widgets import DateTimeWidget, DateWidget, TimeWidget
  14.  
  15. User = get_user_model()
  16. # user login form
  17. class UserLoginForm(forms.Form):
  18.     username = forms.CharField(label='Employee id', widget=forms.TextInput(
  19.         attrs={ 'placeholder':'Enter your Employee ID',}
  20.  
  21.         ))
  22.     password = forms.CharField(widget=forms.PasswordInput(
  23.         attrs={ 'placeholder':'Enter your password',}
  24.  
  25.         ))
  26.     def clean(self, *args, **kwargs):
  27.         username= self.cleaned_data.get("username")
  28.         password= self.cleaned_data.get("password")
  29.         '''user_qs = User.objects.filter(username=username)
  30.         if user_qs.count() == 1:
  31.             user= user_qs.first()'''
  32.         if username and password:
  33.             user= authenticate(username= username, password=password)
  34.             if not user:
  35.                 raise forms.ValidationError("Please enter a valid username or password")
  36.  
  37.             if not user.is_active:
  38.                 raise forms.ValidationError("This user isn't active")
  39.  
  40.         return super(UserLoginForm, self).clean(*args, **kwargs)
  41.  
  42.  
  43. #facility booking form
  44. class FacilityBookForm(forms.ModelForm):
  45.  
  46.     class Meta:
  47.         model = Book_Facility
  48.         fields= ['facility', 'event', 'book_date', 'time_start', 'time_end',]
  49.         widgets = {
  50.             #Use localization and bootstrap 3
  51.             'book_date': DateWidget(attrs={'id':"yourdatetimeid"}, usel10n = True, bootstrap_version=3),
  52.             'time_start': TimeWidget(attrs={'id':"yourdatetimeid"}, usel10n = True, bootstrap_version=3),
  53.             'time_end': TimeWidget(attrs={'id':"yourdatetimeid"}, usel10n = True, bootstrap_version=3),
  54.         }
  55.  
  56.     def clean_book_date(self):
  57.         book_date = self.cleaned_data['book_date']
  58.         if book_date < datetime.date.today():
  59.             raise forms.ValidationError("The date cannot be in the past!")
  60.         return book_date
  61.  
  62.  
  63.         time_start=forms.TimeField(widget=TimeWidget(usel10n=True, bootstrap_version=3))
  64.         time_end=forms.TimeField(widget=TimeWidget(usel10n=True, bootstrap_version=3))
  65. '''    def __init__(self, *args, **kwargs):
  66.         self.username = kwargs.pop('username')
  67.         super(FacilityBookForm, self).__init__(*args, **kwargs)
  68.     def save(self, commit=True):
  69.         inst = super(FacilityBookForm, self).save(commit=False)
  70.         inst.author = self.username
  71.         if commit:
  72.             inst.save()
  73.             self.save_m2m()
  74.         return inst'''
  75.  
  76.  
  77. #user registration form
  78. class UserRegisterForm(forms.ModelForm):
  79.     email= forms.EmailField(widget=forms.EmailInput(attrs={
  80.         'placeholder': 'Enter you email address',}
  81.  
  82.         ), label='Email address')
  83.     username = forms.CharField(label='Employee id', widget=forms.TextInput(
  84.         attrs={ 'placeholder':'Enter your Employee id',}
  85.  
  86.         ))
  87.     first_name= forms.CharField(label='First Name', widget=forms.TextInput(
  88.         attrs={ 'placeholder':'Enter your First Name',}
  89.  
  90.         ))
  91.     last_name = forms.CharField(label='Last Name', widget=forms.TextInput(
  92.         attrs={ 'placeholder':'Enter your Last Name',}
  93.  
  94.         ))
  95.     password = forms.CharField(widget=forms.PasswordInput(
  96.         attrs={ 'placeholder':'Enter your password',}
  97.  
  98.         ))
  99.     password2= forms.CharField(label= 'Confirm Password',
  100.         widget=forms.PasswordInput(
  101.         attrs={ 'placeholder':'Confirm your password',}
  102.  
  103.         ))
  104.     class Meta:
  105.         model = User
  106.         fields = [ 'username', 'email','first_name','last_name', 'password', 'password2',]
  107.  
  108.     def clean_first_name(self):
  109.         first_name = self.cleaned_data.get('first_name')
  110.         return first_name
  111.     def clean_last_name(self):
  112.         last_name = self.cleaned_data.get('last_name')
  113.         return last_name
  114.     def clean_email(self):
  115.         email = self.cleaned_data.get('email')
  116.         email_qs= User.objects.filter(email=email)
  117.         if email_qs.exists():
  118.             raise forms.ValidationError("Email already registered")
  119.         return email
  120.     def clean_password2(self):
  121.         password = self.cleaned_data.get('password')
  122.         if len(password) < 8:
  123.             raise forms.ValidationError('Password too short. Password should have minimum 8 characters.')
  124.         password2 = self.cleaned_data.get('password2')
  125.  
  126.         if password != password2:
  127.             raise forms.ValidationError("The passwords do not match")
  128.         return password
  129. #forgot password
  130. #class PasswordResetRequestForm(forms.Form):
  131.     #email_or_username = forms.Charfield(label=("Email or Employee ID"),max_length=254)
  132. #edit profile
  133. '''class EditProfileForm(UserChangeForm):
  134.     class Meta:
  135.         model = User
  136.         fields = [
  137.  
  138.             'email',
  139.             'first_name',
  140.             'last_name',
  141.             'password',
  142.         ]
  143.  
  144. '''
  145.  
  146. class EditProfileForm(forms.ModelForm):
  147.     email = forms.EmailField(required=True)
  148.     first_name = forms.CharField(required=True)
  149.     last_name = forms.CharField(required=True)
  150.     username = forms.CharField(label='Employee id', widget=forms.TextInput(
  151.         attrs={ 'placeholder':'Enter your Employee id',}
  152.         ))
  153.  
  154.     class Meta:
  155.         model = User
  156.         fields = ['username','email', 'first_name', 'last_name']
  157.     def clean_username(self):
  158.         username = self.cleaned_data.get('username')
  159.         return username
  160.     def clean_email(self):
  161.         email = self.cleaned_data.get('email')
  162.         email_qs= User.objects.filter(email=email).exclude(pk=self.instance.pk)#to disable checking the logged in user
  163.         if email_qs.exists():
  164.             raise forms.ValidationError("Email already registered")
  165.         return email
  166.     def save(self, commit=True):
  167.         user_edit = super(EditProfileForm, self).save(commit=False)
  168.         if commit:
  169.             user_edit.save()
  170.         return user_edit
  171.  
  172.  
  173.  
  174. ''' def clean_email(self):
  175.         email = self.cleaned_data.get('email')
  176.         if email and User.objects.filter(email=email):
  177.             raise forms.ValidationError('This email address is already in use. Please supply a different email address.')
  178.         return email'''
  179.  
  180.  
  181. '''     if commit:
  182.             user.save()
  183.         return user'''
Add Comment
Please, Sign In to add comment