Exemple #1
0
def _set_ready_for_live(short_name, period):
    survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
    exercises = collection_exercise_controllers.get_collection_exercises_by_survey(
        survey_id)
    exercise = get_collection_exercise_by_period(exercises, period)

    if not exercise:
        abort(404)
    try:
        collection_exercise_controllers.execute_collection_exercise(
            exercise['id'])
        success_panel = "Collection exercise executed"
    except ApiError:
        session['error'] = json.dumps({
            "section":
            "head",
            "header":
            "Error: Failed to execute Collection Exercise",
            "message":
            "Error processing collection exercise"
        })
        success_panel = None

    return redirect(
        url_for('collection_exercise_bp.view_collection_exercise',
                short_name=short_name,
                period=period,
                success_panel=success_panel))
Exemple #2
0
def _upload_sample(short_name, period):
    error = _validate_sample()

    if not error:
        survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
        exercises = collection_exercise_controllers.get_collection_exercises_by_survey(
            survey_id)

        # Find the collection exercise for the given period
        exercise = get_collection_exercise_by_period(exercises, period)

        if not exercise:
            return make_response(
                jsonify({'message': 'Collection exercise not found'}), 404)
        sample_summary = sample_controllers.upload_sample(
            short_name, period, request.files['sampleFile'])

        logger.info('Linking sample summary with collection exercise',
                    collection_exercise_id=exercise['id'],
                    sample_id=sample_summary['id'])

        collection_exercise_controllers.link_sample_summary_to_collection_exercise(
            collection_exercise_id=exercise['id'],
            sample_summary_id=sample_summary['id'])

    return redirect(
        url_for('collection_exercise_bp.view_collection_exercise',
                short_name=short_name,
                period=period,
                error=error,
                show_msg='true'))
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,
    }
def _upload_collection_instrument(short_name, period):
    success_panel = None
    error = _validate_collection_instrument()

    if not error:
        file = request.files['ciFile']
        form_type = _get_form_type(file.filename)
        survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
        exercises = collection_exercise_controllers.get_collection_exercises_by_survey(
            survey_id)

        # Find the collection exercise for the given period
        exercise = get_collection_exercise_by_period(exercises, period)
        if not exercise:
            return make_response(
                jsonify({'message': 'Collection exercise not found'}), 404)

        if collection_instrument_controllers.upload_collection_instrument(
                exercise['id'], file, form_type):
            success_panel = "Collection instrument loaded"
        else:
            session['error'] = json.dumps({
                "section": "ciFile",
                "header": "Error: Failed to upload collection instrument",
                "message": "Please try again"
            })
    else:
        session['error'] = json.dumps(error)

    return redirect(
        url_for('collection_exercise_bp.view_collection_exercise',
                short_name=short_name,
                period=period,
                success_panel=success_panel))
def update_event_date(short_name, period, tag):
    survey = survey_controllers.get_survey_by_shortname(short_name)
    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)
    events = collection_exercise_controllers.get_collection_exercise_events_by_id(
        exercise['id'])
    event_name = get_event_name(tag)
    formatted_events = convert_events_to_new_format(events)
    date_restriction_text = get_date_restriction_text(tag, formatted_events)

    try:
        event = formatted_events[tag]

        form = EventDateForm(day=event['date'][:2],
                             month=event['month'],
                             year=event['date'][-4:],
                             hour=event['time'][:2],
                             minute=event['time'][3:5])

    except KeyError:
        form = EventDateForm()

    return render_template('update-event-date.html',
                           form=form,
                           ce=exercise,
                           period=period,
                           survey=survey,
                           event_name=event_name,
                           date_restriction_text=date_restriction_text)
def update_event_date_submit(short_name, period, tag):
    form = EventDateForm(form=request.form)

    if not form.validate():
        flash('Please enter a valid value', 'error')
        return redirect(
            url_for('collection_exercise_bp.update_event_date',
                    short_name=short_name,
                    period=period,
                    tag=tag))

    try:
        valid_date_for_event(tag, form)
    except ValidationError as exception:
        flash(exception, 'error')
        return redirect(
            url_for('collection_exercise_bp.update_event_date',
                    short_name=short_name,
                    period=period,
                    tag=tag))

    survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
    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)

    submitted_dt = datetime(year=int(form.year.data),
                            month=int(form.month.data),
                            day=int(form.day.data),
                            hour=int(form.hour.data),
                            minute=int(form.minute.data),
                            tzinfo=tz.gettz('Europe/London'))
    """Attempts to create the event, returns None if success or returns an error message upon failure."""
    error_message = collection_exercise_controllers.update_event(
        collection_exercise_id=exercise['id'], tag=tag, timestamp=submitted_dt)

    if error_message:
        flash(error_message, 'error')
        return redirect(
            url_for('collection_exercise_bp.update_event_date',
                    short_name=short_name,
                    period=period,
                    tag=tag))

    return redirect(
        url_for('collection_exercise_bp.view_collection_exercise',
                short_name=short_name,
                period=period,
                success_panel='Event date updated.'))
