Ejemplo n.º 1
0
def get_response_statuses(ru_ref, error=None):
    logger.info("Retrieving response statuses", ru_ref=ru_ref)
    short_name = request.args.get("survey")
    period = request.args.get("period")

    completed_respondent = ""

    survey = survey_controllers.get_survey_by_shortname(short_name)

    exercises = collection_exercise_controllers.get_collection_exercises_by_survey(
        survey["id"])
    exercise = collection_exercise_controllers.get_collection_exercise_from_list(
        exercises, period)

    reporting_unit = party_controller.get_business_by_ru_ref(ru_ref)

    statuses = case_controller.get_available_case_group_statuses_direct(
        exercise["id"], ru_ref)
    available_statuses = {
        event: map_ce_response_status(status)
        for event, status in statuses.items()
        if case_controller.is_allowed_status(status)
    }

    case_groups = case_controller.get_case_groups_by_business_party_id(
        reporting_unit["id"])
    case_group = case_controller.get_case_group_by_collection_exercise(
        case_groups, exercise["id"])
    case_group_status = case_group["caseGroupStatus"]
    case_id = get_case_by_case_group_id(case_group["id"]).get("id")
    is_complete = case_group_status in COMPLETE_STATE
    completed_timestamp = get_timestamp_for_completed_case_event(
        case_id) if is_complete else None
    case_events = get_case_events_by_case_id(case_id=case_id)

    if case_group_status == "COMPLETE":
        case_event = get_case_event_for_seft_or_eq(case_events)
        completed_respondent = get_user_from_case_events(case_event)

    return render_template(
        "response-status.html",
        ru_ref=ru_ref,
        ru_name=reporting_unit["name"],
        trading_as=reporting_unit["trading_as"],
        survey_short_name=format_short_name(survey["shortName"]),
        survey_ref=survey["surveyRef"],
        ce_period=period,
        statuses=available_statuses,
        case_group_status=map_ce_response_status(case_group_status),
        case_group_id=case_group["id"],
        error=error,
        is_complete=is_complete,
        completed_timestamp=completed_timestamp,
        completed_respondent=completed_respondent,
    )
def add_collection_exercise_details(collection_exercise, reporting_unit,
                                    case_groups):
    """
    Creates a dict of formatted data.

    :param collection_exercise: A dict containing collection exercise data
    :type collection_exercise: dict
    :param reporting_unit: A dict containing reporting unit attribute data
    :type reporting_unit: dict
    :param case_groups: A list of case group data
    :return: A dict containing formatted data to be used by the template
    :rtype: dict
    """
    response_status = get_case_group_status_by_collection_exercise(
        case_groups, collection_exercise['id'])

    return {
        **collection_exercise, 'responseStatus':
        map_ce_response_status(response_status),
        'companyName':
        reporting_unit['name'],
        'companyRegion':
        map_region(reporting_unit['attributes']['region']),
        'trading_as':
        reporting_unit['trading_as']
    }
Ejemplo n.º 3
0
def build_survey_table_data_dict(collection_exercises: list, case_groups: list) -> list:
    """
    Creates the dictionary of survey & CE information for the front-end table to display

    :param collection_exercises: A list of collection exercises to add to the table
    :param case_groups: A list of case groups for the reporting unit
    :return: A sorted list of survey/CE information to provide to the front-end table
    """
    table_data = {}
    surveys = {}
    for ce in collection_exercises:
        # Keep a mini cache of surveys, so we don't have to keep asking for the same survey data repeatedly
        survey = surveys.get(ce["surveyId"])
        if survey is None:
            survey = get_survey_by_id(ce["surveyId"])
            surveys[ce["surveyId"]] = survey

        if survey["surveyRef"] in table_data:
            # Keep the one with the later go-live date
            if parse_date(table_data[survey["surveyRef"]]["goLive"]) > parse_date(ce["scheduledStartDateTime"]):
                continue
        table_data[survey["surveyRef"]] = {
            "surveyName": f"{survey['surveyRef']} {survey['shortName']}",
            "surveyId": ce["surveyId"],
            "shortName": survey["shortName"],
            "period": ce["exerciseRef"],
            "goLive": ce["scheduledStartDateTime"],
            "caseStatus": map_ce_response_status(get_case_group_status_by_collection_exercise(case_groups, ce["id"])),
        }
    return sorted(table_data.items(), key=lambda t: t[0])
Ejemplo n.º 4
0
def add_collection_exercise_details(collection_exercise, reporting_unit, case_groups):
    collection_exercise["responseStatus"] = map_ce_response_status(
        get_case_group_status_by_collection_exercise(case_groups, collection_exercise["id"])
    )
    collection_exercise["companyName"] = reporting_unit["name"]
    collection_exercise["companyRegion"] = map_region(reporting_unit["attributes"]["region"])
    collection_exercise["tradingAs"] = reporting_unit["trading_as"]

    return collection_exercise
Ejemplo n.º 5
0
def add_collection_exercise_details(collection_exercise, reporting_unit,
                                    case_groups):
    response_status = get_case_group_status_by_collection_exercise(
        case_groups, collection_exercise['id'])
    reporting_unit_ce = party_controller.get_business_by_party_id(
        reporting_unit['id'], collection_exercise['id'])
    statuses = case_controller.get_available_case_group_statuses_direct(
        collection_exercise['id'], reporting_unit['sampleUnitRef'])
    ce_extra = {
        **collection_exercise, 'responseStatus':
        map_ce_response_status(response_status),
        'companyName':
        reporting_unit_ce['name'],
        'companyRegion':
        map_region(reporting_unit_ce['region']),
        'trading_as':
        reporting_unit_ce['trading_as'],
        'statuses':
        statuses.values()
    }
    return ce_extra