def validate(inqcode, period, ruref):
    log.info('save validation button pressed')
    json_data = {
        "survey": inqcode,
        "period": period,
        "reference": ruref,
        "BPMvalidationCallId": "0"
    }
    header = {"x-api-key": api_key}
    status_message = 'Validation Run Successfully'
    try:
        response = api_caller.run_validation(url, json.dumps(json_data),
                                             header)
        log.info("Response from SQS: %s", response)
    except HTTPError as http_err:
        status_message = "Http Error. Unable to call URL"
        log.info('URL error occurred: %s', http_err)
    except ConnectionError as connection_err:
        status_message = "Connection Error. Unable to Connect to API Gateway"
        log.info('API request error occured: %s', connection_err)
    except Exception as e:
        status_message = 'Validation Error. Kubernetes secret does not exist or is incorrect'
        log.info('Validation Error Occurred: %s', e)
        return render_template(
            template_name_or_list="./error_templates/validate_error.html",
            error=e)
    return status_message
示例#2
0
def general_search_screen_selection():
    if request.method == "POST":
        log.info(request.form)
        criteria = ';'.join(i for i in request.form.keys())
        return redirect(
            url_for("contributor_search.general_search_screen",
                    criteria=criteria))
    return render_template(
        './search_screen_choice/GeneralSearchScreenChoice.html')
def general_search_screen_selection():
    log.info("general_search_screen_selection")
    if request.method == "POST":
        criteria = ";".join(i for i in request.form.keys())
        # redirect to general search screen, criteria is added as a url parameter. ?criteria=VALUE1;VALUE2
        return redirect(
            url_for("contributor_search.general_search_screen",
                    criteria=criteria))

    return render_template(
        "./search_screen_choice/GeneralSearchScreenChoice.html")
def send_notification_to_queue(reference, period, survey):

    notification_to_send = [{
        "response": str(uuid1()),
        "reference": reference,
        "period": period,
        "survey": survey,
        "duplicateFlag": False}]
    header = {"x-api-key": baw_notify_key}
    response = api_caller.run_validation(
        baw_notify_url, json.dumps(notification_to_send), header)
    log.info("Response from BAW Notify Queue: %s", response)
def send_override_notification_to_queue(override_data):
    try:
        override_json_data = validate_json(override_data)
        header = {"x-api-key": baw_notify_override_key}
        notification_to_send = build_notify_data_to_send(override_json_data=override_json_data)
        response = api_caller.notify_override(baw_notify_override_url, json.dumps(notification_to_send), header)
        log.info("Response from BPM Notify Queue: %s", response)

        ''' raise exceptions up the stack '''
    except KeyError as error:
        log.error(f'Error with override JSON data {error}')
        raise KeyError(error)
    except ValueError as error:
        log.error(f"Invalid JSON: {error}")
        raise ValueError(error)
    except requests.exceptions.RequestException as error:
        log.error(f'Error with call to Override endpoint {error}')
        raise requests.exceptions.RequestException(error)
    except Exception as error:
        raise Exception(error)
def general_search_screen():

    log.info("general_search_screen")
    criteria = request.args["criteria"].split(";")

    # Build class for the forms that are passed in, this must be done dynamically
    selection_form = create_form_class(criteria, log)
    form = selection_form(request.form)

    # On search, and if form object passes validation enter block
    current_page = -1
    last_page = -1

    # before any searches are done, return just the webpage with the search fields.
    return render_template(
        "./contributor_search/GeneralSearchScreen.html",
        form=form,
        fields=dict(form.__dict__["_fields"]),
        current_page=current_page,
        last_page=last_page,
    )
def general_search_screen_post():
    log.info("general_search_screen_post")
    criteria = request.args["criteria"].split(";")

    # Build class for the forms that are passed in, this must be done dynamically
    # create form object

    # Build class for the forms that are passed in, this must be done dynamically
    selection_form = create_form_class(criteria, log)

    # create form object
    form = selection_form(request.form)
    mutable_form = create_new_dict(request.form)
    clean_parameters = clean_search_parameters(mutable_form)

    url_connect = build_uri(clean_parameters)
    parameters = url_connect
    url_connect += ";first=10"
    log.info("url connect: {}".format(url_connect))

    contributor_data = api_caller.contributor_search(parameters=url_connect)
    data = GraphData(contributor_data)
    output_data = data.nodes
    log.info("Data from GraphQL for search: %s", output_data)

    return render_template("./contributor_search/GeneralSearchScreenGQL.html",
                           form=form,
                           records=output_data,
                           header=output_data[0],
                           fields=dict(form.__dict__["_fields"]),
                           links=data.page_info,
                           parameters=parameters)
def override_validations(inqcode, period, ruref):
    try:
        json_data = request.json
        log.info("Checkbox checked data: %s", str(json_data))
        ruref = json_data['reference']
        inqcode = json_data['survey']
        period = json_data['period']

        log.info("Overriding Validations...")
        override_data_to_send = api_caller.validation_overrides(parameters='', data=json.dumps(json_data))

        log.info("Sending override information to validation-bpmtransform queue...")
        log.info("Override_data_to_send: %s", override_data_to_send)
        send_override_notification_to_queue(override_data_to_send)

        return redirect(url_for('view_form.view_form', inqcode=inqcode, period=period, ruref=ruref))
    except Exception as error:
        raise Exception(f"Error with overriding validations: {error}")
