Exemplo n.º 1
0
    def dissociate_sample(self,
                          account_id,
                          source_id,
                          sample_id,
                          override_locked=False):
        existing_sample = self.get_sample(account_id, source_id, sample_id)
        if existing_sample is None:
            raise werkzeug.exceptions.NotFound("No sample ID: %s" % sample_id)

        if existing_sample.edit_locked and not override_locked:
            raise RepoException(
                "Sample information locked: Sample already evaluated for "
                "processing")

        # Wipe any user entered fields from the sample:
        self.update_info(account_id,
                         source_id,
                         SampleInfo(sample_id, None, None, None),
                         override_locked=override_locked)

        if existing_sample.remove_locked and not override_locked:
            raise RepoException(
                "Sample association locked: Sample already received")

        # And detach the sample from the source
        self._update_sample_association(sample_id,
                                        None,
                                        override_locked=override_locked)
Exemplo n.º 2
0
def update_sample_association(account_id, source_id, sample_id, body,
                              token_info):
    _validate_account_access(token_info, account_id)

    # TODO: API layer doesn't understand that BadRequest can be thrown,
    #  but that looks to be the right result if sample_site bad.
    #  Need to update the api layer if we want to specify 400s.
    #  (Or we leave api as is and say 400's can always be thrown if your
    #  request is bad)
    with Transaction() as t:
        sample_repo = SampleRepo(t)
        source_repo = SourceRepo(t)

        source = source_repo.get_source(account_id, source_id)
        if source is None:
            return jsonify(code=404, message="No such source"), 404

        needs_sample_site = source.source_type in [
            Source.SOURCE_TYPE_HUMAN, Source.SOURCE_TYPE_ANIMAL
        ]

        precludes_sample_site = source.source_type == \
            Source.SOURCE_TYPE_ENVIRONMENT

        sample_site_present = "sample_site" in body and \
                              body["sample_site"] is not None

        if needs_sample_site and not sample_site_present:
            # Human/Animal sources require sample_site to be set
            raise BadRequest("human/animal samples require sample_site")
        if precludes_sample_site and sample_site_present:
            raise BadRequest("environmental samples cannot specify "
                             "sample_site")

        sample_datetime = body['sample_datetime']
        try:
            sample_datetime = fromisotime(sample_datetime)
        except ValueError:
            raise BadRequest("Invalid sample_datetime")
        curdate = datetime.now(sample_datetime.tzinfo)
        lower_limit = curdate + relativedelta(years=-10)
        upper_limit = curdate + relativedelta(months=+1)
        if sample_datetime < lower_limit or sample_datetime > upper_limit:
            raise BadRequest('Invalid sample date')
        # sample_site will not be present if its environmental. this will
        # default to None if the key is not present
        sample_site = body.get('sample_site')
        sample_info = SampleInfo(sample_id, sample_datetime, sample_site,
                                 body["sample_notes"])

        is_admin = token_grants_admin_access(token_info)
        sample_repo.update_info(account_id,
                                source_id,
                                sample_info,
                                override_locked=is_admin)

        final_sample = sample_repo.get_sample(account_id, source_id, sample_id)
        t.commit()
    return jsonify(final_sample), 200
Exemplo n.º 3
0
    def dissociate_sample(self, account_id, source_id, sample_id):
        existing_sample = self.get_sample(account_id, source_id, sample_id)
        if existing_sample is None:
            raise werkzeug.exceptions.NotFound("No sample ID: %s" % sample_id)
        if existing_sample.is_locked:
            raise RepoException("Sample edits locked: Sample already received")

        # Wipe any user entered fields from the sample:
        self.update_info(account_id, source_id,
                         SampleInfo(sample_id, None, None, None))

        # And detach the sample from the source
        self._update_sample_association(sample_id, None)
def create_dummy_sample_objects(filled=False):
    info_dict = DUMMY_FILLED_SAMPLE_INFO if filled else DUMMY_EMPTY_SAMPLE_INFO
    datetime_str = info_dict["sample_datetime"]
    datetime_obj = None if datetime_str is None else isoparse(datetime_str)

    sample_info = SampleInfo(
        info_dict["sample_id"],
        datetime_obj,
        info_dict["sample_site"],
        info_dict["sample_notes"]
    )

    sample = Sample(info_dict["sample_id"],
                    datetime_obj,
                    info_dict["sample_site"],
                    info_dict["sample_notes"],
                    info_dict["sample_barcode"],
                    None,
                    info_dict["sample_projects"])

    return sample_info, sample