Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying a custom camera app for image questions #659

Merged
merged 19 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,13 @@ def workbook_to_json(

if row.get("default"):
new_dict["default"] = process_image_default(row["default"])
parameters_generic.validate(parameters=parameters, allowed=("max-pixels",))
parameters_generic.validate(
parameters=parameters,
allowed=(
"max-pixels",
"app",
),
)
if "max-pixels" in parameters.keys():
try:
int(parameters["max-pixels"])
Expand All @@ -1324,6 +1330,20 @@ def workbook_to_json(
(ROW_FORMAT_STRING % row_number)
+ " Use the max-pixels parameter to speed up submission sending and save storage space. Learn more: https://xlsform.org/#image"
)

if "app" in parameters.keys():
appearance = row.get("control", {}).get("appearance")
if appearance is None or appearance == "annotate":
grzesiek2010 marked this conversation as resolved.
Show resolved Hide resolved
android_package_regex_pattern = (
"^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]$"
)
app_package_name = str(parameters["app"])
if re.fullmatch(android_package_regex_pattern, app_package_name):
new_dict["control"] = new_dict.get("control", {})
new_dict["control"].update({"intent": app_package_name})
else:
raise PyXFormError("Invalid Android package name format.")

parent_children_array.append(new_dict)
continue

Expand Down
84 changes: 84 additions & 0 deletions tests/test_image_app_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
"""
Test image app parameter.
"""
from tests.pyxform_test_case import PyxformTestCase


class ImageAppParameterTest(PyxformTestCase):
grzesiek2010 marked this conversation as resolved.
Show resolved Hide resolved
def test_valid_android_package_name(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | |
| | type | name | label | parameters |
| | image | my_image | Image | app=com.jeyluta.timestampcamerafree |
""",
xml__xpath_match=[
"/h:html/h:body/x:upload[@intent='com.jeyluta.timestampcamerafree' and @mediatype='image/*' and @ref='/data/my_image']"
],
)

def test_invalid_android_package_name(self):
self.assertPyxformXform(
name="data",
errored=True,
md="""
| survey | | | | |
| | type | name | label | parameters |
| | image | my_image | Image | app=something |
""",
error__contains=["Invalid Android package name format"],
)

def test_adding_android_package_name_in_annotate_image(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | parameters | appearance |
| | image | my_image | Image | app=com.jeyluta.timestampcamerafree | annotate |
""",
xml__xpath_match=[
"/h:html/h:body/x:upload[@intent='com.jeyluta.timestampcamerafree' and @mediatype='image/*' and @ref='/data/my_image']"
],
)

def test_ignoring_android_package_name_in_signature_image(self):
grzesiek2010 marked this conversation as resolved.
Show resolved Hide resolved
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | parameters | appearance |
| | image | my_image | Image | app=com.jeyluta.timestampcamerafree | signature |
""",
xml__xpath_match=[
"/h:html/h:body/x:upload[@mediatype='image/*' and @ref='/data/my_image']"
],
)

def test_ignoring_android_package_name_in_draw_image(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | parameters | appearance |
| | image | my_image | Image | app=com.jeyluta.timestampcamerafree | draw |
""",
xml__xpath_match=[
"/h:html/h:body/x:upload[@mediatype='image/*' and @ref='/data/my_image']"
],
)

def test_ignoring_android_package_name_in_selfie_image(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | parameters | appearance |
| | image | my_image | Image | app=com.jeyluta.timestampcamerafree | new-front |
""",
xml__xpath_match=[
"/h:html/h:body/x:upload[@mediatype='image/*' and @ref='/data/my_image']"
],
)
2 changes: 1 addition & 1 deletion tests/test_max_pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_string_extra_params(self):
| | image | my_image | Image | max-pixels=640 foo=bar |
""",
error__contains=[
"Accepted parameters are 'max-pixels'. The following are invalid parameter(s): 'foo'."
"Accepted parameters are 'app, max-pixels'. The following are invalid parameter(s): 'foo'."
],
)

Expand Down
Loading