forms.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. minutes = (
  32. ('0', '00'),
  33. ('0.25', '15'),
  34. ('0.50', '30'),
  35. ('0.75', '45')
  36. )
  37. class LoginForm(forms.Form):
  38. pin = forms.CharField(strip=True, widget=forms.PasswordInput(attrs={
  39. 'class': 'form-control form-control-lg p-4 text-center',
  40. 'id': 'pin',
  41. 'placeholder': 'PIN',
  42. }), validators=[numeric])
  43. class CreateUserForm(forms.Form):
  44. first_name = forms.CharField(strip=True, required=True)
  45. last_name = forms.CharField(strip=True, required=True)
  46. pin = forms.CharField(strip=True, required=True, validators=[numeric, min_pin_length, max_pin_length, pin_blacklist],
  47. help_text="4 character numeric PIN")
  48. class TimeEntryForm(forms.Form):
  49. project = forms.ModelChoiceField(Project.objects.all(), required=False,
  50. label="Select a project if applicable (not required)",
  51. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  52. hours = forms.ChoiceField(required=True, choices=hours,
  53. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  54. minutes = forms.ChoiceField(required=True, choices=minutes,
  55. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  56. class PastTimeEntryForm(forms.Form):
  57. def __init__(self, *args, **kwargs):
  58. self.month = kwargs.pop('month')
  59. self.year = kwargs.pop('year')
  60. super(PastTimeEntryForm, self).__init__(*args, **kwargs)
  61. def number_of_days(self):
  62. days = []
  63. r = monthrange(self.year, self.month)
  64. print(r)
  65. return days
  66. number_of_days
  67. day_of_month = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  68. project = forms.ModelChoiceField(Project.objects.all(), required=False,
  69. label="Select a project if applicable (not required)",
  70. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  71. hours = forms.ChoiceField(required=True, choices=hours,
  72. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  73. minutes = forms.ChoiceField(required=True, choices=minutes,
  74. widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
  75. class SettingsForm(forms.Form):
  76. max_daily_hours = forms.CharField(
  77. required=True,
  78. validators=[max_daily_hours_length],
  79. help_text="Sets the maximum hour value for the Hours select box."
  80. )
  81. max_daily_entries = forms.CharField(
  82. required=True,
  83. help_text="Sets the maximum number of time entries for a given calendar day."
  84. )
  85. session_timeout = forms.CharField(
  86. required=True,
  87. validators=[session_timeout_length],
  88. label="Session Timeout (Minutes)",
  89. help_text="Sets the automatic logout time limit in minutes"
  90. )
  91. projects = forms.BooleanField(required=False, label="Enable Projects",
  92. help_text="Allows for time entries to be attached to projects.")