views.py

About this file

The file handles the report URL endpoints for the OpenRecords application.
The routes are defined to:

Handles the rendering of the reports page,
Retrieval of report data to generate the chart on the frontend.

Code Issues

  • Function get() has a long if ladder with complicated conditions combined with and and or which is difficult to follow. This can be simplified with more functions.
  •   
    
      if agency_ein and user_guid == '':
          if agency_ein == 'all':
              active_requests = Requests.query.with_entities(Requests.status).join(
                  Agencies, Requests.agency_ein == Agencies.ein).filter(
                  Agencies.is_active).all()
              requests_closed = len([r for r in active_requests if r[0] == request_status.CLOSED])
              requests_opened = len(active_requests) - requests_closed
          else:
              active_requests = Requests.query.with_entities(Requests.status).join(
                  Agencies, Requests.agency_ein == Agencies.ein).filter(
                  Agencies.ein == agency_ein, Agencies.is_active).all()
              requests_closed = len([r for r in active_requests if r[0] == request_status.CLOSED])
              requests_opened = len(active_requests) - requests_closed
              if not (current_user.is_anonymous or current_user.is_public):
                  if (current_user.is_agency and current_user.is_agency_admin(agency_ein)) or current_user.is_super:
                      is_visible = True
                      if current_user.is_agency_admin(agency_ein) or current_user.is_super:
                          active_users = sorted(
                              [(user.guid, user.name)
                               for user in Agencies.query.filter_by(ein=agency_ein).one().active_users],
                              key=lambda x: x[1])
                      elif current_user.is_agency_active(agency_ein):
                          active_users = [(current_user.guid, current_user.name)]
                      if active_users:
                          active_users.insert(0, ('', ''))
                          results = True
    
      elif user_guid and (current_user.is_agency_active(agency_ein) or
                          current_user.is_agency_admin(agency_ein) or
                          current_user.is_super):
          is_visible = True
          ureqs = UserRequests.query.filter(UserRequests.user_guid == user_guid
                                            ).all()
    
          requests_closed = len([u for u in ureqs if u.request.status == request_status.CLOSED])
          requests_opened = len([u for u in ureqs if u.request.status != request_status.CLOSED])
    
      
      
  • As seen above, requests_closed and requests_opened are reused and can be called in a single function.
  • Also the queries should be explained at least in one line about what exactly is filtered and why is key=lambda x: x[1] used to sort active_users. There should be a comment about this.
    (It's usage information can be found here.)

Code Check Report


No problems to report
                        

Documentation drawn from source code


.. module:: report.views.

:synopsis: Handles the report URL endpoints for the OpenRecords application


show_report():

This function handles the rendering of the reports page

:return: redirect to reports page

get():

This function handles the retrieval of report data to generate the chart on the frontend.
Takes in agency_ein or user_guid from the frontend and filters for the number of requests closed and requests
opened.

:return: json object({"labels": ["Opened", "Closed"],
values": [150, 135],
active_users": [('', ''), ('o8pj0k', 'John Doe')]}), 200

acknowledgment():
Generates the acknowledgment report.

Returns:
Template with context.


monthly_metrics_report():
Generates the monthly metrics report.

Returns:
Template with context.

Source code