views.py

About this file

The user-request routes are to create a users permissions entry for a request, update the users permissions and remove a user from the request.

Code Issues

  • Docstrings for create(request_id) and edit(request_id) vaguely touches upon what the request body should have. However, it lacks a description regarding the example. What does 1 and 5 mean below?
            
            Expects a request body containing the user's guid and new permissions.
                Ex:
                {
                    'user': '7khz9y',
                    1: true,
                    5: false
                }
            
        
  • create(request_id), edit(request_id), and delete(request_id) functions have lines of code that can be extracted into separate functions
    • Recommend creating a valid_user(permission_type=None) function for the conditional
      As seen from lines 39-45
                  
                  if (
                              current_user.is_agency and (
                                          current_user.is_super or
                                          current_user.is_agency_admin(current_request.agency.ein) or
                                      current_user_request.has_permission(permission.ADD_USER_TO_REQUEST)
                          )
                  ):
                  
                  
      Because both create(request_id) and edit(request_id) use a similar conditional but have different permission types, both can refer to another function that takes in a permission type as a parameter and returns true or false.
    • Code for the for loop below can also be extracted into a separate function to reduce code repetition.
                  
                  for field in required_fields:
                      if user_data.get(field) is None:
                          flash('Uh Oh, it looks like the {} is missing! '
                              'This is probably NOT your fault.'.format(field), category='danger')
                          return redirect(url_for('request.view', request_id=request_id))
                  
                  
    • Extract exception code
                  
                  except UserRequestException as e:
                      sentry.captureException()
                      flash(str(e), category='warning')
                      return redirect(url_for('request.view', request_id=request_id))
                  
                  

Code Check Report


app/user_request/views.py:43:25: E131 continuation line unaligned for hanging indent
app/user_request/views.py:90:25: E131 continuation line unaligned for hanging indent
app/user_request/views.py:137:9: E125 continuation line with same indent as next logical line
                        

Documentation drawn from source code


...module:: user_request.views.

:synopsis: Endpoints for User Requests


create(request_id):

Creates a users permissions entry for a request and sends notification emails.

Expects a request body containing the user's guid and new permissions.
Ex:
{
user': '7khz9y',
1: true,
5: false
}
:return:

edit(request_id):

Updates a users permissions on a request and sends notification emails.

Expects a request body containing the user's guid and updated permissions.
Ex:
{
user': '7khz9y',
1: true,
5: false
}
:return:

delete(request_id):

Removes a user from a request and send notification emails.

Expects a request body containing user's guid and confirmation string.
Ex:
{
user': '7khz9y',
remove-confirmation-string': 'remove
}
:return:

Source code