Exemplo n.º 1
0
    def mutate(self, args, context, info):
        index = args.get('index')
        name = args.get('name')
        sample_group_id = args.get('sample_group_id')
        plant = PlantModel(index, name, sample_group_id)

        try:
            db.session.add(plant)
            db.session.flush()
        except IntegrityError as err:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")
        except DBAPIError:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")
        db.session.commit()

        return CreatePlant(id=plant.id,
                           index=plant.index,
                           name=plant.name,
                           sample_group_id=plant.sample_group_id)
Exemplo n.º 2
0
    def mutate(self, args, context, info):
        exp_data = args.get('experiment_data')
        sample_group_data = exp_data.get('sample_group_data')

        try:
            experiment = ExperimentModel(
                exp_data.get('name'), exp_data.get('description'),
                exp_data.get('scientist'), exp_data.get('group_name'),
                exp_data.get('start_date'),
                exp_data.get('start_of_experimentation'))
            db.session.add(experiment)
            db.session.flush()
            for group_data in sample_group_data:
                group = SampleGroupModel.fromdict(group_data, experiment.id)
                db.session.add(group)
                db.session.flush()
                for plant_data in group_data.get('plants'):
                    plant = PlantModel(plant_data.get('index'),
                                       plant_data.get('name'), group.id)
                    db.session.add(plant)
        except DBAPIError as err:
            #TODO add unique constraint checks
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")

        db.session.commit()
        return ConstructExperiment(
            id=to_global_id('Experiment', experiment.id))
Exemplo n.º 3
0
 def mutate(self, args, context, info):
     identity = get_jwt_identity()
     proj_data = args.get('project_data')
     _, exp_id = from_global_id(proj_data.pop('id'))
     with db.session.no_autoflush:
         try:
             experiment = db.session.query(ExperimentModel).get(exp_id)
             if experiment.scientist == identity.get(
                     'username') or is_admin(identity):
                 EditProject._edit_experiment(proj_data, experiment)
             else:
                 raise ForbiddenActionError(
                     "Unable to edit experiment {} by user {}. Insufficient privileges."
                     .format(experiment.name, identity.get('username')),
                     identity.get('username'))
                 # User is not allowed to edit this project
             db.session.commit()
             return EditProject(experiment=experiment)
         except IntegrityError as err:
             db.session.rollback()
             #TODO add experiment name constraint
             if err.orig.diag.constraint_name == u'uq_sample_groups_treatment_experiment_id':
                 raise ConstraintViolationError(
                     'It is not allowed to have two sample groups with the same '
                     'treatment.')
             elif err.orig.diag.constraint_name == u'sample_group_name_experiment_id_key':
                 raise ConstraintViolationError(
                     'It is not allowed to have two sample groups with the same '
                     'name.')
         except DBAPIError as err:
             logging.getLogger(__name__).exception(
                 "An unexpected DB error occured")
             db.session.rollback()
             raise UnknownDataError("An unexpected DB error occured")
    def mutate(self, args, context, info):
        plant_id = args.get('plant_id')
        _, plant_db_id = from_global_id(plant_id)
        plant = db.session.query(PlantModel).get(plant_db_id)
        experiment_id = plant.sample_group.experiment_id
        timestamp, created = TimestampModel.get_or_create(experiment_id)
        snapshot = SnapshotModel(plant_id=plant_db_id,
                                 timestamp_id=timestamp.id,
                                 camera_position=args.get('camera_position'),
                                 measurement_tool=args.get('measurement_tool'),
                                 phenobox_id=args.get('phenobox_id'))

        try:
            db.session.add(snapshot)
            db.session.flush()
        except IntegrityError as err:
            db.session.rollback()
            if err.orig.diag.constraint_name == u'uq_snapshot_plant_id_timestamp_id':
                raise ConflictingDataError(
                    'There already exists a snapshot for this plant and timestamp'
                )
        except DBAPIError as err:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")
        db.session.commit()

        return CreateSnapshot(id=to_global_id('Snapshot', snapshot.id),
                              timestamp_id=to_global_id(
                                  'Timestamp', timestamp.id),
                              new_timestamp=created)
Exemplo n.º 5
0
    def mutate(self, args, context, info):
        ql_id = args.get('id')
        _, id = from_global_id(ql_id)
        try:
            db.session.delete(db.session.query(PostprocessModel).get(id))
            db.session.commit()
        except DBAPIError:
            logging.getLogger(__name__).exception("An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")

        return DeletePostprocess(id=ql_id)
