def upload_instrument_with_no_collection_exercise(self, survey_id, classifiers=None, session=None):
        """
        Upload a collection instrument to the db without a collection exercise

        :param classifiers: Classifiers associated with the instrument
        :param session: database session
        :param survey_id: database session
        :return: a collection instrument instance
        """

        log.info('Upload instrument', survey_id=survey_id)

        validate_uuid(survey_id)
        instrument = InstrumentModel(ci_type='EQ')

        survey = self._find_or_create_survey_from_survey_id(survey_id, session)
        instrument.survey = survey

        if classifiers:
            deserialized_classifiers = loads(classifiers)
            instruments = self._get_instruments_by_classifier(deserialized_classifiers, None, session)
            for instrument in instruments:
                if instrument.classifiers == deserialized_classifiers:
                    raise RasError("Cannot upload an instrument with an identical set of classifiers", 400)
            instrument.classifiers = deserialized_classifiers

        session.add(instrument)

        return instrument
Example #2
0
    def upload_to_bucket(self, exercise_id, file, ru_ref=None, classifiers=None, session=None):
        """
        Encrypt and upload a collection instrument to the bucket and db

        :param exercise_id: An exercise id (UUID)
        :param ru_ref: The name of the file we're receiving
        :param classifiers: Classifiers associated with the instrument
        :param file: A file object from which we can read the file contents
        :param session: database session
        :return: a collection instrument instance
        """

        log.info("Upload exercise", exercise_id=exercise_id)

        validate_uuid(exercise_id)
        self.validate_non_duplicate_instrument(file, exercise_id, session)
        instrument = InstrumentModel(ci_type="SEFT")

        seft_file = self._create_seft_file(instrument.instrument_id, file, encrypt_and_save_to_db=False)
        instrument.seft_file = seft_file

        exercise = self._find_or_create_exercise(exercise_id, session)
        instrument.exercises.append(exercise)

        survey = self._find_or_create_survey_from_exercise_id(exercise_id, session)
        instrument.survey = survey

        if ru_ref:
            business = self._find_or_create_business(ru_ref, session)
            self.validate_one_instrument_for_ru_specific_upload(exercise, business, session)
            instrument.businesses.append(business)

        if classifiers:
            instrument.classifiers = loads(classifiers)

        try:
            survey_ref = get_survey_ref(instrument.survey.survey_id)
            file.filename = survey_ref + "/" + exercise_id + "/" + file.filename
            seft_ci_bucket = GoogleCloudSEFTCIBucket(current_app.config)
            seft_ci_bucket.upload_file_to_bucket(file=file)
            instrument.seft_file.gcs = True
        except Exception as e:
            log.exception("An error occurred when trying to put SEFT CI in bucket")
            raise e

        session.add(instrument)
        return instrument
    def upload_instrument(self, exercise_id, file, ru_ref=None, classifiers=None, session=None):
        """
        Encrypt and upload a collection instrument to the db

        :param exercise_id: An exercise id (UUID)
        :param ru_ref: The name of the file we're receiving
        :param classifiers: Classifiers associated with the instrument
        :param file: A file object from which we can read the file contents
        :param session: database session
        :return: a collection instrument instance
        """

        log.info('Upload exercise', exercise_id=exercise_id)

        validate_uuid(exercise_id)
        instrument = InstrumentModel(ci_type='SEFT')

        seft_file = self._create_seft_file(instrument.instrument_id, file)
        instrument.seft_file = seft_file

        exercise = self._find_or_create_exercise(exercise_id, session)
        instrument.exercises.append(exercise)

        survey = self._find_or_create_survey_from_exercise_id(exercise_id, session)
        instrument.survey = survey

        if ru_ref:
            business = self._find_or_create_business(ru_ref, session)
            self.validate_one_instrument_for_ru_specific_upload(exercise, business, session)
            instrument.businesses.append(business)

        if classifiers:
            instrument.classifiers = loads(classifiers)

        session.add(instrument)
        return instrument