def test_upload_collection_instrument_failure(self):
     """Tests on failure (500) False is returned"""
     with responses.RequestsMock() as rsps:
         rsps.add(rsps.POST, ci_upload_url, status=500, json={'errors': ['Failed to publish upload message']})
         with self.app.app_context():
             file = self.create_test_file()
             self.assertFalse(upload_collection_instrument(collection_exercise_id, file))
 def test_upload_collection_instrument(self):
     """Tests on success (200) True is returned"""
     with responses.RequestsMock() as rsps:
         rsps.add(rsps.POST, ci_upload_url, status=200)
         with self.app.app_context():
             file = self.create_test_file()
             self.assertTrue(upload_collection_instrument(collection_exercise_id, file))
 def test_upload_collection_instrument_unauthorised(self):
     """Tests on unauthorised (401) False is returned"""
     with responses.RequestsMock() as rsps:
         rsps.add(rsps.POST, ci_upload_url, status=401)
         with self.app.app_context():
             file = self.create_test_file()
             self.assertFalse(upload_collection_instrument(collection_exercise_id, file))
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))
예제 #5
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))