Exemplo n.º 1
0
    def dehydrate_module(self, app, module, langs):
        """
        Convert a Module object to a JValue representation
        with just the good parts.

        NOTE: This is not a tastypie "magic"-name method to
        dehydrate the "module" field; there is no such field.
        """
        dehydrated = {}

        dehydrated['case_type'] = module.case_type

        dehydrated['case_properties'] = app_manager_util.get_case_properties(app, [module.case_type], defaults=['name'])[module.case_type]

        dehydrated['forms'] = []
        for form in module.forms:
            form = Form.get_form(form.unique_id)
            form_jvalue = {
                'xmlns': form.xmlns,
                'name': form.name,
                'questions': form.get_questions(langs),
            }
            dehydrated['forms'].append(form_jvalue)

        return dehydrated
Exemplo n.º 2
0
    def dehydrate_module(self, app, module, langs):
        """
        Convert a Module object to a JValue representation
        with just the good parts.

        NOTE: This is not a tastypie "magic"-name method to
        dehydrate the "module" field; there is no such field.
        """
        try:
            dehydrated = {}

            dehydrated['case_type'] = module.case_type

            dehydrated['case_properties'] = app_manager_util.get_case_properties(
                app, [module.case_type], defaults=['name']
            )[module.case_type]

            dehydrated['unique_id'] = module.unique_id

            dehydrated['forms'] = []
            for form in module.forms:
                form_unique_id = form.unique_id
                form_jvalue = {
                    'xmlns': form.xmlns,
                    'name': form.name,
                    'questions': form.get_questions(langs, include_translations=True),
                    'unique_id': form_unique_id,
                }
                dehydrated['forms'].append(form_jvalue)
            return dehydrated
        except Exception as e:
            return {
                'error': unicode(e)
            }
Exemplo n.º 3
0
    def dehydrate_module(self, app, module, langs):
        """
        Convert a Module object to a JValue representation
        with just the good parts.

        NOTE: This is not a tastypie "magic"-name method to
        dehydrate the "module" field; there is no such field.
        """
        try:
            dehydrated = {}

            dehydrated["case_type"] = module.case_type

            dehydrated["case_properties"] = app_manager_util.get_case_properties(
                app, [module.case_type], defaults=["name"]
            )[module.case_type]

            dehydrated["unique_id"] = module.unique_id

            dehydrated["forms"] = []
            for form in module.forms:
                form_unique_id = form.unique_id
                form = Form.get_form(form_unique_id)
                form_jvalue = {
                    "xmlns": form.xmlns,
                    "name": form.name,
                    "questions": form.get_questions(langs, include_translations=True),
                    "unique_id": form_unique_id,
                }
                dehydrated["forms"].append(form_jvalue)
            return dehydrated
        except Exception as e:
            return {"error": unicode(e)}
Exemplo n.º 4
0
    def dehydrate_module(self, app, module, langs):
        """
        Convert a Module object to a JValue representation
        with just the good parts.

        NOTE: This is not a tastypie "magic"-name method to
        dehydrate the "module" field; there is no such field.
        """
        dehydrated = {}

        dehydrated['case_type'] = module.case_type

        dehydrated['case_properties'] = app_manager_util.get_case_properties(
            app, [module.case_type], defaults=['name'])[module.case_type]

        dehydrated['forms'] = []
        for form in module.forms:
            form = Form.get_form(form.unique_id)
            form_jvalue = {
                'xmlns': form.xmlns,
                'name': form.name,
                'questions': form.get_questions(langs),
            }
            dehydrated['forms'].append(form_jvalue)

        return dehydrated
