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 _get_vacancies_survey_ids() -> list[str]: """ Returns a list of vacancies survey id's. This list is defined in the config :returns: A list of vacancies survey_id's """ return [survey_controllers.get_survey_id_by_short_name(vacancies_survey) for vacancies_survey in VACANCIES_LIST]
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 _get_FDI_survey_id() -> list[str]: """ Returns a list of FDI survey ids. This list is defined in the config :returns: A list of FDI survey_id's """ return [survey_controllers.get_survey_id_by_short_name(fdi_survey) for fdi_survey in FDI_LIST]
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 _get_survey_id(selected_survey): """ Returns a survey_id from a survey shortname. :param selected_survey: A survey shortname (MBS, ASHE, etc) :returns: A list containing the single survey_id of the selected_survey. """ return [survey_controllers.get_survey_id_by_short_name(selected_survey)]
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 _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 post_change_reporting_unit(thread_id): # noqa: C901 thread = message_controllers.get_conversation(thread_id) reporting_unit = request.form.get("reporting-units") if reporting_unit: message_patch_payload = {"business_id": reporting_unit} # You can come to this page after changing a thread category to SURVEY, so we'll add the survey_id to # the message patch survey_shortname = session.get("secure-message-change-survey-shortname") if survey_shortname: survey_id = survey_controllers.get_survey_id_by_short_name(survey_shortname) message_patch_payload["survey_id"] = survey_id # Patch messages first. It's the most 'risky' as it does multiple calls for message in thread["messages"]: message_id = message["msg_id"] try: message_controllers.patch_message(message_id, message_patch_payload) except ApiError: flash("Something went wrong updating the survey. Please try again.", category="error") return redirect(url_for("messages_bp.get_change_reporting_unit", thread_id=thread_id)) flash("The survey has been successfully updated.") # Next, if there a survey_id in the session, patch the category to SURVEY as that's the only thing # it can be if survey_shortname: payload = {"category": "SURVEY"} try: message_controllers.patch_thread(thread_id, payload) except ApiError: flash("Something went wrong updating the category. Please try again.", category="error") return redirect(url_for("messages_bp.get_change_thread_category", thread_id=thread_id)) flash("The category has been successfully updated") del session["secure-message-change-survey-shortname"] return redirect(url_for("messages_bp.view_select_survey")) flash("An option must be selected", category="error") reporting_units = get_respondent_enrolments_from_thread(thread) breadcrumbs = [{"text": "Messages", "url": "/messages"}, {"text": "Filter by survey"}] return render_template( "secure-message/change-reporting-unit.html", thread=thread, thread_id=thread_id, breadcrumbs=breadcrumbs, reporting_units=reporting_units, )
def edit_collection_exercise_details(short_name, period): form = EditCollectionExerciseDetailsForm(form=request.form) if not form.validate(): logger.info( "Failed validation, retrieving collection exercise data for form", short_name=short_name, period=period) ce_details = build_collection_exercise_details(short_name, period) ce_state = ce_details['collection_exercise']['state'] survey_id = survey_controllers.get_survey_id_by_short_name(short_name) locked = ce_state in ('LIVE', 'READY_FOR_LIVE', 'EXECUTION_STARTED', 'VALIDATED', 'EXECUTED') return render_template( 'edit-collection-exercise-details.html', survey_ref=ce_details['survey']['surveyRef'], form=form, short_name=short_name, period=period, locked=locked, ce_state=ce_details['collection_exercise']['state'], errors=form.errors, user_description=ce_details['collection_exercise'] ['userDescription'], collection_exercise_id=ce_details['collection_exercise']['id'], survey_id=survey_id) else: logger.info("Updating collection exercise details", short_name=short_name, period=period) form = request.form collection_exercise_controllers.update_collection_exercise_user_description( form.get('collection_exercise_id'), form.get('user_description')) if form.get('period') != period: collection_exercise_controllers.update_collection_exercise_period( form.get('collection_exercise_id'), form.get('period')) return redirect( url_for('surveys_bp.view_survey', short_name=short_name, ce_updated='True'))
def edit_collection_exercise_details(short_name, period): form = EditCollectionExerciseDetailsForm(form=request.form) if not form.validate(): logger.info( "Failed validation, retrieving collection exercise data for form", short_name=short_name, period=period ) ce_details = build_collection_exercise_details(short_name, period) ce_state = ce_details["collection_exercise"]["state"] survey_id = survey_controllers.get_survey_id_by_short_name(short_name) locked = ce_state in ("LIVE", "READY_FOR_LIVE", "EXECUTION_STARTED", "VALIDATED", "EXECUTED", "ENDED") return render_template( "edit-collection-exercise-details.html", survey_ref=ce_details["survey"]["surveyRef"], form=form, short_name=short_name, period=period, locked=locked, ce_state=ce_details["collection_exercise"]["state"], errors=form.errors, user_description=ce_details["collection_exercise"]["userDescription"], collection_exercise_id=ce_details["collection_exercise"]["id"], survey_id=survey_id, ) else: logger.info("Updating collection exercise details", short_name=short_name, period=period) form = request.form collection_exercise_controllers.update_collection_exercise_user_description( form.get("collection_exercise_id"), form.get("user_description") ) if form.get("period") != period: collection_exercise_controllers.update_collection_exercise_period( form.get("collection_exercise_id"), form.get("period") ) return redirect(url_for("surveys_bp.view_survey", short_name=short_name, ce_updated="True"))
def _get_FDI_survey_id(): return [ survey_controllers.get_survey_id_by_short_name(fdi_survey) for fdi_survey in FDI_LIST ]
def _get_survey_id(selected_survey): return [survey_controllers.get_survey_id_by_short_name(selected_survey)]
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.", ))