Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed default value of date fields in forms #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TimePunchForm(Form):
choices=[("In", "In"), ("Out", "Out")],
)
punch_date = DateField(
u"Date", default=datetime.today(), validators=[DataRequired()]
u"Date", default=datetime.today, validators=[DataRequired()]
)
punch_time = StringField(
u"Time (24-hour)", default="9:00", validators=[DataRequired()]
Expand All @@ -66,10 +66,10 @@ class RequestVacationForm(Form):
"""

vac_start = DateField(
u"Start Date", default=datetime.today(), validators=[DataRequired()]
u"Start Date", default=datetime.today, validators=[DataRequired()]
)
vac_end = DateField(
u"End Date", default=datetime.today(), validators=[DataRequired()]
u"End Date", default=datetime.today, validators=[DataRequired()]
)
vac_request = SubmitField("Submit Request")

Expand All @@ -82,13 +82,11 @@ class AdminFilterEventsForm(Form):
Administrators can search for clock events created by a given user between first_date and second_date.
"""

from .modules import get_time_period
from .modules import start_of_week

email = StringField("Username/Email", validators=[Optional()])
first_date = DateField(
"From", default=get_time_period("w")[0], validators=[Optional()]
)
last_date = DateField("To", default=date.today(), validators=[Optional()])
first_date = DateField("From", default=start_of_week, validators=[Optional()])
last_date = DateField("To", default=datetime.today, validators=[Optional()])
tag = SelectField("Tag", choices=tags, coerce=int, validators=[Optional()])
division = SelectField("Division", choices=divisions, validators=[Optional()])
submit = SubmitField("Filter")
Expand Down Expand Up @@ -132,7 +130,7 @@ class CreatePayRateForm(Form):
"""

email = StringField("Email", validators=[DataRequired(), Email()])
start_date = DateField("Start", default=date.today(), validators=[DataRequired()])
start_date = DateField("Start", default=datetime.today, validators=[DataRequired()])
rate = FloatField("Rate", validators=[DataRequired()])
submit = SubmitField("Create Pay Rate")

Expand Down Expand Up @@ -164,7 +162,7 @@ class AddEventForm(Form):
"""

addemail = StringField("Email", validators=[DataRequired(), Email()])
add_date = DateField(u"Date", default=datetime.today(), validators=[DataRequired()])
add_date = DateField(u"Date", default=datetime.today, validators=[DataRequired()])
add_time = StringField(
u"Time (24-hour)", default="9:00", validators=[DataRequired()]
)
Expand Down Expand Up @@ -270,10 +268,10 @@ class GenerateMultipleTimesheetsForm(Form):

emails = SelectMultipleField(choices=[], validators=[DataRequired()], coerce=str)
start_date = DateField(
u"Start Date", default=datetime.today(), validators=[DataRequired()]
u"Start Date", default=datetime.today, validators=[DataRequired()]
)
end_date = DateField(
u"End Date", default=datetime.today(), validators=[DataRequired()]
u"End Date", default=datetime.today, validators=[DataRequired()]
)
gen_timesheets = SubmitField("Generate Timesheets")

Expand Down
9 changes: 9 additions & 0 deletions app/main/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ def get_time_period(period="d"):
return interval


def start_of_week():
"""
Gets the start of the work week (Monday).
Used primarily for setting the default value of date fields.
:return: A datetime of the first work day of the current week.
"""
return datetime.today() - timedelta(days=datetime.today().weekday())


def process_time_periods(form):
"""
Runs through the possible submit buttons on AdminFilterEventsForms and UserFilterEventsForms.
Expand Down
2 changes: 1 addition & 1 deletion app/main/payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_payrate_before_or_after(email_input, start, before_or_after):
.order_by(sqlalchemy.desc(Pay.start))
.all()
)
p = p[0]
p = p[0] if p else None
# Get the first payment before the given date
else:
p = pay_query.filter(Pay.start >= start).first()
Expand Down