forms.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from django import forms
  2. from django.core.validators import RegexValidator, MaxLengthValidator, MinLengthValidator, ValidationError
  3. from . models import Project, Setting
  4. numeric = RegexValidator(r'^[0-9+]', 'Only numeric characters.')
  5. time_max_length = MaxLengthValidator(4, 'Length limit exceeds 4 characters.')
  6. max_daily_hours_length = MaxLengthValidator(2, 'Only 2 place values allowed.')
  7. session_timeout_length = MaxLengthValidator(4, 'Only 4 place values allowed.')
  8. min_pin_length = MinLengthValidator(4, 'Must be at least 4 characters long.')
  9. max_pin_length = MaxLengthValidator(4, 'May not be longer then 4 characters long.')
  10. def pin_blacklist(value):
  11. blacklist = ['1234', '4321', '0000', '1111',
  12. '2222', '3333', '4444', '5555',
  13. '6666', '7777', '8888', '9999',
  14. '2345', '5432', '3456', '6543',
  15. '4567', '7654', '5678', '8765',
  16. '6789', '9876', '7890', '0987']
  17. if value in blacklist:
  18. raise ValidationError("Please provide a more complex PIN")
  19. else:
  20. return value
  21. hours = []
  22. try:
  23. max_daily_hours = Setting.objects.get(setting='Max Daily Hours')
  24. for hour in range(0, int(max_daily_hours.value)+1):
  25. hours.append(("%i" % hour, "%i" % hour))
  26. except Exception as e:
  27. print(e)
  28. for hour in range(0, 9):
  29. hours.append(("%i" % hour, "%i" % hour))
  30. minutes = (
  31. ('0', '0'),
  32. ('0.25', '15'),
  33. ('0.50', '30'),
  34. ('0.75', '45')
  35. )
  36. class LoginForm(forms.Form):
  37. pin = forms.CharField(strip=True, widget=forms.PasswordInput(attrs={
  38. 'class': 'form-control form-control-lg p-4 text-center',
  39. 'id': 'pin',
  40. 'placeholder': 'PIN',
  41. }), validators=[numeric])
  42. class CreateUserForm(forms.Form):
  43. first_name = forms.CharField(strip=True, required=True)
  44. last_name = forms.CharField(strip=True, required=True)
  45. pin = forms.CharField(strip=True, required=True, validators=[numeric, min_pin_length, max_pin_length, pin_blacklist],
  46. help_text="4 character numeric PIN")
  47. class TimeEntryForm(forms.Form):
  48. project = forms.ModelChoiceField(Project.objects.all(), required=False, label="Select a project if applicable (not required)",
  49. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  50. hours = forms.ChoiceField(required=True, choices=hours,
  51. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  52. minutes = forms.ChoiceField(required=True, choices=minutes,
  53. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  54. class SettingsForm(forms.Form):
  55. max_daily_hours = forms.CharField(required=True, validators=[max_daily_hours_length])
  56. session_timeout = forms.CharField(required=True, validators=[session_timeout_length])
  57. allow_entry_edit = forms.BooleanField(required=True)