Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/application/serializers/application_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def get_embed(self, with_valid=True, params=None):

def get_query_api_input(self, application, params):
query = ''
is_asker = False
if application.work_flow is not None:
work_flow = application.work_flow
if work_flow is not None:
Expand All @@ -333,8 +334,10 @@ def get_query_api_input(self, application, params):
if input_field_list is not None:
for field in input_field_list:
if field['assignment_method'] == 'api_input' and field['variable'] in params:
if field['variable'] == 'asker':
is_asker = True
query += f"&{field['variable']}={params[field['variable']]}"
if 'asker' in params:
if 'asker' in params and not is_asker:
query += f"&asker={params.get('asker')}"
return query

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has a few potential issues and can be optimized:

Potential Issues:

  1. Variable Scope: The is_asker variable is declared inside the if application.work_flow is not None: block but is used outside of it when checking 'asker', which might lead to unexpected behavior.

  2. Logic for API Input Variable: The logic to determine if each input field should include an API request ('api_input') relies solely on the value assigned at runtime, which could cause issues if values change dynamically after initialization.

  3. Redundant Code: There is redundant handling for "asker" in both the inner loop and the outer condition when determining whether to append it to the query parameters.

  4. String Formatting: Use Python's formatted string literals (f-strings) instead of concatenation with +, which improves readability and performance.

Optimization Suggestions:

  1. Initialize is_asker Outside Block: Initialize is_asker before entering the critical section where its usage depends on external conditions.

  2. Avoid Redundant Checks: Combine checks for 'asker' into one statement using logical OR (or). This eliminates redundancy in appending "asker" to the query string.

Here is the revised version of the method:

def get_query_api_input(self, application, params):
    query = ''
    # Initialize is_asker outside the critical section
    is_asker = False

    if application.work_flow is not None:
        work_flow = application.work_flow
        if work_flow is not None:
            input_field_list = [field for field in work_flow.input_fields 
                                if field.assignment_method == 'api_input']
            if input_field_list:
                for field in input_field_list:
                    if field.variable in params:
                        if field.variable == 'asker':
                            is_asker = True
                        
                        query += f"&{field.variable}={params[field.variable]}"
    
    # Append asker only if it wasn't already added due to dynamic value assignment
    if 'asker' in params and not is_asker:
       query += f"&asker={params.get('asker', '')}"

    return query

Explanation of Changes:

  1. Initialization: Added is_asker = False line above any conditional blocks where it will be used.

  2. Dynamic Value Handling: Combined the check for 'asker' into a single condition within the for loop to avoid redundant operations.

  3. F-string Usage: Replaced manual concatenation with f-string formatting for improved clarity and efficiency. Also ensured that non-existent key retrieval uses .get() with a default value of an empty string, preventing exceptions.

Expand Down
Loading