forms.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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', '00'),
  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,
  49. label="Select a project if applicable (not required)",
  50. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  51. hours = forms.ChoiceField(required=True, choices=hours,
  52. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  53. minutes = forms.ChoiceField(required=True, choices=minutes,
  54. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  55. class SettingsForm(forms.Form):
  56. max_daily_hours = forms.CharField(
  57. required=True,
  58. validators=[max_daily_hours_length],
  59. help_text="Sets the maximum hour value for the Hours select box."
  60. )
  61. max_daily_entries = forms.CharField(
  62. required=True,
  63. help_text="Sets the maximum number of time entries for a given calendar day."
  64. )
  65. session_timeout = forms.CharField(
  66. required=True,
  67. validators=[session_timeout_length],
  68. label="Session Timeout (Minutes)",
  69. help_text="Sets the automatic logout time limit in minutes"
  70. )
  71. projects = forms.BooleanField(required=False, label="Enable Projects",
  72. help_text="Allows for time entries to be attached to projects.")