Exemple #7
0
def _upload_seft_collection_instrument(short_name, period):
    success_panel = None
    error = _validate_collection_instrument()

    if not error:
        file = request.files['ciFile']
        is_ru_specific_instrument = False
        if file.filename.split(".")[0].isdigit():
            is_ru_specific_instrument = True

        logger.info("Collection instrument about to be uploaded",
                    filename=file.filename)
        survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
        exercises = collection_exercise_controllers.get_collection_exercises_by_survey(
            survey_id)

        # Find the collection exercise for the given period
        exercise = get_collection_exercise_by_period(exercises, period)
        if not exercise:
            return make_response(
                jsonify({'message': 'Collection exercise not found'}), 404)

        error_text = None
        if is_ru_specific_instrument:
            ru_ref = file.filename.split(".")[0]
            upload_success, error_text = collection_instrument_controllers. \
                upload_ru_specific_collection_instrument(exercise['id'], file, ru_ref)
        else:
            form_type = _get_form_type(file.filename)
            upload_success = collection_instrument_controllers.upload_collection_instrument(
                exercise['id'], file, form_type)

        if upload_success:
            success_panel = "Collection instrument loaded"
        else:
            message = error_text if error_text else "Please try again"
            session['error'] = json.dumps({
                "section": "ciFile",
                "header": "Error: Failed to upload collection instrument",
                "message": message
            })
    else:
        session['error'] = json.dumps(error)

    return redirect(
        url_for('collection_exercise_bp.get_seft_collection_instrument',
                short_name=short_name,
                period=period,
                success_panel=success_panel))
def update_event_date(short_name, period, tag):
    survey = survey_controllers.get_survey_by_shortname(short_name)
    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)
    events = collection_exercise_controllers.get_collection_exercise_events_by_id(
        exercise["id"])
    show = is_viewed_reminder_last_in_sequence(events, tag)
    event_name = get_event_name(tag)
    formatted_events = convert_events_to_new_format(events)
    date_restriction_text = get_date_restriction_text(tag, formatted_events)

    try:
        event = formatted_events[tag]

        form = EventDateForm(
            day=event["date"][:2],
            month=event["month"],
            year=event["date"][-4:],
            hour=event["time"][:2],
            minute=event["time"][3:5],
        )
    except KeyError:
        form = EventDateForm()

    return render_template(
        "update-event-date.html",
        form=form,
        ce=exercise,
        period=period,
        survey=survey,
        event_name=event_name,
        date_restriction_text=date_restriction_text,
        show=show,
        tag=tag,
    )
def get_create_collection_event_form(short_name, period, ce_id, tag):
    logger.info(
        "Retrieving form for create collection exercise event",
        short_name=short_name,
        period=period,
        ce_id=ce_id,
        tag=tag,
    )
    survey = survey_controllers.get_survey(short_name)
    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)

    events = collection_exercise_controllers.get_collection_exercise_events_by_id(exercise["id"])
    form = EventDateForm()
    event_name = get_event_name(tag)
    formatted_events = convert_events_to_new_format(events)
    date_restriction_text = get_date_restriction_text(tag, formatted_events)

    logger.info(
        "Successfully retrieved form for create collection exercise event",
        short_name=short_name,
        period=period,
        ce_id=ce_id,
        tag=tag,
    )

    return render_template(
        "create-ce-event.html",
        ce_id=ce_id,
        short_name=short_name,
        period=period,
        survey=survey,
        event_name=event_name,
        date_restriction_text=date_restriction_text,
        tag=tag,
        form=form,
    )
def update_event_date_submit(short_name, period, tag):
    form = EventDateForm(form=request.form)
    if str(form.checkbox.data) == "True":
        survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
        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 to delete",
                         short_name=short_name,
                         period=period)
            abort(404)
        """Attempts to delete the event, returns None if success or returns an error message upon failure."""
        message = collection_exercise_controllers.delete_event(
            collection_exercise_id=exercise["id"], tag=tag)

        if message:
            flash(message, "error")
            return redirect(
                url_for("collection_exercise_bp.update_event_date",
                        short_name=short_name,
                        period=period,
                        tag=tag))

        return redirect(
            url_for(
                "collection_exercise_bp.view_collection_exercise",
                short_name=short_name,
                period=period,
                success_panel="Event deleted.",
            ))
    if not form.validate():
        flash("Please enter a valid value", "error")
        return redirect(
            url_for("collection_exercise_bp.update_event_date",
                    short_name=short_name,
                    period=period,
                    tag=tag))

    try:
        valid_date_for_event(tag, form)
    except ValidationError as exception:
        flash(exception, "error")
        return redirect(
            url_for("collection_exercise_bp.update_event_date",
                    short_name=short_name,
                    period=period,
                    tag=tag))

    survey_id = survey_controllers.get_survey_id_by_short_name(short_name)
    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)

    submitted_dt = datetime(
        year=int(form.year.data),
        month=int(form.month.data),
        day=int(form.day.data),
        hour=int(form.hour.data),
        minute=int(form.minute.data),
        tzinfo=tz.gettz("Europe/London"),
    )
    """Attempts to create the event, returns None if success or returns an error message upon failure."""
    message = collection_exercise_controllers.update_event(
        collection_exercise_id=exercise["id"], tag=tag, timestamp=submitted_dt)
    if "error" in message:
        flash(message["error"]["message"], "error")
        return redirect(
            url_for("collection_exercise_bp.update_event_date",
                    short_name=short_name,
                    period=period,
                    tag=tag))

    if "info" in message:
        return redirect(
            url_for(
                "collection_exercise_bp.view_collection_exercise",
                short_name=short_name,
                period=period,
                success_panel="Event date updated.",
                info_panel=message["info"],
            ))
    else:
        return redirect(
            url_for(
                "collection_exercise_bp.view_collection_exercise",
                short_name=short_name,
                period=period,
                success_panel="Event date updated.",
            ))