Exemplo n.º 1
0
    def _do_update_with_patch(self, session, order, resource,
                              expected_version):
        self._validate_patch_update(order, resource, expected_version)
        order.lastModified = clock.CLOCK.now()
        order.logPosition = LogPosition()
        order.version += 1
        if resource['status'].lower() == 'cancelled':
            order.amendedReason = resource['amendedReason']
            order.cancelledUsername = resource['cancelledInfo']['author'][
                'value']
            order.cancelledSiteId = get_site(resource['cancelledInfo'])
            order.cancelledTime = clock.CLOCK.now()
            order.orderStatus = BiobankOrderStatus.CANCELLED
        elif resource['status'].lower() == 'restored':
            order.amendedReason = resource['amendedReason']
            order.restoredUsername = resource['restoredInfo']['author'][
                'value']
            order.restoredSiteId = get_site(resource['restoredInfo'])
            order.restoredTime = clock.CLOCK.now()
            order.orderStatus = BiobankOrderStatus.UNSET
        else:
            raise BadRequest(
                'status must be restored or cancelled for patch request.')

        super(BiobankOrderDao, self)._do_update(session, order, resource)
        self._update_history(session, order)
        self._refresh_participant_summary(session, order)
        return order
Exemplo n.º 2
0
 def insert_with_session(self, session, obj):
     obj.version = 1
     if obj.logPosition is not None:
         raise BadRequest('%s.logPosition must be auto-generated.' %
                          self.model_type.__name__)
     obj.logPosition = LogPosition()
     if obj.biobankOrderId is None:
         raise BadRequest('Client must supply biobankOrderId.')
     existing_order = self.get_with_children_in_session(
         session, obj.biobankOrderId)
     if existing_order:
         existing_order_dict = self._order_as_dict(existing_order)
         new_dict = self._order_as_dict(obj)
         if existing_order_dict == new_dict:
             # If an existing matching order exists, just return it without trying to create it again.
             return existing_order
         else:
             raise Conflict('Order with ID %s already exists' %
                            obj.biobankOrderId)
     self._update_participant_summary(session, obj)
     inserted_obj = super(BiobankOrderDao,
                          self).insert_with_session(session, obj)
     if inserted_obj.collectedSiteId is not None:
         ParticipantDao().add_missing_hpo_from_site(
             session, inserted_obj.participantId,
             inserted_obj.collectedSiteId)
     self._update_history(session, obj)
     return inserted_obj
    def insert_with_session(self, session, obj):
        is_amendment = False
        obj.logPosition = LogPosition()
        obj.final = True
        obj.created = clock.CLOCK.now()
        resource_json = json.loads(obj.resource)
        finalized_date = resource_json['entry'][0]['resource'].get('date')
        if finalized_date:
            obj.finalized = parse_date(finalized_date)
        for extension in resource_json['entry'][0]['resource'].get(
                'extension', []):
            url = extension.get('url')
            if url not in _ALL_EXTENSIONS:
                logging.info(
                    'Ignoring unsupported extension for PhysicalMeasurements: %r. Expected one of: %s',
                    url, _ALL_EXTENSIONS)
                continue
            if url == _AMENDMENT_URL:
                self._update_amended(obj, extension, url, session)
                is_amendment = True
                break
        participant_summary = self._update_participant_summary(
            session, obj, is_amendment)
        existing_measurements = (session.query(PhysicalMeasurements).filter(
            PhysicalMeasurements.participantId == obj.participantId).all())
        if existing_measurements:
            new_dict = self._measurements_as_dict(obj)
            for measurements in existing_measurements:
                if self._measurements_as_dict(measurements) == new_dict:
                    # If there are already measurements that look exactly like this, return them
                    # without inserting new measurements.
                    return measurements
        PhysicalMeasurementsDao.set_measurement_ids(obj)

        inserted_obj = super(PhysicalMeasurementsDao,
                             self).insert_with_session(session, obj)
        if not is_amendment:  # Amendments aren't expected to have site ID extensions.
            if participant_summary.biospecimenCollectedSiteId is None:
                ParticipantDao().add_missing_hpo_from_site(
                    session, inserted_obj.participantId,
                    inserted_obj.finalizedSiteId)

        # Flush to assign an ID to the measurements, as the client doesn't provide one.
        session.flush()
        # Update the resource to contain the ID.
        resource_json['id'] = str(obj.physicalMeasurementsId)
        obj.resource = json.dumps(resource_json)
        return obj
