stc

a simple time card webapp
git clone _git@git.brennen.work:stc.git
Log | Files | Refs | README

commit 767465e6f911f57f24929bafb636536320d0a10f
parent f138e2a796e09ea8863e5a23b7d226671953d2da
Author: Youth Employment Program Production <youthemployment22@gmail.com>
Date:   Wed,  1 May 2024 10:51:52 -0600

enforce positive hours on form validation for NewUserHours

Diffstat:
Mapp/forms.py | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mapp/templates/dashboard/punchclock/otheruser.html | 3+++
2 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/app/forms.py b/app/forms.py @@ -1,6 +1,76 @@ from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, PasswordField, BooleanField, SelectField, DateField, TelField, EmailField, FloatField, IntegerField, TimeField, DecimalField -from wtforms.validators import DataRequired, optional, length, InputRequired, EqualTo +from wtforms.validators import DataRequired, optional, length, InputRequired, EqualTo, NumberRange, ValidationError + +class GreaterThan: + """ + Compares the values of two fields. + + :param fieldname: + The name of the compared field. + :param message: + Error message to raise in case of validation error. + """ + def __init__(self, fieldname, message=None): + self.fieldname = fieldname + self.message = message + + def __call__(self, form, field): + try: + other = form[self.fieldname] + except KeyError as exc: + raise ValidationError( + field.gettext("Invalid fieldname '$s'.") % self.fieldname + ) from exc + if field.data > other.data: + return + + d = { + "other_label":hasattr(other,"label") + and other.label.text + or self.fieldname, + "other_name": self.fieldname, + } + message = self.message + if message is None: + message = field.gettext("Field must be Greater than %(other_name)s.") + + raise ValidationError(message % d) + +class LessThan: + """ + Compares the values of two fields + + :param fieldname: + The name of the field to compare to. + :param message: + Error message to raise in case of validation error. + """ + def __init__(self, fieldname, message=None): + self.fieldname = fieldname + self.message = message + + def __call__(self, form, field): + try: + other = form[self.fieldname] + except KeyError as exc: + raise ValidationError( + field.gettext("Invalid field name '%s'.") % self.fieldname + ) from exc + if field.data < other.data: + return + + d = { + "other_label":hasattr(other, "label") + and other.label.text + or self.fieldname, + "other_name": self.fieldname, + } + message = self.message + if message is None: + message = field.gettext("Field must be less than %(other_name)s.") + + raise ValidationError(message % d) # Login class currently assumes mongodb collection(Users) with structure # { @@ -110,10 +180,12 @@ class CrewClockinWidget(FlaskForm): submitEntr = SubmitField('Clock In') #clockin = SubmitField('Clock In') + class NewHoursForm(FlaskForm): dateSel = DateField('Date',validators=[DataRequired()]) projectSel = SelectField('Project',validators=[DataRequired()]) - startTime = TimeField('Start',validators=[DataRequired()]) + startTime = TimeField('Start',validators=[InputRequired(),LessThan('endTime')]) + #endTime = TimeField('End',validators=[DataRequired(),GreaterThan('startTime')]) endTime = TimeField('End',validators=[DataRequired()]) lunchSel = BooleanField('Lunch') perDiemSel = BooleanField('Per Diem') diff --git a/app/templates/dashboard/punchclock/otheruser.html b/app/templates/dashboard/punchclock/otheruser.html @@ -16,6 +16,9 @@ <form action="" method="POST" novalidate> <table> {% for field in form %}{% if field.widget.input_type != 'hidden' and field.widget.input_type != 'submit' %} + {% for error in field.errors %} + <span style="color:red;">[{{error}}]</span> + {% endfor %} <tr><td>{{ field.label }}</td><td>{{ field }}</td></tr> {% endif %}{% endfor %} </table>