forms.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. max_daily_hours = Setting.objects.get(setting='Max Daily Hours')
  23. for hour in range(0, int(max_daily_hours.value)+1):
  24. hours.append(("%i" % hour, "%i" % hour))
  25. days = (
  26. ('1', '1'),
  27. ('2', '2'),
  28. ('3', '3'),
  29. ('4', '4'),
  30. ('5', '5'),
  31. ('6', '6'),
  32. ('7', '7'),
  33. ('8', '8'),
  34. ('9', '9'),
  35. ('10', '10'),
  36. ('11', '11'),
  37. ('12', '12'),
  38. ('13', '13'),
  39. ('14', '14'),
  40. ('15', '15'),
  41. ('16', '16'),
  42. ('17', '17'),
  43. ('18', '18'),
  44. ('19', '19'),
  45. ('20', '20'),
  46. ('21', '21'),
  47. ('22', '22'),
  48. ('23', '23'),
  49. ('24', '24'),
  50. ('25', '25'),
  51. ('26', '26'),
  52. ('27', '27'),
  53. ('28', '28'),
  54. ('29', '29'),
  55. ('30', '30'),
  56. ('31', '31')
  57. )
  58. minutes = (
  59. ('0', '00'),
  60. ('0.25', '15'),
  61. ('0.50', '30'),
  62. ('0.75', '45')
  63. )
  64. class LoginForm(forms.Form):
  65. pin = forms.CharField(strip=True, widget=forms.PasswordInput(attrs={
  66. 'class': 'form-control form-control-lg p-4 text-center',
  67. 'id': 'pin',
  68. 'placeholder': 'PIN',
  69. }), validators=[numeric])
  70. class CreateUserForm(forms.Form):
  71. first_name = forms.CharField(strip=True, required=True)
  72. last_name = forms.CharField(strip=True, required=True)
  73. pin = forms.CharField(strip=True, required=True, validators=[numeric, min_pin_length, max_pin_length, pin_blacklist],
  74. help_text="4 character numeric PIN")
  75. class TimeEntryForm(forms.Form):
  76. day_of_month = forms.ChoiceField(choices=days, widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  77. project = forms.ModelChoiceField(Project.objects.all(), required=False,
  78. label="Select a project if applicable (not required)",
  79. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  80. hours = forms.ChoiceField(required=True, choices=hours,
  81. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  82. minutes = forms.ChoiceField(required=True, choices=minutes,
  83. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  84. class SettingsForm(forms.Form):
  85. max_daily_hours = forms.CharField(
  86. required=True,
  87. validators=[max_daily_hours_length],
  88. help_text="Sets the maximum hour value for the Hours select box."
  89. )
  90. max_daily_entries = forms.CharField(
  91. required=True,
  92. help_text="Sets the maximum number of time entries for a given calendar day."
  93. )
  94. session_timeout = forms.CharField(
  95. required=True,
  96. validators=[session_timeout_length],
  97. label="Session Timeout (Minutes)",
  98. help_text="Sets the automatic logout time limit in minutes"
  99. )
  100. projects = forms.BooleanField(required=False, label="Enable Projects",
  101. help_text="Allows for time entries to be attached to projects.")