Exemplo n.º 6
0
 def mutate(self, args, context, info):
     try:
         timestamp_id = args.get('timestamp_id')
         _, timestamp_db_id = from_global_id(timestamp_id)
         timestamp = db.session.query(TimestampModel).get(timestamp_db_id)
         setattr(timestamp, 'completed', True)
         db.session.flush()
         db.session.commit()
         return CompleteTimestamp(id=timestamp_id)
     except DBAPIError:
         logging.getLogger(__name__).exception(
             "An unexpected DB error occured")
         db.session.rollback()
         raise UnknownDataError("An unexpected DB error occured")
Exemplo n.º 7
0
    def mutate(self, args, context, info):
        ql_id = args.get('id')
        _, db_id = from_global_id(ql_id)
        snapshot = db.session.query(SnapshotModel).get(db_id)
        if snapshot is None:
            return  # Exception to indicate this entry does not exist
        try:
            snapshot.purge()
        except DBAPIError as err:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")

        return DeleteSnapshot(id=ql_id)
Exemplo n.º 8
0
 def mutate(self, args, context, info):
     sample_group = SampleGroupModel.fromdict(args.get('group_data'), args.get('experiment_id'))
     try:
         db.session.add(sample_group)
         db.session.flush()
     except IntegrityError as err:
         logging.getLogger(__name__).exception("An unexpected DB error occured")
         db.session.rollback()
         raise UnknownDataError("An unexpected DB error occured")
     db.session.commit()
     return CreateSampleGroup(id=sample_group.id, name=sample_group.name, treatment=sample_group.treatment,
                              description=sample_group.description,
                              species=sample_group.speciesm, genotype=sample_group.genotype,
                              variety=sample_group.variety,
                              growth_conditions=sample_group.growth_conditions, is_control=sample_group.is_control,
                              experiment_id=sample_group.experiment_id)
Exemplo n.º 9
0
    def mutate(self, args, context, info):
        snapshot_id = args.get('snapshot_id')
        _, snapshot_db_id = from_global_id(snapshot_id)
        try:
            snapshot = db.session.query(SnapshotModel).get(snapshot_db_id)
            snapshot.excluded = args.get('exclude')

            db.session.add(snapshot)
            db.session.flush()
        except DBAPIError as err:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")
        db.session.commit()

        return ChangeSnapshotExclusion(id=snapshot_id,
                                       excluded=args.get('exclude'))
Exemplo n.º 10
0
    def mutate(self, args, context, info):
        ql_id = args.get('id')
        _, id = from_global_id(ql_id)
        try:
            experiment = db.session.query(ExperimentModel).get(id)
            if len(experiment.timestamps) > 0:
                raise UnableToDeleteError(
                    "Experiment could not be deleted because there are timestamps associated with it"
                )
            db.session.delete(experiment)
            db.session.commit()
        except DBAPIError as err:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")

        return DeleteExperiment(id=ql_id)
Exemplo n.º 11
0
    def mutate(self, args, context, info):
        name = args.get('name')
        description = args.get('description')
        scientist = args.get('scientist')
        group_name = args.get('group_name')
        experiment = ExperimentModel(name, description, scientist, group_name)
        try:
            db.session.add(experiment)
            db.session.flush()
        except DBAPIError as err:
            logging.getLogger(__name__).exception(
                "An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")

        db.session.commit()
        return CreateExperiment(id=experiment.id,
                                name=experiment.name,
                                description=experiment.description,
                                scientist=experiment.scientist,
                                group_name=group_name)
Exemplo n.º 12
0
    def mutate(self, args, context, info):
        snapshot_id = args.get('snapshot_id')
        _, snapshot_db_id = from_global_id(snapshot_id)
        t = args.get('type')
        if t is not None and t.strip() == '':
            t = None
        image = ImageModel(snapshot_id=snapshot_db_id, path=args.get('path'), filename=args.get('filename'),
                           angle=args.get('angle'),
                           image_type=t)

        try:
            db.session.add(image)
            db.session.flush()
        except IntegrityError as err:
            print(err.message)
            db.session.rollback()
            raise ConflictingDataError("Image already exists")
        except DBAPIError:
            logging.getLogger(__name__).exception("An unexpected DB error occured")
            db.session.rollback()
            raise UnknownDataError("An unexpected DB error occured")
        db.session.commit()

        return AddImage(id=to_global_id('Image', image.id))