def test_get_survey_by_short_name_fail(self): with responses.RequestsMock() as rsps: rsps.add(rsps.GET, url_get_survey_by_short_name, status=400) with app.app_context(): with self.assertRaises(ApiError): survey_controller.get_survey_by_short_name( survey["shortName"])
def get_case_data(case_id, party_id, business_party_id, survey_short_name): logger.info("Attempting to retrieve detailed case data", case_id=case_id, party_id=party_id) # Check if respondent has permission to see case data case = get_case_by_case_id(case_id) survey = survey_controller.get_survey_by_short_name(survey_short_name) if not party_controller.is_respondent_enrolled(party_id, business_party_id, survey): raise NoSurveyPermission(party_id, case_id) case_data = { "collection_exercise": collection_exercise_controller.get_collection_exercise( case["caseGroup"]["collectionExerciseId"]), "collection_instrument": collection_instrument_controller.get_collection_instrument( case["collectionInstrumentId"], app.config["COLLECTION_INSTRUMENT_URL"], app.config["BASIC_AUTH"]), "survey": survey, "business_party": party_controller.get_party_by_business_id(business_party_id, app.config["PARTY_URL"], app.config["BASIC_AUTH"]), }
def get_case_data(case_id, party_id, business_party_id, survey_short_name): logger.info('Attempting to retrieve detailed case data', case_id=case_id, party_id=party_id) # Check if respondent has permission to see case data case = get_case_by_case_id(case_id) if not party_controller.is_respondent_enrolled(party_id, business_party_id, survey_short_name): raise NoSurveyPermission(party_id, case_id) case_data = { "collection_exercise": collection_exercise_controller.get_collection_exercise( case['caseGroup']['collectionExerciseId']), "collection_instrument": collection_instrument_controller.get_collection_instrument( case['collectionInstrumentId'], app.config['COLLECTION_INSTRUMENT_URL'], app.config['BASIC_AUTH']), "survey": survey_controller.get_survey_by_short_name(survey_short_name), "business_party": party_controller.get_party_by_business_id(business_party_id, app.config['PARTY_URL'], app.config['BASIC_AUTH']) } logger.info('Successfully retrieved all data relating to case', case_id=case_id, party_id=party_id) return case_data
def send_help_message(session, short_name, business_id): """Sends secure message for the help pages""" form = SecureMessagingForm(request.form) option = request.args['option'] sub_option = request.args['sub_option'] if not form.validate(): flash(form.errors['body'][0]) if sub_option == 'not_defined': return redirect( url_for('surveys_bp.get_send_help_message', short_name=short_name, business_id=business_id, option=option)) else: return redirect( url_for('surveys_bp.get_send_help_message_page', short_name=short_name, business_id=business_id, option=option, sub_option=sub_option)) else: subject = request.args['subject'] party_id = session.get_party_id() survey = survey_controller.get_survey_by_short_name(short_name) business_id = business_id logger.info("Form validation successful", party_id=party_id) sent_message = _send_new_message(subject, party_id, survey['id'], business_id) thread_url = url_for( "secure_message_bp.view_conversation", thread_id=sent_message['thread_id']) + "#latest-message" flash(Markup(f'Message sent. <a href={thread_url}>View Message</a>')) return redirect(url_for('surveys_bp.get_survey_list', tag='todo'))
def get_help_page(session, short_name, business_id): """Gets Survey Help page provided survey short name and business_id""" survey = survey_controller.get_survey_by_short_name(short_name) return render_template('surveys/help/surveys-help.html', form=HelpOptionsForm(), short_name=short_name, survey_name=survey['longName'], business_id=business_id)
def is_respondent_enrolled(party_id, business_party_id, survey_short_name, return_survey=False): survey = survey_controller.get_survey_by_short_name(survey_short_name) for enrolment in get_respondent_enrolments(party_id): if enrolment['business_id'] == business_party_id and enrolment[ 'survey_id'] == survey['id']: if return_survey: return {'survey': survey} return True
def test_get_survey_by_short_name_success(self): with responses.RequestsMock() as rsps: rsps.add(rsps.GET, url_get_survey_by_short_name, json=survey, status=200, content_type="application/json") with app.app_context(): get_survey = survey_controller.get_survey_by_short_name( survey["shortName"]) self.assertIn("Bricks", get_survey["shortName"])
def get_help_option_select(session, short_name, business_id, option): """Gets help completing this survey's additional options (sub options)""" template = option_template_url_mapping.get(option, "Invalid template") if template == 'Invalid template': abort(404) else: survey = survey_controller.get_survey_by_short_name(short_name) return render_template(template, short_name=short_name, business_id=business_id, option=option, form=HelpCompletingThisSurveyForm(), survey_name=survey['longName'])
def get_help_option_sub_option_select(session, short_name, business_id, option, sub_option): """Provides additional options with sub option provided""" template = sub_option_template_url_mapping.get(sub_option, "Invalid template") survey = survey_controller.get_survey_by_short_name(short_name) if template == 'Invalid template': abort(404) else: return render_template(template, short_name=short_name, option=option, sub_option=sub_option, business_id=business_id, survey_name=survey['longName'], inside_legal_basis=is_legal_basis_mandatory( survey['legalBasisRef']))
def download_survey(session): party_id = session.get_party_id() case_id = request.args["case_id"] business_party_id = request.args["business_party_id"] survey_short_name = request.args["survey_short_name"] logger.info("Attempting to download collection instrument", case_id=case_id, party_id=party_id) # Check if respondent has permission to download for this case case = case_controller.get_case_by_case_id(case_id) survey = survey_controller.get_survey_by_short_name(survey_short_name) if not party_controller.is_respondent_enrolled(party_id, business_party_id, survey): raise NoSurveyPermission(party_id, case_id) collection_instrument, headers = collection_instrument_controller.download_collection_instrument( case["collectionInstrumentId"], case_id, party_id ) logger.info("Successfully downloaded collection instrument", case_id=case_id, party_id=party_id) return collection_instrument, 200, headers
def upload_survey(session): party_id = session.get_party_id() case_id = request.args["case_id"] business_party_id = request.args["business_party_id"] survey_short_name = request.args["survey_short_name"] logger.info("Attempting to upload collection instrument", case_id=case_id, party_id=party_id) if request.content_length > app.config["MAX_UPLOAD_LENGTH"]: return redirect( url_for( "surveys_bp.upload_failed", _external=True, case_id=case_id, business_party_id=business_party_id, survey_short_name=survey_short_name, error_info="size", )) # Check if respondent has permission to upload for this case survey = survey_controller.get_survey_by_short_name(survey_short_name) if not party_controller.is_respondent_enrolled(party_id, business_party_id, survey): raise NoSurveyPermission(party_id, case_id) case = case_controller.get_case_by_case_id(case_id) case_group = case.get("caseGroup") collection_exercise_id = case_group.get("collectionExerciseId") business_party = party_controller.get_party_by_business_id( case_group["partyId"], app.config["PARTY_URL"], app.config["BASIC_AUTH"], collection_exercise_id=collection_exercise_id, verbose=True, ) # Get the uploaded file upload_file = request.files["file"] try: # Upload the file to the collection instrument service collection_instrument_controller.upload_collection_instrument( upload_file, case, business_party, party_id, survey) except CiUploadError as ex: error_type = determine_error_type(ex) if not error_type: logger.error( "Unexpected error message returned from collection instrument service", error_message=ex.error_message, party_id=party_id, case_id=case_id, ) error_type = "unexpected" return redirect( url_for( "surveys_bp.upload_failed", _external=True, case_id=case_id, business_party_id=business_party_id, survey_short_name=survey_short_name, error_info=error_type, )) logger.info("Successfully uploaded collection instrument", party_id=party_id, case_id=case_id) unread_message_count = { "unread_message_count": conversation_controller.try_message_count_from_session(session) } return render_template( "surveys/surveys-upload-success.html", upload_filename=upload_file.filename, unread_message_count=unread_message_count, )
case_id=case_id, party_id=party_id, version=version) version = "v2" else: raise ValueError(f"The eq version [{version}] is not supported") if case["caseGroup"]["caseGroupStatus"] in ("COMPLETE", "COMPLETEDBYPHONE", "NOLONGERREQUIRED"): logger.info( "The case group status is complete, opening an EQ is forbidden", case_id=case_id, party_id=party_id) abort(403) survey = survey_controller.get_survey_by_short_name(survey_short_name) if not party_controller.is_respondent_enrolled(party_id, business_party_id, survey): raise NoSurveyPermission(party_id, case_id) payload = EqPayload().create_payload(case, collection_exercise, party_id, business_party_id, survey, version) json_secret_keys = app.config["JSON_SECRET_KEYS"] encrypter = Encrypter(json_secret_keys) if version == "v2": token = encrypter.encrypt(payload, "eq") eq_url = app.config["EQ_URL"] + token elif version == "v3": token = encrypter.encrypt(payload, "eq_v3")