Exemplo n.º 4
0
    def test_schema_biobank_order_and_datetime_roundtrip(self):
        bo_id = 1
        now = isodate.parse_datetime('2016-01-04T10:28:50-04:00')

        write_session = self.database.make_session()

        p = self._create_participant(write_session)
        site = Site(siteId=1,
                    siteName='site',
                    googleGroup='*****@*****.**',
                    mayolinkClientNumber=12345,
                    hpoId=1)
        write_session.add(site)
        write_session.commit()

        bo = BiobankOrder(biobankOrderId=bo_id,
                          participantId=p.participantId,
                          created=now,
                          sourceSiteId=1,
                          version=1,
                          sourceUsername='******',
                          collectedSiteId=1,
                          collectedUsername='******',
                          processedSiteId=1,
                          processedUsername='******',
                          finalizedSiteId=1,
                          finalizedUsername='******',
                          collectedNote=r'written by ' + self.fake.last_name(),
                          processedNote=u'd',
                          finalizedNote=u'e',
                          logPosition=LogPosition())
        bo.identifiers.append(BiobankOrderIdentifier(system='a', value='b'))
        bo.samples.append(
            BiobankOrderedSample(test='a',
                                 description=u'a test invented by ' +
                                 self.fake.first_name(),
                                 processingRequired=True,
                                 collected=now,
                                 processed=now,
                                 finalized=now))
        write_session.add(bo)
        write_session.commit()

        read_session = self.database.make_session()
        bo = read_session.query(BiobankOrder).get(bo_id)
        self.assertEquals(
            bo.created.isoformat(),
            now.astimezone(tzutc()).replace(tzinfo=None).isoformat())
Exemplo n.º 5
0
    def _do_update(self, session, order, existing_obj):
        order.lastModified = clock.CLOCK.now()
        order.biobankOrderId = existing_obj.biobankOrderId
        order.orderStatus = BiobankOrderStatus.AMENDED
        if hasattr(existing_obj, 'amendedInfo'
                   ) and existing_obj.amendedInfo.get('author') is not None:
            order.amendedUsername = existing_obj.amendedInfo.get('author').get(
                'value')
        if hasattr(existing_obj, 'amendedInfo'):
            order.amendedSiteId = get_site(existing_obj.amendedInfo)
        order.amendedTime = clock.CLOCK.now()
        order.logPosition = LogPosition()
        order.version += 1
        # Ensure that if an order was previously cancelled/restored those columns are removed.
        self._clear_cancelled_and_restored_fields(order)

        super(BiobankOrderDao, self)._do_update(session, order, existing_obj)
        session.add(order.logPosition)

        self._refresh_participant_summary(session, order)
        self._update_history(session, order)
