forms.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from django import forms
  2. from django.core.validators import RegexValidator, MaxLengthValidator, MinLengthValidator, ValidationError
  3. from . models import Project, Setting
  4. from calendar import monthrange
  5. numeric = RegexValidator(r'^[0-9+]', 'Only numeric characters.')
  6. time_max_length = MaxLengthValidator(4, 'Length limit exceeds 4 characters.')
  7. max_daily_hours_length = MaxLengthValidator(2, 'Only 2 place values allowed.')
  8. session_timeout_length = MaxLengthValidator(4, 'Only 4 place values allowed.')
  9. min_pin_length = MinLengthValidator(4, 'Must be at least 4 characters long.')
  10. max_pin_length = MaxLengthValidator(4, 'May not be longer then 4 characters long.')
  11. def pin_blacklist(value):
  12. blacklist = ['1234', '4321', '0000', '1111',
  13. '2222', '3333', '4444', '5555',
  14. '6666', '7777', '8888', '9999',
  15. '2345', '5432', '3456', '6543',
  16. '4567', '7654', '5678', '8765',
  17. '6789', '9876', '7890', '0987']
  18. if value in blacklist:
  19. raise ValidationError("Please provide a more complex PIN")
  20. else:
  21. return value
  22. hours = []
  23. try:
  24. max_daily_hours = Setting.objects.get(setting='Max Daily Hours')
  25. for hour in range(0, int(max_daily_hours.value)+1):
  26. hours.append(("%i" % hour, "%i" % hour))
  27. except Exception as e:
  28. print(e)
  29. for hour in range(0, 9):
  30. hours.append(("%i" % hour, "%i" % hour))
  31. days = (
  32. ('1', '1'),
  33. ('2', '2'),
  34. ('3', '3'),
  35. ('4', '4'),
  36. ('5', '5'),
  37. ('6', '6'),
  38. ('7', '7'),
  39. ('8', '8'),
  40. ('9', '9'),
  41. ('10', '10'),
  42. ('11', '11'),
  43. ('12', '12'),
  44. ('13', '13'),
  45. ('14', '14'),
  46. ('15', '15'),
  47. ('16', '16'),
  48. ('17', '17'),
  49. ('18', '18'),
  50. ('19', '19'),
  51. ('20', '20'),
  52. ('21', '21'),
  53. ('22', '22'),
  54. ('23', '23'),
  55. ('24', '24'),
  56. ('25', '25'),
  57. ('26', '26'),
  58. ('27', '27'),
  59. ('28', '28'),
  60. ('29', '29'),
  61. ('30', '30'),
  62. ('31', '31')
  63. )
  64. minutes = (
  65. ('0', '00'),
  66. ('0.25', '15'),
  67. ('0.50', '30'),
  68. ('0.75', '45')
  69. )
  70. class LoginForm(forms.Form):
  71. pin = forms.CharField(strip=True, widget=forms.PasswordInput(attrs={
  72. 'class': 'form-control form-control-lg p-4 text-center',
  73. 'id': 'pin',
  74. 'placeholder': 'PIN',
  75. }), validators=[numeric])
  76. class CreateUserForm(forms.Form):
  77. first_name = forms.CharField(strip=True, required=True)
  78. last_name = forms.CharField(strip=True, required=True)
  79. pin = forms.CharField(strip=True, required=True, validators=[numeric, min_pin_length, max_pin_length, pin_blacklist],
  80. help_text="4 character numeric PIN")
  81. class TimeEntryForm(forms.Form):
  82. day_of_month = forms.ChoiceField(choices=days, widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  83. project = forms.ModelChoiceField(Project.objects.all(), required=False,
  84. label="Select a project if applicable (not required)",
  85. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  86. hours = forms.ChoiceField(required=True, choices=hours,
  87. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  88. minutes = forms.ChoiceField(required=True, choices=minutes,
  89. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  90. class SettingsForm(forms.Form):
  91. max_daily_hours = forms.CharField(
  92. required=True,
  93. validators=[max_daily_hours_length],
  94. help_text="Sets the maximum hour value for the Hours select box."
  95. )
  96. max_daily_entries = forms.CharField(
  97. required=True,
  98. help_text="Sets the maximum number of time entries for a given calendar day."
  99. )
  100. session_timeout = forms.CharField(
  101. required=True,
  102. validators=[session_timeout_length],
  103. label="Session Timeout (Minutes)",
  104. help_text="Sets the automatic logout time limit in minutes"
  105. )
  106. projects = forms.BooleanField(required=False, label="Enable Projects",
  107. help_text="Allows for time entries to be attached to projects.")