def build_collection_exercise_details(short_name, period):
    survey = survey_controllers.get_survey_by_shortname(short_name)
    survey_id = survey["id"]
    exercises = collection_exercise_controllers.get_collection_exercises_by_survey(survey_id)
    exercise = get_collection_exercise_by_period(exercises, period)
    if not exercise:
        logger.error("Failed to find collection exercise by period", short_name=short_name, period=period)
        abort(404)
    collection_exercise_id = exercise["id"]
    survey["shortName"] = format_short_name(survey["shortName"])
    full_exercise = collection_exercise_controllers.get_collection_exercise_by_id(collection_exercise_id)
    exercise_events = collection_exercise_controllers.get_collection_exercise_events_by_id(collection_exercise_id)
    collection_instruments = collection_instrument_controllers.get_collection_instruments_by_classifier(
        collection_exercise_id=collection_exercise_id, survey_id=survey_id
    )

    eq_ci_selectors = collection_instrument_controllers.get_collection_instruments_by_classifier(
        ci_type="EQ", survey_id=survey_id
    )

    summary_id = collection_exercise_controllers.get_linked_sample_summary_id(collection_exercise_id)
    sample_summary = sample_controllers.get_sample_summary(summary_id) if summary_id else None
    ci_classifiers = survey_controllers.get_survey_ci_classifier(survey_id)

    return {
        "survey": survey,
        "collection_exercise": full_exercise,
        "events": convert_events_to_new_format(exercise_events),
        "collection_instruments": collection_instruments,
        "eq_ci_selectors": eq_ci_selectors,
        "sample_summary": _format_sample_summary(sample_summary),
        "ci_classifiers": ci_classifiers,
    }
Ejemplo n.º 2
0
def post_link_collection_instrument(short_name):
    form = LinkCollectionInstrumentForm(form=request.form)
    eq_ci_selectors = []
    try:
        # The eq_id of a collection instrument will ALWAYS be its shortname.
        short_name_lower = str(short_name).lower()
        survey_uuid = survey_controllers.get_survey_by_shortname(
            short_name_lower)["id"]
        eq_ci_selectors = collection_instrument_controllers.get_collection_instruments_by_classifier(
            ci_type="EQ", survey_id=survey_uuid)
        if not form.validate():
            return render_template(
                "link-collection-instrument.html",
                short_name=short_name,
                form=form,
                errors=form.errors.items(),
                eq_ci_selectors=eq_ci_selectors,
            )
        collection_instrument_controllers.link_collection_instrument_to_survey(
            survey_uuid, short_name_lower, form.formtype.data)
        # Need to get selectors a second time as we just added one and the list from before is outdated.
        eq_ci_selectors = collection_instrument_controllers.get_collection_instruments_by_classifier(
            ci_type="EQ", survey_id=survey_uuid)
    except ApiError as err:
        try:
            error_dict = loads(err.message)
            errors = [("formtype", [error_dict["errors"][0]])]
        except (JSONDecodeError, KeyError):
            # If the message isn't JSON, or the 'errors' key doesn't exist, we'll render the message anyway as it
            # might still be helpful.
            errors = [("", [err.message])]

        return render_template(
            "link-collection-instrument.html",
            form=form,
            eq_ci_selectors=eq_ci_selectors,
            errors=errors,
            short_name=short_name,
        )

    form.formtype.data = ""  # Reset the value on successful submission
    return render_template("link-collection-instrument.html",
                           form=form,
                           eq_ci_selectors=eq_ci_selectors,
                           short_name=short_name)
Ejemplo n.º 3
0
def get_link_collection_instrument(short_name):
    form = LinkCollectionInstrumentForm(form=request.form)
    short_name_lower = str(short_name).lower()
    survey_id = survey_controllers.get_survey_by_shortname(
        short_name_lower)['id']
    eq_ci_selectors = collection_instrument_controllers.get_collection_instruments_by_classifier(
        ci_type='EQ', survey_id=survey_id)
    logger.info(eq_ci_selectors)
    return render_template('link-collection-instrument.html',
                           short_name=short_name,
                           form=form,
                           eq_ci_selectors=eq_ci_selectors)