Exemplo n.º 5
0
    def dehydrate_module(self, app, module, langs):
        """
        Convert a Module object to a JValue representation
        with just the good parts.

        NOTE: This is not a tastypie "magic"-name method to
        dehydrate the "module" field; there is no such field.
        """
        try:
            dehydrated = {}

            dehydrated['case_type'] = module.case_type

            dehydrated['case_properties'] = app_manager_util.get_case_properties(
                app, [module.case_type], defaults=['name']
            )[module.case_type]

            dehydrated['unique_id'] = module.unique_id

            dehydrated['forms'] = []
            for form in module.forms:
                form_unique_id = form.unique_id
                form_jvalue = {
                    'xmlns': form.xmlns,
                    'name': form.name,
                    'questions': form.get_questions(langs, include_translations=True),
                    'unique_id': form_unique_id,
                }
                dehydrated['forms'].append(form_jvalue)
            return dehydrated
        except Exception as e:
            return {
                'error': unicode(e)
            }
Exemplo n.º 6
0
    def generate_schema_from_builds(domain, case_type):
        """Builds a schema from Application builds for a given identifier

        :param domain: The domain that the export belongs to
        :param unique_form_id: The unique identifier of the item being exported
        :returns: Returns a ExportDataSchema instance
        """
        app_build_ids = get_all_app_ids(domain)
        all_case_schema = CaseExportDataSchema()

        for app_doc in iter_docs(Application.get_db(), app_build_ids):
            app = Application.wrap(app_doc)
            case_property_mapping = get_case_properties(
                app,
                [case_type],
                include_parent_properties=False
            )
            case_schema = CaseExportDataSchema._generate_schema_from_case_property_mapping(
                case_property_mapping,
                app.version,
            )
            case_history_schema = CaseExportDataSchema._generate_schema_for_case_history(
                case_property_mapping,
                app.version,
            )

            all_case_schema = CaseExportDataSchema._merge_schemas(
                all_case_schema,
                case_schema,
                case_history_schema
            )

        return all_case_schema
Exemplo n.º 7
0
    def case_properties(self):
        props = set([])

        for app in self.applications:
            prop_map = get_case_properties(app, [self.case_type], defaults=("name",))
            props |= set(prop_map[self.case_type])

        return props
Exemplo n.º 8
0
    def case_properties(self):
        props = set([])

        for app in self.applications:
            prop_map = get_case_properties(app, [self.case_type],
                                           defaults=("name", ))
            props |= set(prop_map[self.case_type])

        return props
Exemplo n.º 9
0
    def generate_schema_from_builds(domain, case_type, force_rebuild=False):
        """Builds a schema from Application builds for a given identifier

        :param domain: The domain that the export belongs to
        :param unique_form_id: The unique identifier of the item being exported
        :returns: Returns a CaseExportDataSchema instance
        """

        original_id, original_rev = None, None
        current_case_schema = get_latest_case_export_schema(domain, case_type)

        if current_case_schema and not force_rebuild:
            # Save the original id an rev so we can later save the document under the same _id
            original_id, original_rev = current_case_schema._id, current_case_schema._rev
        else:
            current_case_schema = CaseExportDataSchema()

        app_build_ids = CaseExportDataSchema._get_app_build_ids_to_process(
            domain,
            current_case_schema.last_app_versions,
        )

        for app_doc in iter_docs(Application.get_db(), app_build_ids):
            app = Application.wrap(app_doc)
            case_property_mapping = get_case_properties(
                app,
                [case_type],
                include_parent_properties=False
            )
            case_schema = CaseExportDataSchema._generate_schema_from_case_property_mapping(
                case_property_mapping,
                app.copy_of,
                app.version,
            )
            case_history_schema = CaseExportDataSchema._generate_schema_for_case_history(
                case_property_mapping,
                app.copy_of,
                app.version,
            )

            current_case_schema = CaseExportDataSchema._merge_schemas(
                current_case_schema,
                case_schema,
                case_history_schema
            )

            current_case_schema.record_update(app.copy_of, app.version)

        if original_id and original_rev:
            current_case_schema._id = original_id
            current_case_schema._rev = original_rev
        current_case_schema.domain = domain
        current_case_schema.case_type = case_type
        current_case_schema.save()

        return current_case_schema
