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))
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 create_collection_exercise(survey_ref, short_name): logger.info("Attempting to create collection exercise", survey_ref=survey_ref, survey=short_name) ce_form = CreateCollectionExerciseDetailsForm(form=request.form) form = request.form survey_id = form.get('hidden_survey_id') survey_name = form.get('hidden_survey_name') if not ce_form.validate(): logger.info("Failed validation, retrieving survey data for form", survey=short_name, survey_ref=survey_ref) error = None if ce_form.errors['period'][ 1] == 'Please enter numbers only for the period': error = ce_form.errors['period'][1] return render_template('create-collection-exercise.html', form=ce_form, short_name=short_name, errors=error, survey_ref=survey_ref, survey_id=survey_id, survey_name=survey_name) created_period = form.get('period') ce_details = collection_exercise_controllers.get_collection_exercises_by_survey( survey_id) for ce in ce_details: if ce['exerciseRef'] == str(created_period): error = "Use a period that is not in use by any collection exercise for this survey." return render_template('create-collection-exercise.html', form=ce_form, short_name=short_name, errors=error, survey_ref=survey_ref, survey_id=survey_id, survey_name=survey_name) logger.info("Creating collection exercise for survey", survey=short_name, survey_ref=survey_ref) collection_exercise_controllers.create_collection_exercise( survey_id, survey_name, form.get('user_description'), form.get('period')) logger.info("Successfully created collection exercise", survey=short_name, survey_ref=survey_ref) return redirect( url_for('surveys_bp.view_survey', short_name=short_name, ce_created='True', new_period=form.get('period')))
def create_collection_exercise(survey_ref, short_name): logger.info("Attempting to create collection exercise", survey_ref=survey_ref, survey=short_name) ce_form = CreateCollectionExerciseDetailsForm(form=request.form) form = request.form survey_id = form.get("hidden_survey_id") survey_name = form.get("hidden_survey_name") survey_eq_version = form.get("hidden_eq_version") if not ce_form.validate(): logger.info("Failed validation, retrieving survey data for form", survey=short_name, survey_ref=survey_ref) error = None if ce_form.errors["period"][1] == "Please enter numbers only for the period": error = ce_form.errors["period"][1] return render_template( "create-collection-exercise.html", form=ce_form, short_name=short_name, errors=error, survey_ref=survey_ref, survey_id=survey_id, survey_name=survey_name, survey_eq_version=survey_eq_version, ) created_period = form.get("period") ce_details = collection_exercise_controllers.get_collection_exercises_by_survey(survey_id) for ce in ce_details: if ce["exerciseRef"] == str(created_period): error = "Use a period that is not in use by any collection exercise for this survey." return render_template( "create-collection-exercise.html", form=ce_form, short_name=short_name, errors=error, survey_ref=survey_ref, survey_id=survey_id, survey_name=survey_name, survey_eq_version=survey_eq_version, ) logger.info( "Creating collection exercise for survey", survey=short_name, survey_ref=survey_ref, survey_eq_version=survey_eq_version, ) collection_exercise_controllers.create_collection_exercise( survey_id, survey_name, form.get("user_description"), form.get("period"), survey_eq_version ) logger.info("Successfully created collection exercise", survey=short_name, survey_ref=survey_ref) return redirect( url_for("surveys_bp.view_survey", short_name=short_name, ce_created="True", new_period=form.get("period")) )
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 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.'))
def validate_period(form, field): hidden_survey_id = form.hidden_survey_id.data hidden_ce_id = form.collection_exercise_id.data ce_details = collection_exercise_controllers.get_collection_exercises_by_survey( hidden_survey_id) inputted_period = field.data if inputted_period is None: raise ValidationError("Please enter numbers only for the period") for ce in ce_details: if ce["id"] == str(hidden_ce_id): continue if ce["exerciseRef"] == str(inputted_period): raise ValidationError("Please enter a period not in use")
def home(): overview_survey = session.get("overview_survey") if not session.get("overview_survey"): return redirect(url_for("home_bp.get_overview_survey")) survey = survey_controllers.get_survey_by_shortname(overview_survey) collection_exercises = collection_exercise_controllers.get_collection_exercises_by_survey(survey["id"]) current_collection_exercise = get_current_collection_exercise(collection_exercises) if not current_collection_exercise: return render_template("overview/no_collection_exercise_overview.html", survey=overview_survey) formatted_data = format_data_for_template(current_collection_exercise, survey) return render_template("overview/overview.html", data=formatted_data)
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.", ))