Example #1
0
def get_form_data_schema(request, domain, form_unique_id):
    """Get data schema

    One of `app_id` or `form_unique_id` is required. `app_id` is ignored
    if `form_unique_id` is provided.

    :returns: A list of data source schema definitions. A data source schema
    definition is a dictionary. For details on the content of the dictionary,
    see https://github.com/dimagi/Vellum/blob/master/src/datasources.js
    """
    data = []

    try:
        form, app = Form.get_form(form_unique_id, and_app=True)
    except ResourceConflict:
        raise Http404()

    if app.domain != domain:
        raise Http404()

    try:
        data.append(get_session_schema(form))
        if form.requires_case() or is_usercase_in_use(domain):
            data.append(get_casedb_schema(form))
    except Exception:
        logger.exception("schema error")
        return HttpResponseBadRequest("schema error, see log for details")

    data.extend(
        sorted(item_lists_by_domain(domain), key=lambda x: x['name'].lower())
    )
    kw = {}
    if "pretty" in request.GET:
        kw["indent"] = 2
    return HttpResponse(json.dumps(data, **kw))
Example #2
0
 def test_get_session_schema_for_module_with_no_case_type(self):
     form = self.add_form()
     schema = util.get_session_schema(form)
     self.assert_has_kv_pairs(schema, {
         "id": "commcaresession",
         "uri": "jr://instance/session",
         "name": "Session",
         "path": "/session/data",
     })
     assert "case_id" not in schema["structure"], schema["structure"]
Example #3
0
 def test_get_session_schema_for_user_registration_form(self):
     app = self.make_app()
     schema = util.get_session_schema(app.user_registration)
     self.assert_has_kv_pairs(schema, {
         "id": "commcaresession",
         "uri": "jr://instance/session",
         "name": "Session",
         "path": "/session/data",
     })
     assert "case_id" not in schema["structure"], schema["structure"]
Example #4
0
 def test_get_session_schema_for_module_with_no_case_type(self):
     form = self.add_form()
     schema = util.get_session_schema(form)
     self.assert_has_kv_pairs(
         schema, {
             "id": "commcaresession",
             "uri": "jr://instance/session",
             "name": "Session",
             "path": "/session",
         })
     assert "data" not in schema["structure"], schema["structure"]
Example #5
0
 def test_get_session_schema_for_simple_module_with_case(self):
     app = self.make_app()
     form = self.add_form(app, "village")
     schema = util.get_session_schema(form)
     self.assertDictEqual(schema["structure"]["case_id"], {
         "reference": {
             "source": "casedb",
             "subset": "village",
             "key": "@case_id",
         },
     })
Example #6
0
 def test_get_session_schema_for_user_registration_form(self):
     app = self.make_app()
     schema = util.get_session_schema(app.user_registration)
     self.assert_has_kv_pairs(
         schema, {
             "id": "commcaresession",
             "uri": "jr://instance/session",
             "name": "Session",
             "path": "/session/data",
         })
     assert "case_id" not in schema["structure"], schema["structure"]
Example #7
0
 def test_get_session_schema_for_simple_module_with_case(self):
     module, form = self.factory.new_basic_module('village', 'village')
     self.factory.form_requires_case(form)
     schema = util.get_session_schema(form)
     self.assertDictEqual(schema["structure"]["case_id"], {
         "reference": {
             "source": "casedb",
             "subset": "case",
             "key": "@case_id",
         },
     })
Example #8
0
 def test_get_session_schema_for_simple_module_with_case(self):
     module, form = self.factory.new_basic_module('village', 'village')
     self.factory.form_requires_case(form)
     schema = util.get_session_schema(form)
     self.assertDictEqual(
         schema["structure"]["case_id"], {
             "reference": {
                 "source": "casedb",
                 "subset": "case",
                 "key": "@case_id",
             },
         })
Example #9
0
 def test_get_session_schema_for_simple_module_with_case(self):
     app = self.make_app()
     form = self.add_form(app, "village")
     schema = util.get_session_schema(form)
     self.assertDictEqual(
         schema["structure"]["case_id"], {
             "reference": {
                 "source": "casedb",
                 "subset": "village",
                 "key": "@case_id",
             },
         })
