Esempio n. 1
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
def create_dummy_kit(account_id=None, source_id=None):
    with Transaction() as t:
        _create_mock_kit(t, barcodes=[BARCODE],
                         mock_sample_ids=[MOCK_SAMPLE_ID])

        # if an account and source were provided, put some dummy
        # collection info into the sample and associate it to this source
        if account_id is not None and source_id is not None:

            sample_info, _ = create_dummy_sample_objects(True)
            sample_repo = SampleRepo(t)
            sample_repo.associate_sample(account_id, source_id, MOCK_SAMPLE_ID)
            sample_repo.update_info(account_id, source_id, sample_info)

        t.commit()