def setnewpass(reset_key): """Set the user's new password Update the user's password if they entered a new one correctly. Arguments: reset_key - the unique key for resetting the password """ user = PasswordReset.get_by_key(reset_key) if not user: flash( "That is not a valid reset_key. That key may have expired.", "alert-warning", ) return redirect(url_for('default.home')) form = NewPassForm() if form.validate_on_submit(): password = form.password.data for pw_reset in user.pw_reset: db.session.delete(pw_reset) user.set_password(password) db.session.commit() flash("Password reset!", "alert-success") return redirect(url_for('default.home')) else: flash_form_errors(form) return render_template('auth/setnewpass.html', form=form, reset_key=reset_key)
def create_chart(): """Create a new chart If the user successfully submitted the form, add the new chart to the db, commit the session, and redirect the user to the list of their charts. Otherwise, render the template to show the user the create chart page. """ LocalCreateChartForm = CreateChartForm.get_instance() for department in Department.query.all(): if len(department.fields) > 0: LocalCreateChartForm.add_department(department) form = LocalCreateChartForm() form.user_id.data = current_user.id form.chart_type.choices = [(ctype.id, ctype.name.upper()) for ctype in ChartType.query.all()] if form.validate_on_submit(): # Add the new chart to the database db.session.add(form.chart) db.session.commit() return redirect(url_for('reports.my_charts')) else: flash_form_errors(form) return render_template('reports/create_chart.html', form=form)
def resetpass(): """Reset the user's password If the user successfully submitted the form, send a password reset email. Otherwise, render the reset form again. """ form = ForgotForm() if form.validate_on_submit(): email = form.email.data pw_reset = PasswordReset(user=User.get_by_email(email), ) db.session.add(pw_reset) db.session.commit() #Send email here title = 'Reset your Password' url = "{site}/auth/setnewpass/{key}".format( site=os.environ['DLI_REPORTS_SITE_URL'], key=pw_reset.key, ) content = 'Click this link to reset your password: {url}'.format( url=url) content += '\nThis link will expire in 7 days!' msg = Message(title, recipients=[email]) msg.body = content mail.send(msg) flash("Email sent!", "alert-success") return redirect(url_for('default.home')) else: flash_form_errors(form) return render_template('auth/resetpass.html', form=form)
def create_chart(): """Create a new chart If the user successfully submitted the form, add the new chart to the db, commit the session, and redirect the user to the list of their charts. Otherwise, render the template to show the user the create chart page. """ LocalCreateChartForm = CreateChartForm.get_instance() for department in Department.query.all(): if len(department.fields) > 0: LocalCreateChartForm.add_department(department) form = LocalCreateChartForm() form.user_id.data = current_user.id form.chart_type.choices = [ (ctype.id, ctype.name.upper()) for ctype in ChartType.query.all() ] if form.validate_on_submit(): # Add the new chart to the database db.session.add(form.chart) db.session.commit() return redirect(url_for('reports.my_charts')) else: flash_form_errors(form) return render_template('reports/create_chart.html', form=form)
def edit_report(report_id): """Edit the specified report""" report = Report.query.get(report_id) if report is None: flash( "Report not found!", "alert-warning", ) elif not current_user.is_admin and not report.user.id == current_user.id: flash( "You don't have permission to edit that.", "alert-warning", ) else: LocalEditReportForm = EditReportForm.get_instance() for department in Department.query.all(): if len(department.fields) > 0: LocalEditReportForm.add_department(department) form = LocalEditReportForm() if form.validate_on_submit(): flash('Report: {name} has been updated'.format(name=form.report.name), 'alert-success') db.session.commit() return redirect(url_for('reports.my_reports')) else: flash_form_errors(form) form.name.data = report.name form.report_id.data = report_id form.tags.data = ', '.join(tag.name for tag in report.tags) for department in Department.query.all(): if len(department.fields) > 0: set_fields = [field for field in report.fields if field.department.id == department.id] getattr(form, department.name).data = [f.id for f in set_fields] return render_template('reports/edit.html', form=form, report=report)
def edit(): """Edit the user's account settings""" form = EditAccountForm() form.user_id.data = current_user.id # Dynamically load the location and department choices form.location.choices = [ (location.id, location.name) for location in Location.query.all() ] form.department.choices = [ (dept.id, dept.name) for dept in Department.query.all() ] if form.validate_on_submit(): db.session.commit() flash( "Your account has been updated successfully", "alert-success", ) return redirect(request.args.get('next') or url_for('account.home')) else: flash_form_errors(form) # Set the form defaults form.name.data = current_user.name form.location.default = current_user.location.id form.department.default = current_user.department.id return render_template('account/edit.html', form=form)
def edit_page(page_name=''): """Edit a specific page of the wiki""" page = WikiPage.query.filter_by(name=page_name).first() form = EditWikiPageForm() if form.validate_on_submit(): page = WikiPage.query.filter_by(name=form.name.data).first() # Update the WikiPage's information if page is not None: page.name = form.page.name page.content = form.page.content page.modtime = datetime.datetime.now().strftime( '%m/%d/%Y %I:%M %p') page.editor = current_user.name flash( "WikiPage updated successfully", "alert-success", ) # Create a new WikiPage else: db.session.add(form.page) flash( "WikiPage added successfully", "alert-success", ) db.session.commit() return redirect(url_for('wiki.view_page', page_name=form.page.name)) else: flash_form_errors(form) if page is not None: form.name.data = page.name form.content.data = page.content return render_template('wiki/edit.html', form=form)
def resetpass(): """Reset the user's password If the user successfully submitted the form, send a password reset email. Otherwise, render the reset form again. """ form = ForgotForm() if form.validate_on_submit(): email = form.email.data pw_reset = PasswordReset( user=User.get_by_email(email), ) db.session.add(pw_reset) db.session.commit() #Send email here title = 'Reset your Password' url = "{site}/auth/setnewpass/{key}".format( site=os.environ['DLI_REPORTS_SITE_URL'], key=pw_reset.key, ) content = 'Click this link to reset your password: {url}'.format(url=url) content += '\nThis link will expire in 7 days!' msg = Message(title, recipients=[email]) msg.body = content mail.send(msg) flash("Email sent!", "alert-success") return redirect(url_for('default.home')) else: flash_form_errors(form) return render_template('auth/resetpass.html', form=form)
def edit_page(page_name=''): """Edit a specific page of the wiki""" page = WikiPage.query.filter_by(name=page_name).first() form = EditWikiPageForm() if form.validate_on_submit(): page = WikiPage.query.filter_by(name=form.name.data).first() # Update the WikiPage's information if page is not None: page.name = form.page.name page.content = form.page.content page.modtime = datetime.datetime.now().strftime('%m/%d/%Y %I:%M %p') page.editor = current_user.name flash( "WikiPage updated successfully", "alert-success", ) # Create a new WikiPage else: db.session.add(form.page) flash( "WikiPage added successfully", "alert-success", ) db.session.commit() return redirect(url_for('wiki.view_page', page_name=form.page.name)) else: flash_form_errors(form) if page is not None: form.name.data = page.name form.content.data = page.content return render_template('wiki/edit.html', form=form)
def search(): """Search for reports that contains a keyword in owner,name,tag,department,location""" form = SearchForm() if form.validate_on_submit(): return render_template('reports/search_results.html', reports=form.reports) else: flash_form_errors(form) return render_template('reports/search.html', form=form)
def search(): """Search for a specific wiki with keyword""" form = SearchForm() if form.validate_on_submit(): # Show the user the list of results (form.results maybe?) return render_template('wiki/search.html', form=form, results=form.results) else: flash_form_errors(form) return render_template('wiki/home.html', form=form)
def login(): """Login the user If the user successfully submitted the form, log them in. Otherwise, render the login form for the user to input their information. """ form = LoginForm() if form.validate_on_submit(): # User has authenticated. Log in. login_user(form.user, remember=form.remember.data) flash("You are now logged into DLI-Reports", "alert-success") return redirect(request.args.get('next') or url_for('default.home')) else: flash_form_errors(form) return render_template('auth/login.html', form=form)
def edit_chart(chart_id): """Edit the specified chart""" chart = Chart.query.get(chart_id) if chart is None: flash( "Error: Chart not found!", "alert-warning", ) elif not current_user.is_admin and not chart.user.id == current_user.id: flash( "You don't have permission to delete that.", "alert-warning", ) else: LocalEditChartForm = EditChartForm.get_instance() for department in Department.query.all(): if len(department.fields) > 0: LocalEditChartForm.add_department(department) form = LocalEditChartForm() form.chart_type.choices = [(ctype.id, ctype.name.upper()) for ctype in ChartType.query.all()] if form.validate_on_submit(): flash( 'Chart: {name} has been updated'.format(name=form.chart.name), 'alert-success') db.session.commit() return redirect(url_for('reports.my_charts')) else: flash_form_errors(form) form.chart_id.data = chart_id form.name.data = chart.name form.chart_type.data = chart.ctype.id form.with_table.data = chart.with_table form.tags.data = ', '.join(tag.name for tag in chart.tags) for department in Department.query.all(): if len(department.fields) > 0: set_fields = [ field for field in chart.fields if field.department.id == department.id ] getattr(form, department.name).data = [f.id for f in set_fields] return render_template('reports/edit_chart.html', form=form, chart=chart)
def edit_fields(): """Render the field editing page First perform a check to ensure the user is an admin. Load the "Add Field" form. If the user has submitted a new field, add it to the db and commit the session before redirecting the user to view the list of all fields. Otherwise, show the user the list of all fields. """ if not current_user.is_admin: flash( "Sorry! You don't have permission to access that page.", "alert-warning", ) return redirect(url_for('default.home')) form = AddFieldForm() # Dynamically load the department and type choices form.department.choices = [ (dept.id, dept.name) for dept in Department.query.all() ] form.field_type.choices = [ (ftype.id, ftype.name.upper()) for ftype in FieldType.query.all() ] if form.validate_on_submit(): db.session.add(form.field) db.session.commit() flash( "Field added successfully", "alert-success", ) return redirect(url_for('admin.edit_fields')) else: flash_form_errors(form) return render_template( 'admin/edit_fields.html', form=form, departments=Department.query.all(), )
def edit_report(report_id): """Edit the specified report""" report = Report.query.get(report_id) if report is None: flash( "Report not found!", "alert-warning", ) elif not current_user.is_admin and not report.user.id == current_user.id: flash( "You don't have permission to edit that.", "alert-warning", ) else: LocalEditReportForm = EditReportForm.get_instance() for department in Department.query.all(): if len(department.fields) > 0: LocalEditReportForm.add_department(department) form = LocalEditReportForm() if form.validate_on_submit(): flash( 'Report: {name} has been updated'.format( name=form.report.name), 'alert-success') db.session.commit() return redirect(url_for('reports.my_reports')) else: flash_form_errors(form) form.name.data = report.name form.report_id.data = report_id form.tags.data = ', '.join(tag.name for tag in report.tags) for department in Department.query.all(): if len(department.fields) > 0: set_fields = [ field for field in report.fields if field.department.id == department.id ] getattr(form, department.name).data = [f.id for f in set_fields] return render_template('reports/edit.html', form=form, report=report)
def edit_fields(): """Render the field editing page First perform a check to ensure the user is an admin. Load the "Add Field" form. If the user has submitted a new field, add it to the db and commit the session before redirecting the user to view the list of all fields. Otherwise, show the user the list of all fields. """ if not current_user.is_admin: flash( "Sorry! You don't have permission to access that page.", "alert-warning", ) return redirect(url_for('default.home')) form = AddFieldForm() # Dynamically load the department and type choices form.department.choices = [(dept.id, dept.name) for dept in Department.query.all()] form.field_type.choices = [(ftype.id, ftype.name.upper()) for ftype in FieldType.query.all()] if form.validate_on_submit(): db.session.add(form.field) db.session.commit() flash( "Field added successfully", "alert-success", ) return redirect(url_for('admin.edit_fields')) else: flash_form_errors(form) return render_template( 'admin/edit_fields.html', form=form, departments=Department.query.all(), )
def register(registration_key): """Register a new user If the user successfully submitted the form, add the new user to the db, commit the session, and login the user before redirecting to the home page. Otherwise, render the template to show the user the registration page. Arguments: registration_key - the unique key for registering the user's account """ candidate = RegisterCandidate.query.filter_by( registration_key=registration_key).first() if not candidate: flash("Sorry, you aren't allowed to register at this time.", "alert-warning") return redirect(url_for('default.home')) form = RegistrationForm() form.registration_key.data = registration_key form.location.choices = [(location.id, location.name) for location in Location.query.all()] form.department.choices = [(department.id, department.name) for department in Department.query.all()] if form.validate_on_submit(): db.session.add(form.user) db.session.commit() candidate = RegisterCandidate.query.filter_by( email=form.user.email).first() if candidate: db.session.delete(candidate) db.session.commit() # Log the user in and redirect to the homepage login_user(form.user, form.remember.data) flash("You have created a new account at DLI-Reports", "alert-success") return redirect(request.args.get('next') or url_for('default.home')) else: flash_form_errors(form) form.email.data = candidate.email return render_template('auth/register.html', form=form, registration_key=registration_key)
def edit_users(page_num=1): """Render the user editing page First perform a check to ensure the user is an admin. Load the "Add User" form. If the user has submitted a new user, add it to the db and commit the session before redirecting the user to view the list of all users. Otherwise, show the user the list of all users. """ if not current_user.is_admin: flash( "Sorry! You don't have permission to access that page.", "alert-warning", ) return redirect(url_for('default.home')) form = AddUserForm() if form.validate_on_submit(): db.session.add(form.user) db.session.commit() candidate = RegisterCandidate.query.filter_by( email=form.user.email).first() if candidate is not None: candidate.send_link() flash( "Sent an invite link to {email}".format(email=form.user.email), "alert-success", ) return redirect(url_for('admin.edit_users')) else: # Get a list of users users = User.query.paginate(page_num) candidates = RegisterCandidate.query.all() flash_form_errors(form) return render_template( 'admin/edit_users.html', form=form, users=users, candidates=candidates, )
def bugsplat(error=None): """Default handler page for reporting bugs or feature requests""" form = ErrorReportForm() form.user_id.data = current_user.id if form.validate_on_submit(): db.session.add(form.error_report) db.session.commit() flash( "Thank you for your report! We will look at this as soon as possible.", "alert-success", ) return redirect(url_for('default.home')) else: flash_form_errors(form) form.error.data = error form.report_type.data = 0 if error: form.report_type.data = 1 return render_template('admin/bugsplat.html', form=form)
def edit_users(page_num=1): """Render the user editing page First perform a check to ensure the user is an admin. Load the "Add User" form. If the user has submitted a new user, add it to the db and commit the session before redirecting the user to view the list of all users. Otherwise, show the user the list of all users. """ if not current_user.is_admin: flash( "Sorry! You don't have permission to access that page.", "alert-warning", ) return redirect(url_for('default.home')) form = AddUserForm() if form.validate_on_submit(): db.session.add(form.user) db.session.commit() candidate = RegisterCandidate.query.filter_by(email=form.user.email).first() if candidate is not None: candidate.send_link() flash( "Sent an invite link to {email}".format(email=form.user.email), "alert-success", ) return redirect(url_for('admin.edit_users')) else: # Get a list of users users = User.query.paginate(page_num) candidates = RegisterCandidate.query.all() flash_form_errors(form) return render_template( 'admin/edit_users.html', form=form, users=users, candidates=candidates, )
def question(): """Email administrators""" form = AskQuestionForm() if form.validate_on_submit(): # Send email to administrators users = [u.email for u in User.query.filter_by(is_admin=True)] emailtitle = form.emailtitle.data content = form.content.data msg = Message(emailtitle, recipients=users, reply_to=form.email.data) msg.body = '{notice}\n{content}'.format( notice= ('A new question was asked concerning the online DLI policies Wiki. ' 'Please reply to this email to answer the question.'), content=content, ) mail.send(msg) flash("Email Sent!", "alert-success") return redirect(url_for('wiki.home')) else: flash_form_errors(form) return render_template('wiki/question.html', form=form)
def edit_chart(chart_id): """Edit the specified chart""" chart = Chart.query.get(chart_id) if chart is None: flash( "Error: Chart not found!", "alert-warning", ) elif not current_user.is_admin and not chart.user.id == current_user.id: flash( "You don't have permission to delete that.", "alert-warning", ) else: LocalEditChartForm = EditChartForm.get_instance() for department in Department.query.all(): if len(department.fields) > 0: LocalEditChartForm.add_department(department) form = LocalEditChartForm() form.chart_type.choices = [ (ctype.id, ctype.name.upper()) for ctype in ChartType.query.all() ] if form.validate_on_submit(): flash('Chart: {name} has been updated'.format(name=form.chart.name), 'alert-success') db.session.commit() return redirect(url_for('reports.my_charts')) else: flash_form_errors(form) form.chart_id.data = chart_id form.name.data = chart.name form.chart_type.data = chart.ctype.id form.with_table.data = chart.with_table form.tags.data = ', '.join(tag.name for tag in chart.tags) for department in Department.query.all(): if len(department.fields) > 0: set_fields = [field for field in chart.fields if field.department.id == department.id] getattr(form, department.name).data = [f.id for f in set_fields] return render_template('reports/edit_chart.html', form=form, chart=chart)
def download_report(report_id): """Download a report as an Excel file""" report = Report.query.get(report_id) if report is None: flash("Report not found!", "alert-warning") return redirect(url_for('reports.my_reports')) form = DownloadReportForm() if form.validate_on_submit(): if not report.excel_file_exists(form.start, form.end): report.create_excel_file(form.start, form.end) return send_file( report.excel_filepath_for_ds(form.start, form.end), as_attachment=True, ) else: flash_form_errors(form) return render_template( 'reports/download.html', form=form, report=report, )
def register(registration_key): """Register a new user If the user successfully submitted the form, add the new user to the db, commit the session, and login the user before redirecting to the home page. Otherwise, render the template to show the user the registration page. Arguments: registration_key - the unique key for registering the user's account """ candidate = RegisterCandidate.query.filter_by(registration_key=registration_key).first() if not candidate: flash("Sorry, you aren't allowed to register at this time.", "alert-warning") return redirect(url_for('default.home')) form = RegistrationForm() form.registration_key.data = registration_key form.location.choices = [ (location.id, location.name) for location in Location.query.all() ] form.department.choices = [ (department.id, department.name) for department in Department.query.all() ] if form.validate_on_submit(): db.session.add(form.user) db.session.commit() candidate = RegisterCandidate.query.filter_by(email=form.user.email).first() if candidate: db.session.delete(candidate) db.session.commit() # Log the user in and redirect to the homepage login_user(form.user, form.remember.data) flash("You have created a new account at DLI-Reports", "alert-success") return redirect(request.args.get('next') or url_for('default.home')) else: flash_form_errors(form) form.email.data = candidate.email return render_template('auth/register.html', form=form, registration_key=registration_key)
def question(): """Email administrators""" form = AskQuestionForm() if form.validate_on_submit(): # Send email to administrators users = [u.email for u in User.query.filter_by(is_admin=True)] emailtitle = form.emailtitle.data content = form.content.data msg = Message(emailtitle, recipients=users, reply_to=form.email.data) msg.body = '{notice}\n{content}'.format( notice=( 'A new question was asked concerning the online DLI policies Wiki. ' 'Please reply to this email to answer the question.' ), content=content, ) mail.send(msg) flash("Email Sent!", "alert-success") return redirect(url_for('wiki.home')) else: flash_form_errors(form) return render_template('wiki/question.html', form=form)
def edit_locations(): """Render the location editing page First perform a check to ensure the user is an admin. Load the "Add Location" form. If the user has submitted a new location, add it to the db and commit the session before redirecting the user to view the list of all locations. Otherwise, show the user the list of all locations. """ if not current_user.is_admin: flash( "Sorry! You don't have permission to access that page.", "alert-warning", ) return redirect(url_for('default.home')) form = AddLocationForm() if form.validate_on_submit(): db.session.add(form.location) db.session.commit() flash( "Location added successfully", "alert-success", ) return redirect(url_for('admin.edit_locations')) else: # Get a list of all locations locations = Location.query.all() flash_form_errors(form) return render_template( 'admin/edit_locations.html', form=form, locations=locations, )
def setnewpass(reset_key): """Set the user's new password Update the user's password if they entered a new one correctly. Arguments: reset_key - the unique key for resetting the password """ user = PasswordReset.get_by_key(reset_key) if not user: flash("That is not a valid reset_key. That key may have expired.", "alert-warning",) return redirect(url_for('default.home')) form = NewPassForm() if form.validate_on_submit(): password = form.password.data for pw_reset in user.pw_reset: db.session.delete(pw_reset) user.set_password(password) db.session.commit() flash("Password reset!", "alert-success") return redirect(url_for('default.home')) else: flash_form_errors(form) return render_template('auth/setnewpass.html', form=form, reset_key=reset_key)
def submit_report_data(report_id, ds=None, dept_id=None): """Submit new report data If the user successfully submitted the form, submit all of the report data, commit the session, and redirect the user to view their reports. Otherwise, render the template to show the user the report data submission form. """ if not ds or ds == 'today': ds = datetime.now().strftime('%Y-%m-%d') elif ds == 'yesterday': ds = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') # Check to see if the user picked a different day or department change_form = ChangeDateAndDepartmentForm() if change_form.validate_on_submit() and change_form.department.data: return redirect( url_for( 'reports.submit_report_data', report_id=report_id, ds=change_form.ds, dept_id=change_form.dept_id, ) ) report = Report.query.get(report_id) # We must generate the dynamic form before loading it if not dept_id: dept_id = current_user.department.id department = Department.query.get(dept_id) if not department: flash( "No department with that ID found.", "alert-warning", ) return redirect(url_for('reports.submit_report_data', report_id=report_id)) LocalSubmitReportDataForm = SubmitReportDataForm.get_instance() for field in report.fields: if field.department_id == dept_id: LocalSubmitReportDataForm.add_field(field) # *Now* the form is properly initialized form = LocalSubmitReportDataForm() if form.validate_on_submit() and form.ds.data: # Delete any old values that already existed for stale_value in form.stale_values: db.session.delete(stale_value) # Also "invalidate" any existing Excel sheets that used this data for data_point in form.data_points: for report in data_point.field.reports: report.remove_excel_files() # Add all of the new data points db.session.add_all(form.data_points) db.session.commit() flash( "Report data successfully submitted.", "alert-success", ) return redirect(url_for('reports.all_reports')) else: flash_form_errors(change_form) flash_form_errors(form) form.ds.data = ds # Set the change_form defaults change_form.date.data = datetime.strptime(ds, "%Y-%m-%d") change_form.department.data = dept_id or current_user.department.id for field in form.instance_fields: # This line allows us to dynamically load the field data formfield = getattr(form, field.name) if not formfield.data: existing_value = field.data_points.filter_by(ds=ds).first() if existing_value: formfield.data = existing_value.pretty_value chunk_size = 10 field_list = form.instance_fields chunked_fields = [field_list[n:n+chunk_size] for n in range(0, len(field_list), chunk_size)] return render_template( 'reports/submit_data.html', change_form=change_form, form=form, chunked_fields=chunked_fields, report=report, department=department, ds=ds, )
def submit_report_data(report_id, ds=None, dept_id=None): """Submit new report data If the user successfully submitted the form, submit all of the report data, commit the session, and redirect the user to view their reports. Otherwise, render the template to show the user the report data submission form. """ if not ds or ds == 'today': ds = datetime.now().strftime('%Y-%m-%d') elif ds == 'yesterday': ds = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') # Check to see if the user picked a different day or department change_form = ChangeDateAndDepartmentForm() if change_form.validate_on_submit() and change_form.department.data: return redirect( url_for( 'reports.submit_report_data', report_id=report_id, ds=change_form.ds, dept_id=change_form.dept_id, )) report = Report.query.get(report_id) # We must generate the dynamic form before loading it if not dept_id: dept_id = current_user.department.id department = Department.query.get(dept_id) if not department: flash( "No department with that ID found.", "alert-warning", ) return redirect( url_for('reports.submit_report_data', report_id=report_id)) LocalSubmitReportDataForm = SubmitReportDataForm.get_instance() for field in report.fields: if field.department_id == dept_id: LocalSubmitReportDataForm.add_field(field) # *Now* the form is properly initialized form = LocalSubmitReportDataForm() if form.validate_on_submit() and form.ds.data: # Delete any old values that already existed for stale_value in form.stale_values: db.session.delete(stale_value) # Also "invalidate" any existing Excel sheets that used this data for data_point in form.data_points: for report in data_point.field.reports: report.remove_excel_files() # Add all of the new data points db.session.add_all(form.data_points) db.session.commit() flash( "Report data successfully submitted.", "alert-success", ) return redirect(url_for('reports.all_reports')) else: flash_form_errors(change_form) flash_form_errors(form) form.ds.data = ds # Set the change_form defaults change_form.date.data = datetime.strptime(ds, "%Y-%m-%d") change_form.department.data = dept_id or current_user.department.id for field in form.instance_fields: # This line allows us to dynamically load the field data formfield = getattr(form, field.name) if not formfield.data: existing_value = field.data_points.filter_by(ds=ds).first() if existing_value: formfield.data = existing_value.pretty_value chunk_size = 10 field_list = form.instance_fields chunked_fields = [ field_list[n:n + chunk_size] for n in range(0, len(field_list), chunk_size) ] return render_template( 'reports/submit_data.html', change_form=change_form, form=form, chunked_fields=chunked_fields, report=report, department=department, ds=ds, )