Beispiel #1
0
def form_response_list(formId):
    """Show all responses for a particular form.
    Example
    /responses?query=test
    /responses?query=test&autocomplete=1
    /responses?query=5cdf&autocomplete=1&search_by_id=1
    """
    from ..main import app

    form = Form.objects.only("formOptions",
                             "cff_permissions").get({"_id": ObjectId(formId)})
    if (form.formOptions and form.formOptions.dataOptions
            and "views" in form.formOptions.dataOptions):
        form.formOptions.dataOptions["views"] = replaceKey(
            replaceKey(form.formOptions.dataOptions["views"], "||", "."), "|",
            "$")

    query_params = app.current_request.query_params or {}

    query = query_params.get("query", None)
    dataOptionView = query_params.get("dataOptionView", None)
    if dataOptionView:
        app.check_permissions(form, ["Responses_View"])
        return _dataOptionView(form, dataOptionView)
    elif query:
        app.check_permissions(form, ["Responses_View", "Responses_CheckIn"])
        autocomplete = query_params.get("autocomplete", None)
        search_by_id = query_params.get("search_by_id", None)
        show_unpaid = query_params.get("show_unpaid", None)
        return _search(form, query, autocomplete, search_by_id, show_unpaid)
    else:
        app.check_permissions(form, ["Responses_View"])
        return _all(form)
Beispiel #2
0
 def test_replace_simple(self):
     self.assertEqual(replaceKey({"$ref": "a"}, "$", "|"), {"|ref": "a"})
     self.assertEqual(replaceKey({"items": [{
         "$ref": "a"
     }]}, "$", "|"), {"items": [{
         "|ref": "a"
     }]})
     self.assertEqual(replaceKey({"a.b.c": "a"}, ".", "||"),
                      {"a||b||c": "a"})
Beispiel #3
0
def form_response_list(formId):
    """Show all responses for a particular form.
    Example
    /responses?query=test
    /responses?query=test&autocomplete=1
    /responses?query=5cdf&autocomplete=1&search_by_id=1
    """
    from ..main import app

    form = Form.objects.only("formOptions",
                             "cff_permissions").get({"_id": ObjectId(formId)})
    if (form.formOptions and form.formOptions.dataOptions
            and "views" in form.formOptions.dataOptions):
        form.formOptions.dataOptions["views"] = replaceKey(
            replaceKey(form.formOptions.dataOptions["views"], "||", "."), "|",
            "$")

    query_params = app.current_request.query_params or {}

    query = query_params.get("query", None)
    dataOptionViewId = query_params.get("dataOptionView", None)
    apiKey = query_params.get("apiKey", None)
    skip_perm_check = False
    if (form.formOptions.responseListApiKey and apiKey
            and form.formOptions.responseListApiKey == hashlib.sha512(
                apiKey.encode()).hexdigest()):
        skip_perm_check = True
    if dataOptionViewId:
        dataOptionViewList = filter(lambda x: x["id"] == dataOptionViewId,
                                    form.formOptions.dataOptions["views"])
        try:
            dataOptionView = next(dataOptionViewList)
        except StopIteration:
            raise Exception(
                f"dataOptionView with id {dataOptionViewId} not found.")
        if ("apiKey" in dataOptionView and apiKey and dataOptionView["apiKey"]
                == hashlib.sha512(apiKey.encode()).hexdigest()):
            skip_perm_check = True
        if not skip_perm_check:
            app.check_permissions(form, ["Responses_View"])
        return _dataOptionView(form, dataOptionView)
    elif query:
        if not skip_perm_check:
            app.check_permissions(form,
                                  ["Responses_View", "Responses_CheckIn"])
        autocomplete = query_params.get("autocomplete", None)
        search_by_id = query_params.get("search_by_id", None)
        show_unpaid = query_params.get("show_unpaid", None)
        return _search(form, query, autocomplete, search_by_id, show_unpaid)
    else:
        if not skip_perm_check:
            app.check_permissions(form, ["Responses_View"])
        return _all(form)
Beispiel #4
0
 def test_replace_dataOptionView(self):
     orig = [{
         "id":
         "aggregateView",
         "type":
         "stats",
         "stats": [{
             "type":
             "single",
             "title":
             "a",
             "queryType":
             "aggregate",
             "queryValue": [
                 {
                     "value.registrationType": "sponsorship"
                 },
                 {
                     "$group": {
                         "_id": None,
                         "n": {
                             "$sum": 1
                         }
                     }
                 },
             ],
         }],
     }]
     new = [{
         "id":
         "aggregateView",
         "type":
         "stats",
         "stats": [{
             "type":
             "single",
             "title":
             "a",
             "queryType":
             "aggregate",
             "queryValue": [
                 {
                     "value.registrationType": "sponsorship"
                 },
                 {
                     "|group": {
                         "_id": None,
                         "n": {
                             "|sum": 1
                         }
                     }
                 },
             ],
         }],
     }]
     # self.assertEqual(replaceKey(orig[0]["stats"][0]["queryValue"], "$", "|"), new[0]["stats"][0]["queryValue"])
     self.assertEqual(replaceKey(orig, "$", "|"), new)
Beispiel #5
0
def form_render(formId):
    """Render single form."""
    form = None
    try:
        form = Form.objects.only("name", "schema", "uiSchema", "formOptions",
                                 "cff_permissions").get(
                                     {"_id": ObjectId(formId)})
        # Convert __$ref back to $ref.
        if form.schema:
            form.schema = renameKey(form.schema, "__$ref", "$ref")
        if (form.formOptions and form.formOptions.dataOptions
                and "views" in form.formOptions.dataOptions):
            form.formOptions.dataOptions["views"] = replaceKey(
                replaceKey(form.formOptions.dataOptions["views"], "||", "."),
                "|", "$")
    except DoesNotExist:
        raise NotFoundError(f"Form ID not found: {formId}")
    return {"res": serialize_model(form)}
Beispiel #6
0
def form_edit(formId):
    from ..main import app

    form = Form.objects.get({"_id": ObjectId(formId)})
    app.check_permissions(form, "Forms_Edit")
    body = pick(
        app.current_request.json_body,
        ["schema", "uiSchema", "formOptions", "name", "tags"],
    )
    for k, v in body.items():
        setattr(form, k, v)
    # Rename $ref properly.
    if form.schema:
        form.schema = renameKey(form.schema, "$ref", "__$ref")
    if (form.formOptions and form.formOptions.dataOptions
            and "views" in form.formOptions.dataOptions):
        form.formOptions.dataOptions["views"] = replaceKey(
            replaceKey(form.formOptions.dataOptions["views"], "$", "|"), ".",
            "||")
    form.save()
    form = Form.objects.get({"_id": ObjectId(formId)})
    return {"res": {"success": True, "updated_values": serialize_model(form)}}