예제 #1
0
    def test_regular_srf(self):
        form_builder = FormBuilderJson()
        # Redcap metadata fixture
        metadata = open(
            os.path.join(os.path.dirname(__file__),
                         'fixtures/regular_project_metadata.json'),
            'rb').read()
        # Redcap record fixture
        record_set = open(
            os.path.join(os.path.dirname(__file__),
                         'fixtures/regular_record_set.json'), 'rb').read()
        # Construct form from fixtures
        html = form_builder.construct_form(json.loads(metadata),
                                           json.loads(record_set),
                                           'demographics', '1')
        # Load test fixture
        fixture = open(
            os.path.join(os.path.dirname(__file__),
                         'fixtures/test_regular_srf.html'), 'rb').read()

        self.assertEqual(html, fixture)
    def test_regular_srf(self):
        form_builder = FormBuilderJson()
        # Redcap metadata fixture
        metadata = open(
            os.path.join(
                os.path.dirname(__file__),
                'fixtures/regular_project_metadata.json'), 'rb').read()
        # Redcap record fixture
        record_set = open(
            os.path.join(
                os.path.dirname(__file__),
                'fixtures/regular_record_set.json'), 'rb').read()
        # Construct form from fixtures
        html = form_builder.construct_form(json.loads(metadata),
                                           json.loads(record_set),
                                           'demographics',
                                           '1')
        # Load test fixture
        fixture = open(os.path.join(
            os.path.dirname(__file__),
            'fixtures/test_regular_srf.html'), 'rb').read()

        self.assertEqual(html, fixture)
예제 #3
0
    def subRecordForm(self, external_record, form_spec="", *args, **kwargs):
        """
        Generates a REDCap data entry form for a specific ExternalRecord and
        REDCap Form and event. It is necessary to call configure before calling
        this method.

        Required Inputs (kwargs):
        -------------------------

        * external_record = ExternalRecord object from ehb_client
        * form_spec = String in the form N_M
            N is the form number (0 indexed)
            M is the event number (0 indexed).

        The form and event numbers are mapped to form names and event names in
        the order they were provided in the call to configure

        If the REDCap project is not longitudinal (i.e. Survey or Data Forms
        Classic) the event number is not required and will be ignored if
        included

        Optional Inputs:
        ----------------

        session = the session var.

        If provided the driver will use the session var to cache form field
        names which improves save time performance
        """

        er = external_record

        # make sure the proper information has been provided and that configure
        #  has been called
        if not er or not form_spec:
            return None
        if not (self.event_labels and self.unique_event_names and self.form_data) and not self.form_names:
            return None
        # make sure form_spec is of the correct format, N_M, negatives NOT
        # allowed for non longitudinal studies event number is not required,
        # is ok if supplied it will be ignored
        if self.form_names and not (re.match(r"^\d+$", form_spec) or re.match(r"^\d+_\d+$", form_spec)):
            return None

        split = form_spec.split("_")
        form_num = int(split[0])

        if not self.form_names:
            event_num = int(split[1])

        # Make sure the form and event indices are inbounds
        if self.form_names and form_num > (len(self.form_names) - 1):
            return None
        elif not self.form_names and (
            form_num > (len(self.form_data_ordered) - 1) or event_num > (len(self.unique_event_names) - 1)
        ):
            return None

        form_name = self.form_data_ordered[form_num]
        # need to get the meta data from REDCAp to construct the form and the
        # record to populate previously entered values
        form_builder = FormBuilderJson()
        meta_data = self.raw_to_json(self.meta(_format=self.FORMAT_JSON, rawResponse=True))
        session = kwargs.get("session", None)

        if self.form_names:
            record_set = (
                self.get(_format=self.FORMAT_JSON, records=[er.record_id], rawResponse=True, forms=[form_name])
                .read()
                .strip()
            )
            record_set = self.raw_to_json(record_set)
            return form_builder.construct_form(
                meta_data, record_set, form_name, er.record_id, None, None, None, session, self.record_id_field_name
            )
        else:
            temp = self.get(_format=self.FORMAT_JSON, rawResponse=True, records=[er.record_id])
            record_set = temp.read().strip()
            record_set = self.raw_to_json(record_set)
            return form_builder.construct_form(
                meta_data,
                record_set,
                form_name,
                er.record_id,
                event_num,
                self.unique_event_names,
                self.event_labels,
                session,
                self.record_id_field_name,
            )
예제 #4
0
    def subRecordForm(self, external_record, form_spec='', *args, **kwargs):
        '''
        Generates a REDCap data entry form for a specific ExternalRecord and
        REDCap Form and event. It is necessary to call configure before calling
        this method.

        Required Inputs (kwargs):
        -------------------------

        * external_record = ExternalRecord object from ehb_client
        * form_spec = String in the form N_M
            N is the form number (0 indexed)
            M is the event number (0 indexed).

        The form and event numbers are mapped to form names and event names in
        the order they were provided in the call to configure
        If the REDCap project is not longitudinal (i.e. Survey or Data Forms
        Classic) the event number is not required and will be ignored if
        included

        Optional Inputs:
        ----------------

        session = the session var.

        If provided the driver will use the session var to cache form field
        names which improves save time performance
        '''

        er = external_record

        # make sure the proper information has been provided and that configure
        #  has been called
        if not er or not form_spec:
            return None
        if not (self.event_labels and self.unique_event_names
                and self.form_data) and not self.form_names:
            return None
        # make sure form_spec is of the correct format, N_M, negatives NOT
        # allowed for non longitudinal studies event number is not required,
        # is ok if supplied it will be ignored
        if self.form_names and not (re.match(r'^\d+$', form_spec)
                                    or re.match(r'^\d+_\d+$', form_spec)):
            return None

        split = form_spec.split('_')
        form_num = int(split[0])

        if not self.form_names:
            event_num = int(split[1])

        # Make sure the form and event indices are inbounds
        if self.form_names and form_num > (len(self.form_names) - 1):
            return None
        elif not self.form_names and (form_num >
                                      (len(self.form_data_ordered) - 1)
                                      or event_num >
                                      (len(self.unique_event_names) - 1)):
            return None

        form_name = self.form_data_ordered[form_num]
        # need to get the meta data from REDCAp to construct the form and the
        # record to populate previously entered values
        form_builder = FormBuilderJson()
        meta_data = self.raw_to_json(
            self.meta(_format=self.FORMAT_JSON, rawResponse=True))
        session = kwargs.get('session', None)

        if self.form_names:
            record_set = self.get(_format=self.FORMAT_JSON,
                                  records=[er.record_id],
                                  rawResponse=True,
                                  forms=[form_name]).read().strip()
            record_set = self.raw_to_json(record_set)
            return form_builder.construct_form(meta_data, record_set,
                                               form_name, er.record_id, None,
                                               None, None, session,
                                               self.record_id_field_name)
        else:
            temp = self.get(_format=self.FORMAT_JSON,
                            rawResponse=True,
                            records=[er.record_id])
            record_set = temp.read().strip()
            record_set = self.raw_to_json(record_set)
            return form_builder.construct_form(meta_data, record_set,
                                               form_name, er.record_id,
                                               event_num,
                                               self.unique_event_names,
                                               self.event_labels, session,
                                               self.record_id_field_name)
def form_builder():
    return FormBuilderJson()