Exemplo n.º 10
0
def get_case_data_source(app, case_type):

    prop_map = get_case_properties(app, [case_type], defaults=DEFAULT_CASE_PROPERTY_DATATYPES.keys())
    return DataSourceConfiguration(
        domain=app.domain,
        referenced_doc_type="CommCareCase",
        table_id=_clean_table_name(app.domain, case_type),
        display_name=case_type,
        configured_filter=make_case_data_source_filter(case_type),
        configured_indicators=[make_case_property_indicator(property) for property in prop_map[case_type]],
    )
Exemplo n.º 11
0
 def search_case_property_response(self):
     """
     Returns a dict of {case type: [case properties...]}
     """
     result = {}
     for app in self.apps:
         case_types = list(set([m.case_type for m in app.modules]))
         for case_type in case_types:
             if case_type not in result:
                 result[case_type] = ['name']
             for properties in get_case_properties(app, [case_type]).values():
                 result[case_type].extend(properties)
     return self.clean_dict_list(result)
Exemplo n.º 12
0
 def search_case_property_response(self):
     """
     Returns a dict of {case type: [case properties...]}
     """
     result = {}
     for app in self.apps:
         case_types = list(set([m.case_type for m in app.modules]))
         for case_type in case_types:
             if case_type not in result:
                 result[case_type] = ['name']
             for properties in get_case_properties(app,
                                                   [case_type]).values():
                 result[case_type].extend(properties)
     return self.clean_dict_list(result)
Exemplo n.º 13
0
def get_case_data_source(app, case_type):

    prop_map = get_case_properties(
        app, [case_type], defaults=list(DEFAULT_CASE_PROPERTY_DATATYPES))
    return DataSourceConfiguration(
        domain=app.domain,
        referenced_doc_type='CommCareCase',
        table_id=_clean_table_name(app.domain, case_type),
        display_name=case_type,
        configured_filter=make_case_data_source_filter(case_type),
        configured_indicators=[
            make_case_property_indicator(property)
            for property in prop_map[case_type]
        ])
Exemplo n.º 14
0
    def __init__(self, domain, app, source_type, source_id):
        assert (source_type in ['case', 'form'])

        self.domain = domain
        self.app = app
        self.source_type = source_type
        # source_id is a case type of form id
        self.source_id = source_id
        if self.source_type == 'form':
            self.source_form = Form.get_form(self.source_id)
            self.source_xform = XForm(self.source_form.source)
        if self.source_type == 'case':
            prop_map = get_case_properties(
                self.app, [self.source_id], defaults=DEFAULT_CASE_PROPERTY_DATATYPES.keys()
            )
            self.case_properties = sorted(set(prop_map[self.source_id]) | {'closed'})
Exemplo n.º 15
0
    def __init__(self, domain, app, source_type, source_id):
        assert (source_type in ['case', 'form'])

        self.domain = domain
        self.app = app
        self.source_type = source_type
        # source_id is a case type of form id
        self.source_id = source_id
        if self.source_type == 'form':
            self.source_form = Form.get_form(self.source_id)
            self.source_xform = XForm(self.source_form.source)
        if self.source_type == 'case':
            prop_map = get_case_properties(
                self.app, [self.source_id],
                defaults=DEFAULT_CASE_PROPERTY_DATATYPES.keys())
            self.case_properties = sorted(
                set(prop_map[self.source_id]) | {'closed'})
Exemplo n.º 16
0
 def assertCaseProperties(self, app, case_type, expected_properties):
     properties = util.get_case_properties(app, [case_type])
     self.assertEqual(
         set(properties[case_type]),
         set(expected_properties),
     )
Exemplo n.º 17
0
 def assertCaseProperties(self, app, case_type, expected_properties):
     properties = util.get_case_properties(app, [case_type])
     self.assertEqual(
         set(properties[case_type]),
         set(expected_properties),
     )