Example #10
0
    def test_get_session_schema_for_child_module(self):
        # m0 - opens 'gold-fish' case.
        # m1 - has m0 as root-module, has parent-select, updates 'guppy' case
        self.module_0, _ = self.factory.new_basic_module('parent', 'gold-fish')
        self.module_1, _ = self.factory.new_basic_module('child', 'guppy', parent_module=self.module_0)
        # m0f0 registers gold-fish case and a child case ('guppy')
        m0f0 = self.module_0.get_form(0)
        self.factory.form_requires_case(m0f0, update={'name': 'goldilocks'})
        self.factory.form_opens_case(m0f0, 'guppy', is_subcase=True)

        # m1f0 has parent-select, updates `guppy` case
        m1f0 = self.module_1.get_form(0)
        self.factory.form_requires_case(m1f0, parent_case_type='gold-fish')

        casedb_schema = util.get_casedb_schema(m1f0)
        session_schema = util.get_session_schema(m1f0)

        expected_session_schema_structure = {
            "case_id_guppy": {
                "reference": {
                    "subset": "case",
                    "source": "casedb",
                    "key": "@case_id"
                }
            }
        }

        expected_casedb_schema_subsets = [
            {
                "structure": {
                    "case_name": {}
                },
                "related": {
                    "parent": "parent"
                },
                "id": "case",
                "key": "@case_type",
                "name": "guppy"
            },
            {
                "structure": {
                    "name": {},
                    "case_name": {}
                },
                "related": None,
                "id": "parent",
                "key": "@case_type",
                "name": "parent (gold-fish)"
            }
        ]

        self.assertEqual(casedb_schema['subsets'], expected_casedb_schema_subsets)
        self.assertEqual(session_schema['structure'], expected_session_schema_structure)
Example #11
0
    def test_get_session_schema_for_child_module(self):
        # m0 - opens 'gold-fish' case.
        # m1 - has m0 as root-module, has parent-select, updates 'guppy' case
        self.module_0, _ = self.factory.new_basic_module('parent', 'gold-fish')
        self.module_1, _ = self.factory.new_basic_module(
            'child', 'guppy', parent_module=self.module_0)
        # m0f0 registers gold-fish case and a child case ('guppy')
        m0f0 = self.module_0.get_form(0)
        self.factory.form_requires_case(m0f0, update={'name': 'goldilocks'})
        self.factory.form_opens_case(m0f0, 'guppy', is_subcase=True)

        # m1f0 has parent-select, updates `guppy` case
        m1f0 = self.module_1.get_form(0)
        self.factory.form_requires_case(m1f0, parent_case_type='gold-fish')

        casedb_schema = util.get_casedb_schema(m1f0)
        session_schema = util.get_session_schema(m1f0)

        expected_session_schema_structure = {
            "case_id_guppy": {
                "reference": {
                    "subset": "case",
                    "source": "casedb",
                    "key": "@case_id"
                }
            }
        }

        expected_casedb_schema_subsets = [{
            "structure": {
                "case_name": {}
            },
            "related": {
                "parent": "parent"
            },
            "id": "case",
            "key": "@case_type",
            "name": "guppy"
        }, {
            "structure": {
                "name": {},
                "case_name": {}
            },
            "related": None,
            "id": "parent",
            "key": "@case_type",
            "name": "parent (gold-fish)"
        }]

        self.assertEqual(casedb_schema['subsets'],
                         expected_casedb_schema_subsets)
        self.assertEqual(session_schema['structure'],
                         expected_session_schema_structure)
Example #12
0
 def test_get_session_schema_with_user_case(self):
     module, form = self.factory.new_basic_module('village', 'village')
     with patch('corehq.apps.app_manager.util.is_usercase_in_use') as mock:
         mock.return_value = True
         schema = util.get_session_schema(form)
         self.assertDictEqual(
             schema["structure"]["context"], {
                 "merge": True,
                 "structure": {
                     "userid": {
                         "reference": {
                             "hashtag": "#user",
                             "source": "casedb",
                             "subset": util.USERCASE_TYPE,
                             "subset_key": "@case_type",
                             "subset_filter": True,
                             "key": "hq_user_id",
                         },
                     },
                 },
             })
