def get_instruments_by_exercise_id_csv(exercise_id, session=None): """ Finds all collection instruments associated with an exercise and returns them in csv format :param exercise_id :param session: database session :return: collection instruments in csv """ log.info('Getting csv for instruments', exercise_id=exercise_id) validate_uuid(exercise_id) csv_format = '"{count}","{file_name}","{length}","{date_stamp}"\n' count = 1 csv = csv_format.format(count='Count', file_name='File Name', length='Length', date_stamp='Time Stamp') exercise = query_exercise_by_id(exercise_id, session) if not exercise: return None for instrument in exercise.instruments: csv += csv_format.format(count=count, file_name=instrument.name, length=instrument.seft_file.len if instrument.seft_file else None, date_stamp=instrument.stamp) count += 1 return csv
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
def test_validate_uuid_error(self): # Given a invalid uuid uuid = "invalid_uuid" # When a call is made to validate_uuid # Then an RasError is raised with self.assertRaises(RasError): validate_uuid(uuid)
def get_instrument_by_id(instrument_id, session): """ Get the collection instrument from the db using the id :param instrument_id: The id of the instrument we want :param session: database session :return: instrument """ log.info('Searching for instrument', instrument_id=instrument_id) validate_uuid(instrument_id) instrument = query_instrument_by_id(instrument_id, session) return instrument
def get_exercise_by_id(exercise_id, session): """ Retrieves exercise :param exercise_id: An exercise id (UUID) :param session: database session :return: exercise """ log.info('Searching for exercise', exercise_id=exercise_id) validate_uuid(exercise_id) exercise = query_exercise_by_id(exercise_id, session) return exercise
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 get_instruments_by_exercise_id_csv(exercise_id, session=None): """ Finds all collection instruments associated with an exercise and returns them in csv format :param exercise_id :param session: database session :return: collection instruments in csv """ log.info("Getting csv for instruments", exercise_id=exercise_id) validate_uuid(exercise_id) csv_format = '"{count}","{file_name}","{length}","{date_stamp}"\n' count = 1 csv = csv_format.format(count="Count", file_name="File Name", length="Length", date_stamp="Time Stamp") exercise = query_exercise_by_id(exercise_id, session) if not exercise: return None for instrument in exercise.instruments: if instrument.seft_file.gcs: try: survey_ref = get_survey_ref(instrument.survey.survey_id) exercise_id = str(instrument.exids[0]) file_path = survey_ref + "/" + exercise_id + "/" + instrument.seft_file.file_name seft_ci_bucket = GoogleCloudSEFTCIBucket(current_app.config) file = seft_ci_bucket.download_file_from_bucket(file_path) csv += csv_format.format( count=count, file_name=instrument.seft_file.file_name, length=len(file), date_stamp=instrument.stamp, ) except Exception: log.exception("Couldn't find SEFT CI in bucket") else: csv += csv_format.format( count=count, file_name=instrument.name, length=instrument.seft_file.len if instrument.seft_file else None, date_stamp=instrument.stamp, ) count += 1 return csv
def link_instrument_to_exercise(self, instrument_id, exercise_id, session=None): """ Link a collection instrument to a collection exercise :param instrument_id: A collection instrument id (UUID) :param exercise_id: A collection exercise id (UUID) :param session: database session :return: True if instrument has been successfully linked to exercise """ log.info('Linking instrument to exercise', instrument_id=instrument_id, exercise_id=exercise_id) validate_uuid(instrument_id) validate_uuid(exercise_id) instrument = self.get_instrument_by_id(instrument_id, session) exercise = self._find_or_create_exercise(exercise_id, session) instrument.exercises.append(exercise) log.info('Successfully linked instrument to exercise', instrument_id=instrument_id, exercise_id=exercise_id) return True
def test_validate_uuid(self): # Given a valid uuid uuid = "6710e50e-224b-4918-9706-c6b28f7481cd" # When a call is made to validate_uuid is_valid_uuid = validate_uuid(uuid) # Then True is returned self.assertTrue(is_valid_uuid)
def patch_seft_instrument(self, instrument_id: str, file, session): """ Replaces the the seft_file for an instrument with the one provided. :param instrument_id: The top level instrument id that needs changing :param file: A FileStorage object with the new file :param session: A database session :raises RasError: Raised when instrument id is invalid, instrument not found, or instrument isn't of type SEFT """ validate_uuid(instrument_id) instrument = self.get_instrument_by_id(instrument_id, session) if instrument is None: log.error('Instrument not found') raise RasError('Instrument not found', 400) if instrument.type != 'SEFT': log.error('Not a SEFT instrument') raise RasError('Not a SEFT instrument', 400) seft_model = self._update_seft_file(instrument.seft_file, file) session.add(seft_model)
def patch_seft_instrument(self, instrument_id: str, file, session): """ Replaces the the seft_file for an instrument with the one provided. :param instrument_id: The top level instrument id that needs changing :param file: A FileStorage object with the new file :param session: A database session :raises RasError: Raised when instrument id is invalid, instrument not found, or instrument isn't of type SEFT """ validate_uuid(instrument_id) instrument = self.get_instrument_by_id(instrument_id, session) if instrument is None: log.error("Instrument not found") raise RasError("Instrument not found", 400) if instrument.type != "SEFT": log.error("Not a SEFT instrument") raise RasError("Not a SEFT instrument", 400) survey_ref = get_survey_ref(instrument.survey.survey_id) exercise_id = str(instrument.exids[0]) seft_model = self._update_seft_file(instrument.seft_file, file, survey_ref, exercise_id) session.add(seft_model)
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