Exemplo n.º 6
0
    def test_schema(self):
        session = self.database.make_session()

        hpo = HPO(hpoId=1,
                  name='UNSET',
                  displayName='No organization set',
                  organizationType=OrganizationType.UNSET)
        code_book = CodeBook(codeBookId=1,
                             created=datetime.datetime.now(),
                             latest=True,
                             name="pmi",
                             system="http://foo/bar",
                             version="v0.1.1")
        session.add(hpo)
        session.add(code_book)
        session.commit()

        organization = Organization(organizationId=1,
                                    externalId='org',
                                    displayName='Organization',
                                    hpoId=1)
        session.add(organization)
        session.commit()

        site = Site(siteId=1,
                    siteName='site',
                    googleGroup='*****@*****.**',
                    mayolinkClientNumber=12345,
                    organizationId=1)
        code1 = Code(codeId=1,
                     codeBookId=1,
                     system="a",
                     value="b",
                     shortValue="q",
                     display=u"c",
                     topic=u"d",
                     codeType=CodeType.MODULE,
                     mapped=True,
                     created=datetime.datetime.now())
        codeHistory1 = CodeHistory(codeId=1,
                                   codeBookId=1,
                                   system="a",
                                   value="b",
                                   shortValue="q",
                                   display=u"c",
                                   topic=u"d",
                                   codeType=CodeType.MODULE,
                                   mapped=True,
                                   created=datetime.datetime.now())
        session.add(site)
        session.add(code1)
        session.add(codeHistory1)
        session.commit()

        code2 = Code(codeId=2,
                     codeBookId=1,
                     parentId=1,
                     system="a",
                     value="c",
                     display=u"X",
                     topic=u"d",
                     codeType=CodeType.QUESTION,
                     mapped=True,
                     created=datetime.datetime.now())
        codeHistory2 = CodeHistory(codeId=2,
                                   codeBookId=1,
                                   parentId=1,
                                   system="a",
                                   value="c",
                                   display=u"X",
                                   topic=u"d",
                                   codeType=CodeType.QUESTION,
                                   mapped=True,
                                   created=datetime.datetime.now())
        session.add(code2)
        session.add(codeHistory2)
        session.commit()

        code3 = Code(codeId=3,
                     codeBookId=1,
                     parentId=2,
                     system="a",
                     value="d",
                     display=u"Y",
                     topic=u"d",
                     codeType=CodeType.ANSWER,
                     mapped=False,
                     created=datetime.datetime.now())
        codeHistory3 = CodeHistory(codeId=3,
                                   codeBookId=1,
                                   parentId=2,
                                   system="a",
                                   value="d",
                                   display=u"Y",
                                   topic=u"d",
                                   codeType=CodeType.ANSWER,
                                   mapped=False,
                                   created=datetime.datetime.now())
        session.add(code3)
        session.add(codeHistory3)
        session.commit()

        session.commit()

        p = self._participant_with_defaults(
            participantId=1,
            version=1,
            biobankId=2,
            clientId='*****@*****.**',
            hpoId=hpo.hpoId,
            signUpTime=datetime.datetime.now(),
            lastModified=datetime.datetime.now())
        ps = self._participant_summary_with_defaults(
            participantId=1,
            biobankId=2,
            lastModified=datetime.datetime.now(),
            hpoId=hpo.hpoId,
            firstName=self.fake.first_name(),
            middleName=self.fake.first_name(),
            lastName=self.fake.last_name(),
            email=self.fake.email(),
            zipCode='78751',
            dateOfBirth=datetime.date.today(),
            genderIdentityId=1,
            consentForStudyEnrollment=QuestionnaireStatus.SUBMITTED,
            consentForStudyEnrollmentTime=datetime.datetime.now(),
            numBaselineSamplesArrived=2)
        p.participantSummary = ps
        session.add(p)
        ph = self._participant_history_with_defaults(
            participantId=1,
            biobankId=2,
            clientId='*****@*****.**',
            hpoId=hpo.hpoId,
            signUpTime=datetime.datetime.now(),
            lastModified=datetime.datetime.now())
        session.add(ph)
        session.commit()

        session.add(
            BiobankStoredSample(biobankStoredSampleId='WEB1234542',
                                biobankId=p.biobankId,
                                biobankOrderIdentifier='KIT',
                                test='1UR10',
                                confirmed=datetime.datetime.utcnow()))
        session.add(
            BiobankStoredSample(
                biobankStoredSampleId='WEB99999',  # Sample ID must be unique.
                biobankId=p.
                biobankId,  # Participant ID and test may be duplicated.
                biobankOrderIdentifier='KIT',
                test='1UR10',
                confirmed=datetime.datetime.utcnow()))

        pm = PhysicalMeasurements(physicalMeasurementsId=1,
                                  participantId=1,
                                  created=datetime.datetime.now(),
                                  resource='blah',
                                  final=False,
                                  logPosition=LogPosition())
        pm2 = PhysicalMeasurements(physicalMeasurementsId=2,
                                   participantId=1,
                                   created=datetime.datetime.now(),
                                   resource='blah',
                                   final=True,
                                   amendedMeasurementsId=1,
                                   logPosition=LogPosition())
        session.add(pm)
        session.add(pm2)
        session.commit()

        q1 = Measurement(measurementId=3,
                         physicalMeasurementsId=pm.physicalMeasurementsId,
                         codeSystem='codeSystem',
                         codeValue='codeValue',
                         measurementTime=datetime.datetime.now(),
                         valueCodeSystem='valueCodeSystem',
                         valueCodeValue='value3')
        session.add(q1)
        session.commit()

        m1 = Measurement(measurementId=1,
                         physicalMeasurementsId=pm.physicalMeasurementsId,
                         codeSystem='codeSystem',
                         codeValue='codeValue',
                         measurementTime=datetime.datetime.now(),
                         bodySiteCodeSystem='bodySiteCodeSystem',
                         bodySiteCodeValue='bodySiteCodeValue',
                         valueString='a',
                         valueDecimal=1.2,
                         valueUnit='cm',
                         valueCodeSystem='valueCodeSystem',
                         valueCodeValue='value',
                         valueDateTime=datetime.datetime.now(),
                         qualifierId=q1.measurementId)
        session.add(m1)
        session.commit()

        m2 = Measurement(measurementId=2,
                         physicalMeasurementsId=pm.physicalMeasurementsId,
                         codeSystem='codeSystem',
                         codeValue='codeValue',
                         measurementTime=datetime.datetime.now(),
                         valueCodeSystem='valueCodeSystem',
                         valueCodeValue='value2',
                         parentId=m1.measurementId,
                         qualifierId=q1.measurementId)
        session.add(m2)
        session.commit()

        q = Questionnaire(questionnaireId=1,
                          version=1,
                          created=datetime.datetime.now(),
                          lastModified=datetime.datetime.now(),
                          resource='what?')
        qh = QuestionnaireHistory(questionnaireId=1,
                                  version=1,
                                  created=datetime.datetime.now(),
                                  lastModified=datetime.datetime.now(),
                                  resource='what?')
        qh.questions.append(
            QuestionnaireQuestion(questionnaireQuestionId=1,
                                  questionnaireId=1,
                                  questionnaireVersion=1,
                                  linkId="1.2.3",
                                  codeId=2,
                                  repeats=True))
        qh.concepts.append(
            QuestionnaireConcept(questionnaireConceptId=1,
                                 questionnaireId=1,
                                 questionnaireVersion=1,
                                 codeId=1))
        session.add(q)
        session.add(qh)
        session.commit()

        qr = QuestionnaireResponse(questionnaireResponseId=1,
                                   questionnaireId=1,
                                   questionnaireVersion=1,
                                   participantId=1,
                                   created=datetime.datetime.now(),
                                   resource='blah')
        qr.answers.append(
            QuestionnaireResponseAnswer(questionnaireResponseAnswerId=1,
                                        questionnaireResponseId=1,
                                        questionId=1,
                                        endTime=datetime.datetime.now(),
                                        valueSystem='a',
                                        valueCodeId=3,
                                        valueDecimal=123,
                                        valueString=self.fake.first_name(),
                                        valueDate=datetime.date.today()))

        session.add(qr)
        session.commit()

        mv = MetricsVersion(metricsVersionId=1,
                            inProgress=False,
                            complete=True,
                            date=datetime.datetime.utcnow(),
                            dataVersion=1)
        session.add(mv)
        session.commit()

        mb = MetricsBucket(metricsVersionId=1,
                           date=datetime.date.today(),
                           hpoId='PITT',
                           metrics='blah')
        session.add(mb)
        session.commit()