Skip to content

Commit

Permalink
Merge pull request #32873 from dimagi/ze/determine_if_user_or_update_…
Browse files Browse the repository at this point in the history
…rule

add user type names for system case auto updates
  • Loading branch information
zandre-eng authored Apr 26, 2023
2 parents 17c77f7 + 9a6b024 commit f021e81
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
3 changes: 2 additions & 1 deletion corehq/apps/data_interfaces/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
from corehq.apps.casegroups.models import CommCareCaseGroup
from corehq.apps.domain.models import Domain
from corehq.apps.domain_migration_flags.api import any_migrations_in_progress
from corehq.apps.hqcase.utils import get_case_by_identifier
from corehq.form_processor.models import CommCareCase, XFormInstance
from corehq.motech.repeaters.const import RECORD_CANCELLED_STATE


def add_cases_to_case_group(domain, case_group_id, uploaded_data, progress_tracker):
from corehq.apps.hqcase.utils import get_case_by_identifier

response = {
'errors': [],
'success': [],
Expand Down
4 changes: 4 additions & 0 deletions corehq/apps/hqcase/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from corehq.apps.users.util import SYSTEM_USER_ID
from corehq.form_processor.exceptions import CaseNotFound, MissingFormXml
from corehq.form_processor.models import CommCareCase
from corehq.apps.data_interfaces.deduplication import DEDUPE_XMLNS
from corehq.motech.dhis2.const import XMLNS_DHIS2

CASEBLOCK_CHUNKSIZE = 100
SYSTEM_FORM_XMLNS = 'http://commcarehq.org/case'
Expand All @@ -25,6 +27,8 @@
SYSTEM_FORM_XMLNS: gettext_lazy('System Form'),
EDIT_FORM_XMLNS: gettext_lazy('Data Cleaning Form'),
AUTO_UPDATE_XMLNS: gettext_lazy('Automatic Case Update Rule'),
DEDUPE_XMLNS: gettext_lazy('Deduplication Rule'),
XMLNS_DHIS2: gettext_lazy('DHIS2 Integration')
}

ALLOWED_CASE_IDENTIFIER_TYPES = [
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/reports/standard/cases/case_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def form_to_json(domain, form, timezone):
"username": form.metadata.username if form.metadata else '',
},
'readable_name': form_name,
'user_type': get_user_type(form.metadata, domain) if form.metadata else 'Unknown',
'user_type': get_user_type(form, domain),
}


Expand Down
16 changes: 10 additions & 6 deletions corehq/apps/reports/standard/cases/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from corehq.apps.reports.filters.case_list import CaseListFilter as EMWF
from corehq.apps.reports.models import HQUserType
from corehq.apps.hqwebapp.doc_info import get_doc_info_by_id
from corehq.apps.hqcase.utils import SYSTEM_FORM_XMLNS_MAP


def _get_special_owner_ids(domain, admin, unknown, web, demo, commtrack):
Expand Down Expand Up @@ -179,13 +180,16 @@ def query_location_restricted_forms(query, domain, couch_user):
return query.filter(form_es.user_id(accessible_ids))


def get_user_type(form_metadata, domain=None):
def get_user_type(form, domain=None):
user_type = 'Unknown'
if getattr(form_metadata, 'userID', None):
doc_info = get_doc_info_by_id(domain, form_metadata.userID)
if doc_info:
if getattr(form.metadata, 'username', None) == 'system':
if form.xmlns in SYSTEM_FORM_XMLNS_MAP:
user_type = SYSTEM_FORM_XMLNS_MAP[form.xmlns]
else:
user_type = 'System'
elif getattr(form.metadata, 'userID', None):
doc_info = get_doc_info_by_id(domain, form.metadata.userID)
if doc_info.type_display:
user_type = doc_info.type_display
elif form_metadata.username == 'system':
user_type = 'System'

return user_type
24 changes: 18 additions & 6 deletions corehq/apps/reports/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from corehq.form_processor.utils import TestFormMetadata
from corehq.util.test_utils import TestFileMixin, get_form_ready_to_save
from corehq.apps.reports.standard.cases.utils import get_user_type
from corehq.apps.data_interfaces.deduplication import DEDUPE_XMLNS

DOMAIN = 'test_domain'
USER_ID = "5bc1315c-da6f-466d-a7c4-4580bc84a7b9"
Expand Down Expand Up @@ -44,22 +45,33 @@ def test_summarize_user_counts(self):

class TestGetUserType(SimpleTestCase):
def setUp(self):
self.form_metadata = Mock(userID=None, username='foobar')
self.form = Mock(
xmlns='my-xmlns',
metadata=Mock(
userID=None,
username='foobar'
)
)

def test_unknown_user(self):
self.assertEqual(get_user_type(self.form_metadata), 'Unknown')
self.assertEqual(get_user_type(self.form), 'Unknown')

def test_system_user(self):
self.form_metadata.username = 'system'
self.assertEqual(get_user_type(self.form_metadata), 'System')
self.form.metadata.username = 'system'
self.assertEqual(get_user_type(self.form), 'System')

def test_system_update(self):
self.form.metadata.username = 'system'
self.form.xmlns = DEDUPE_XMLNS
self.assertEqual(get_user_type(self.form), 'Deduplication Rule')

@patch(
'corehq.apps.reports.standard.cases.utils.get_doc_info_by_id',
return_value=Mock(type_display='Foobar')
)
def test_display_user(self, _):
self.form_metadata.userID = '1234'
self.assertEqual(get_user_type(self.form_metadata), 'Foobar')
self.form.metadata.userID = '1234'
self.assertEqual(get_user_type(self.form), 'Foobar')


def test_get_user_id():
Expand Down
4 changes: 1 addition & 3 deletions corehq/ex-submodules/casexml/apps/case/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ def get_case_history(case):
'Form Name': name,
'Form Received On': form.received_on,
'Form Submitted By': form.metadata.username,
'Form User Type': (
get_user_type(form.metadata, case.domain) if form.metadata else 'Unknown'
),
'Form User Type': get_user_type(form, case.domain),
}
case_blocks = extract_case_blocks(form)
for block in case_blocks:
Expand Down

0 comments on commit f021e81

Please sign in to comment.