utils.py
About this file
This module handles the functions for requests to:
- Create a new FOIL Request and associated Users, UserRequests, and Events.
- Get mailing address from form data.
- Try to store and scan an uploaded file when no request id has been generated.
- Save an upload file to the quarantine directory, with this directory being the file's immediate parent.
- Move an approved upload to the upload directory.
- Generates an agency-specific FOIL request id.
- Generate HTML for rich-text emails.
- Generates a GUID for an anonymous user.
- Sends out a confirmation email to requester and bcc the agency default email associated with the request.
- Creates user_requests entries for agency administrators.
- Creates Users, Emails, and Events entries for a contact submission for a request. Sends email with message to all agency users associated with the request.
Code Issues
-
The function
create_requestis over 200 lines long. It should be broken up into smaller functions for readability and unit testing. The numbered comments are the logical places to break up the code: turn each numbered comment into a function call. -
In the following code, the same call is being made in the
ifand theelsewith slightly different parameters:
Instead, the parameters should be set in theif requester_email: safely_send_and_add_email( request.id, email_content, subject, to=[requester_email], bcc=bcc, ) # otherwise send the email directly to the agency else: safely_send_and_add_email( request.id, email_content, subject, to=[agency.default_email], )ifand theelse, then the call made in one place. -
For the following line:
a constant string should be defined, to increase re-usability and ease possible ports to other languages.body = "Name: {} {}\n\nEmail: {}\n\nSubject: {}\n\nMessage:\n{}".format(
Code Check Report
app/request/utils.py:41:1: F401 'app.lib.date_utils.get_following_date' imported but unused
app/request/utils.py:41:1: F401 'app.lib.date_utils.is_business_day' imported but unused
app/request/utils.py:387:121: E501 line too long (135 > 120 characters)
Documentation drawn from source code
app.request.utils~~~~~~~~~~~~~~~~synopsis: Handles the functions for requests
create_request(title,Creates a new FOIL Request and associated Users, UserRequests, and Events.:param title: request title:param description: detailed description of the request:param tz_name: client's timezone name:param agency_ein: agency_ein selected for the request:param first_name: first name of the requester:param last_name: last name of the requester:param submission: request submission method:param agency_date_submitted_local: submission date chosen by agency:param email: requester's email address:param user_title: requester's organizational title:param organization: requester's organization:param phone: requester's phone number:param fax: requester's fax number:param address: requester's mailing address:param upload_path: file path of the validated upload:param custom_metadata: JSON containing all data from custom request forms
get_address(form):Get mailing address from form data.:type form: app.request.forms.AgencyUserRequestFormapp.request.forms.AnonymousRequestForm
handle_upload_no_id(file_field):Try to store and scan an uploaded file when no request idhas been generated. Return the stored upload file pathon success, otherwise add errors to the file field.:param file_field: form file field:return: the file path to the stored upload
_quarantine_upload_no_id(upload_file):Save an upload file to the quarantine directory, withthis directory being the file's immediate parent.This file should not exist after the completion of thecreate-request pipeline and is therefore treated explicitlyas temporary (its name is prefixed with an indicator).:type upload_file: werkzeug.datastructures.FileStorage:return: the file path to the quarantined upload
_move_validated_upload(request_id, tmp_path):Move an approved upload to the upload directory.:param request_id: the id of the request associated with the upload:param tmp_path: the temporary file path to the uploadgenerated by app.request.utils._quarantine_upload_no_id()
generate_request_id(agency_ein):Generates an agency-specific FOIL request id.:param agency_ein: agency_ein ein used to generate the request_id:return: generated FOIL Request ID (FOIL - year - agency ein - 5 digits for request number)
generate_email_template(template_name, **kwargs):Generate HTML for rich-text emails.:param template_name: specific email template:param kwargs::return: email template
generate_guid():Generates a GUID for an anonymous user.:return: the generated id
send_confirmation_email(request, agency, user):Sends out a confirmation email to requester and bcc the agency default email associated with the request.Also calls the add_email function to create a Emails object to be stored in the database.:param request: Requests object containing the new created request:param agency: Agencies object containing the agency of the new request:param user: Users object containing the user who created the request
_create_agency_user_requests(request_id, agency_admins, guid_for_event):Creates user_requests entries for agency administrators.:param request_id: Request being created:param agency_users: List of Users:param guid_for_event: guid used to create request events:return:
create_contact_record(request, first_name, last_name, email, subject, message):Creates Users, Emails, and Events entries for a contact submission for a request.Sends email with message to all agency users associated with the request.:param request: request object:param first_name: sender's first name:param last_name: sender's last name:param email: sender's email:param subject: subject of email:param message: email body