-
-
Notifications
You must be signed in to change notification settings - Fork 217
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
allow submitting system forms with a name for display to users #31923
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
12f4a54
update docstring with latest best practice
snopoke 3204cc1
rename form_extras to submission_extras
snopoke 5e19c37
lint
snopoke 28dbf68
add form_name to submit_case_blocks
snopoke 7141f86
pass form_name from case update rules
snopoke 666e0e0
refactor _FormType
snopoke 8121700
include form name in non-app forms if supplied
snopoke 2273a91
tests for xmlns_to_name
snopoke 7601265
patch DB lookup
snopoke c16f7b7
coerce to string
snopoke 39b38f2
update calls to xmlns_to_name to include name from xml
snopoke db0a758
extract defaults out of loop
snopoke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ def replace(self, *args): | |
return StringWithAttributes(string) | ||
|
||
|
||
class FormDisplay(): | ||
class FormDisplay: | ||
|
||
def __init__(self, form_doc, report, lang=None): | ||
self.form = form_doc | ||
|
@@ -74,54 +74,72 @@ def readable_form_name(self): | |
self.report.domain, | ||
self.form.get("xmlns"), | ||
app_id=self.form.get("app_id"), | ||
lang=self.lang | ||
lang=self.lang, | ||
form_name=self.form.get("@name"), | ||
) | ||
|
||
|
||
class _FormType(object): | ||
|
||
def __init__(self, domain, xmlns, app_id): | ||
def __init__(self, domain, xmlns, app_id, form_name): | ||
self.domain = domain | ||
self.xmlns = xmlns | ||
self.app_id = app_id | ||
self.form_name = form_name | ||
|
||
def get_label(self, lang=None, separator=None): | ||
if separator is None: | ||
separator = " > " | ||
|
||
return ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?w=1 reads a lot easier for this commit |
||
self.get_label_from_app(lang, separator) | ||
or self.get_name_from_xml(separator) | ||
or self.append_form_name(self.xmlns, separator) | ||
) | ||
|
||
def get_label_from_app(self, lang, separator): | ||
form = get_form_analytics_metadata(self.domain, self.app_id, self.xmlns) | ||
if form and form.get('app'): | ||
langs = form['app']['langs'] | ||
app_name = form['app']['name'] | ||
app = form and form.get('app') | ||
if not app: | ||
return | ||
|
||
if form.get('is_user_registration'): | ||
form_name = "User Registration" | ||
title = separator.join([app_name, form_name]) | ||
else: | ||
def _menu_name(menu, lang): | ||
if lang and menu.get(lang): | ||
return menu.get(lang) | ||
else: | ||
for lang in langs + list(menu): | ||
menu_name = menu.get(lang) | ||
if menu_name is not None: | ||
return menu_name | ||
return "?" | ||
|
||
module_name = _menu_name(form["module"]["name"], lang) | ||
form_name = _menu_name(form["form"]["name"], lang) | ||
title = separator.join([app_name, module_name, form_name]) | ||
|
||
if form.get('app_deleted'): | ||
title += ' [Deleted]' | ||
if form.get('duplicate'): | ||
title += " [Multiple Forms]" | ||
name = title | ||
elif self.xmlns in SYSTEM_FORM_XMLNS_MAP: | ||
name = SYSTEM_FORM_XMLNS_MAP[self.xmlns] | ||
langs = form['app']['langs'] | ||
app_name = form['app']['name'] | ||
|
||
if form.get('is_user_registration'): | ||
form_name = "User Registration" | ||
title = separator.join([app_name, form_name]) | ||
else: | ||
name = self.xmlns | ||
def _menu_name(menu, lang): | ||
if lang and menu.get(lang): | ||
return menu.get(lang) | ||
else: | ||
for lang in langs + list(menu): | ||
menu_name = menu.get(lang) | ||
if menu_name is not None: | ||
return menu_name | ||
return "?" | ||
|
||
module_name = _menu_name(form["module"]["name"], lang) | ||
form_name = _menu_name(form["form"]["name"], lang) | ||
title = separator.join([app_name, module_name, form_name]) | ||
|
||
if form.get('app_deleted'): | ||
title += ' [Deleted]' | ||
if form.get('duplicate'): | ||
title += " [Multiple Forms]" | ||
return title | ||
|
||
def get_name_from_xml(self, separator): | ||
if self.xmlns in SYSTEM_FORM_XMLNS_MAP: | ||
readable_xmlns = str(SYSTEM_FORM_XMLNS_MAP[self.xmlns]) | ||
return self.append_form_name(readable_xmlns, separator) | ||
|
||
def append_form_name(self, name, separator): | ||
if self.form_name: | ||
name = separator.join([name, self.form_name]) | ||
return name | ||
|
||
|
||
def xmlns_to_name(domain, xmlns, app_id, lang=None, separator=None): | ||
return _FormType(domain, xmlns, app_id).get_label(lang, separator) | ||
def xmlns_to_name(domain, xmlns, app_id, lang=None, separator=None, form_name=None): | ||
return _FormType(domain, xmlns, app_id, form_name).get_label(lang, separator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import SimpleTestCase | ||
|
||
from corehq.apps.hqcase.utils import SYSTEM_FORM_XMLNS, SYSTEM_FORM_XMLNS_MAP | ||
|
||
from corehq.apps.reports.display import xmlns_to_name | ||
from corehq.util.test_utils import generate_cases | ||
|
||
UNKNOWN_XMLNS = "http://demo.co/form" | ||
KNOWN_XMLNS = SYSTEM_FORM_XMLNS | ||
KNOWN_XMLNS_DISPLAY = SYSTEM_FORM_XMLNS_MAP[KNOWN_XMLNS] | ||
|
||
|
||
@patch("corehq.apps.reports.display.get_form_analytics_metadata", new=lambda *a, **k: None) | ||
class TestXmlnsToName(SimpleTestCase): | ||
@generate_cases([ | ||
(UNKNOWN_XMLNS, UNKNOWN_XMLNS), | ||
(f"{UNKNOWN_XMLNS} > form name", UNKNOWN_XMLNS, "form name"), | ||
(f"{UNKNOWN_XMLNS} ] form name", UNKNOWN_XMLNS, "form name", " ] "), | ||
(KNOWN_XMLNS_DISPLAY, KNOWN_XMLNS), | ||
(f"{KNOWN_XMLNS_DISPLAY} > form name", KNOWN_XMLNS, "form name"), | ||
(f"{KNOWN_XMLNS_DISPLAY} ] form name", KNOWN_XMLNS, "form name", " ] "), | ||
]) | ||
def test_xmlns_to_name(self, expected, xmlns, form_name=None, separator=None): | ||
name = xmlns_to_name("domain", xmlns, "123", separator=separator, form_name=form_name) | ||
self.assertEqual(name, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