def samples(self): """ Get SampleGroup's associated Samples. This will hit Mongo every time it is called! Responsibility for caching the result lies on the calling method. """ return Sample.objects(uuid__in=self.sample_ids)
def add_sample(resp): # pylint: disable=unused-argument """Add sample.""" try: post_data = request.get_json() sample_group_uuid = post_data['sample_group_uuid'] sample_name = post_data['name'] except TypeError: raise ParseError('Missing Sample creation payload.') except KeyError: raise ParseError('Invalid Sample creation payload.') try: sample_group = SampleGroup.query.filter_by(id=sample_group_uuid).one() except NoResultFound: raise InvalidRequest('Sample Group does not exist!') sample = Sample.objects(name=sample_name).first() if sample is not None: raise InvalidRequest('A Sample with that name already exists.') try: analysis_result = AnalysisResultMeta().save() sample = Sample(name=sample_name, analysis_result=analysis_result, metadata={ 'name': sample_name }).save() sample_group.sample_ids.append(sample.uuid) db.session.commit() result = sample_schema.dump(sample).data return result, 201 except ValidationError as validation_error: current_app.logger.exception('Sample could not be created.') raise InternalError(str(validation_error)) except IntegrityError as integrity_error: current_app.logger.exception( 'Sample could not be added to Sample Group.') db.session.rollback() raise InternalError(str(integrity_error))