def save_form(parameters, requestform, inqcode, period, ruref):
    try:
        response_data = extract_responses(requestform)
        log.info('Response data: %s', response_data)
        # Build up JSON structure to save
        json_output = {}
        json_output["responses"] = response_data
        json_output["user"] = get_user()
        json_output["reference"] = ruref
        json_output["period"] = period
        json_output["survey"] = inqcode

        # Send the data to the business layer for processing
        log.info("Output JSON: %s", str(json_output))
        response = api_caller.save_response(parameters=parameters,
                                            data=json_output)
        log.info("Response from save request: %s", response)
        if not response == "{\"continue\":\"No question responses to save\"}":
            log.info("Data edited. Sending notification to BAW...")
            send_notification_to_queue(ruref, period, inqcode)
        status_message = 'Responses saved successfully'
        return status_message
    except HTTPError as http_error:
        status_message = 'There was a problem with the HTTP call ' + http_error
        log.info('HTTP Error %s', http_error)
    except ConnectionError as connection_error:
        status_message = 'There was a problem with the connection ' + connection_error
        log.info('Connection Error %s', connection_error)
    except Timeout as timeout_error:
        status_message = 'Timeout error ' + timeout_error
        log.info('Timeout Error %s', timeout_error)
    except RequestException as requests_error:
        status_message = 'There was a problem with your request ' + requests_error + 'Please contact Data Clearing Support Team'
        log.info('Requests Error: %s', requests_error)
def view_form(inqcode, period, ruref):
    if (
        request.form
        and request.form['action'] == 'save-and-validate'
        and not current_app.auth.has_permission(["dev", "survey.*.write", "survey.*.manager"])
    ):
        abort(403)
    log.info("View_Form -- START --")

    log.info("Request.form: %s", request.form)

    try:
        status_message = ""
        url_parameters = dict(
            zip(["survey", "period", "reference"], [inqcode, period, ruref]))
        parameters = build_uri(url_parameters)

        contributor_details = api_caller.contributor_search(parameters=parameters)
        contributor_data = json.loads(contributor_details)
        log.info("Contributor Details: %s", contributor_data)
        log.info("Contributor Details[0]: %s", contributor_data['data'][0])

        validation_outputs = api_caller.validation_outputs(parameters=parameters)
        validations = json.loads(validation_outputs)
        log.info("Validations output: %s", validations)

        view_forms = api_caller.view_form_responses(parameters=parameters)
        view_form_data = json.loads(view_forms)
        log.info("View Form Data: %s", view_form_data)

        historic_data = api_caller.get_historic_data(parameters=parameters)
        historic_data_json = json.loads(historic_data)
        log.info("History Data: %s", historic_data_json)

        grouped_historic_data = group_historic_data(historic_data_json)
        log.info("Grouped Historic Data by question : %s", grouped_historic_data)

        status = contributor_data['data'][0]['status']
        status_colour = check_status(status)
        log.info("status colour: %s", status_colour)

        filtered_validations = filter_validations(validations)
        log.info("filtered validations: %s", filtered_validations)
        response_and_validations = combine_responses_and_validations(view_form_data, filtered_validations)
        log.info("response_and_validations: %s", response_and_validations)
        ordered_response_and_validations = question_order(response_and_validations)
        log.info("Combined Response and Validation Info Data: %s", ordered_response_and_validations)

        override_button = override_all_button(ordered_response_and_validations)
    except Exception as error:
        log.info("Error %s", error)

    if request.form and request.form['action'] == 'save-and-validate':
        save_form(parameters, request.form, inqcode, period, ruref)
        validate(inqcode, period, ruref)
        time.sleep(6)
        return redirect(url_for('view_form.view_form', inqcode=inqcode, period=period, ruref=ruref))

    return render_template(
        template_name_or_list=form_view_template_HTML,
        survey=inqcode,
        period=period,
        ruref=ruref,
        data=ordered_response_and_validations,
        override_button=override_button,
        status_message=json.dumps(status_message),
        contributor_details=contributor_data['data'][0],
        validation=filter_validations(validations),
        user=get_user(),
        status_colour=status_colour,
        historic_data=historic_data_json,
        grouped_historic_data=grouped_historic_data)
def previous_page():
    log.info("Previous page")
    newpage = request.json["cursor"]
    search_parameters = request.json["search_params"]
    parameters = "graphql;" + f";endCursor={newpage}" + f";{search_parameters}" + ";last=10"
    return change_page(parameters)
def next_page():
    log.info("Next page")
    newpage = request.json["cursor"]
    search_parameters = request.json["search_params"]
    parameters = "graphql;" + f";startCursor={newpage}" + f";{search_parameters}" + ";first=10"
    return change_page(parameters)