Example #13
0
 def test_get_session_schema(self):
     module, form = self.factory.new_basic_module('village', 'village')
     schema = util.get_session_schema(form)
     self.assertNotIn("context", schema["structure"], repr(schema))
Example #14
0
def get_data_schema(request, domain, app_id=None, form_unique_id=None):
    """Get data schema

    One of `app_id` or `form_unique_id` is required. `app_id` is ignored
    if `form_unique_id` is provided.

    :returns: A list of data source schema definitions. A data source schema
    definition is a dictionary with the following format:
    ```
    {
        "id": string (default instance id)
        "uri": string (instance src)
        "path": string (path of root nodeset, not including `instance(...)`)
        "name": string (human readable name)
        "structure": {
            element: {
                "name": string (optional human readable name)
                "structure": {
                    nested-element: { ... }
                },
            },
            ref-element: {
                "reference": {
                    "source": string (optional data source id, defaults to this data source)
                    "subset": string (optional subset id)
                    "key": string (referenced property)
                }
            },
            @attribute: { },
            ...
        },
        "subsets": [
            {
                "id": string (unique identifier for this subset)
                "key": string (unique identifier property name)
                "name": string (optional human readable name)
                "structure": { ... }
                "related": {
                    string (relationship): string (related subset name),
                    ...
                }
            },
            ...
        ]
    }
    ```
    A structure may contain nested structure elements. A nested element
    may contain one of "structure" (a concrete structure definition) or
    "reference" (a link to some other structure definition). Any
    structure item may have a human readable "name".
    """
    data = []
    if form_unique_id is None:
        app = get_app(domain, app_id)
        form = None
    else:
        try:
            form, app = Form.get_form(form_unique_id, and_app=True)
        except ResourceConflict:
            raise Http404()
        data.append(get_session_schema(form))
    if app.domain != domain:
        raise Http404()
    data.append(get_casedb_schema(app))  # TODO use domain instead of app
    data.extend(
        sorted(item_lists_by_domain(domain), key=lambda x: x['name'].lower())
    )
    kw = {}
    if "pretty" in request.GET:
        kw["indent"] = 2
    return HttpResponse(json.dumps(data, **kw))
Example #15
0
def get_form_data_schema(request, domain, form_unique_id):
    """Get data schema

    One of `app_id` or `form_unique_id` is required. `app_id` is ignored
    if `form_unique_id` is provided.

    :returns: A list of data source schema definitions. A data source schema
    definition is a dictionary with the following format:
    ```
    {
        "id": string (default instance id)
        "uri": string (instance src)
        "path": string (path of root nodeset, not including `instance(...)`)
        "name": string (human readable name)
        "structure": {
            element: {
                "name": string (optional human readable name)
                "structure": {
                    nested-element: { ... }
                },
            },
            ref-element: {
                "reference": {
                    "source": string (optional data source id, defaults to this data source)
                    "subset": string (optional subset id)
                    "key": string (referenced property)
                }
            },
            @attribute: { },
            ...
        },
        "subsets": [
            {
                "id": string (unique identifier for this subset)
                "key": string (unique identifier property name)
                "name": string (optional human readable name)
                "structure": { ... }
                "related": {
                    string (relationship): string (related subset name),
                    ...
                }
            },
            ...
        ]
    }
    ```
    A structure may contain nested structure elements. A nested element
    may contain one of "structure" (a concrete structure definition) or
    "reference" (a link to some other structure definition). Any
    structure item may have a human readable "name".
    """
    data = []

    try:
        form, app = Form.get_form(form_unique_id, and_app=True)
    except ResourceConflict:
        raise Http404()

    if app.domain != domain:
        raise Http404()

    try:
        data.append(get_session_schema(form))
        if form and form.requires_case():
            data.append(get_casedb_schema(form))
    except Exception as e:
        return HttpResponseBadRequest(e)

    data.extend(
        sorted(item_lists_by_domain(domain), key=lambda x: x['name'].lower()))
    kw = {}
    if "pretty" in request.GET:
        kw["indent"] = 2
    return HttpResponse(json.dumps(data, **kw))