def timesheet(): """ Calculate the total amount of lesson time. """ # Create new form objects for minimum and maximum dates. select_date_form = SelectMinMaxDateForm() # Set lessons and hours worked to None. time_sheet_lessons = time_sheet_time = None if request.method == 'POST' and select_date_form.validate_on_submit(): # Set the min and max dates to the dates on the form. min_date = select_date_form.min_date.data max_date = select_date_form.max_date.data else: # This month. min_date = datetime.now() - timedelta(days=30) max_date = datetime.now() # Set the form defaults to these dates. select_date_form.min_date.default = min_date select_date_form.max_date.default = max_date # Process the form to update. select_date_form.process() # Select the lessons time_sheet_lessons = generate_timesheet( current_user, min_date, max_date ) # Total the seconds worked. time_sheet_time = total_time(time_sheet_lessons) # Return the template with the data. return render_template( 'tutor/timesheet.html', time_sheet_lessons=time_sheet_lessons, time_sheet_time=time_sheet_time, select_date_form=select_date_form )