Skip to content

Commit

Permalink
Merge pull request #35055 from dimagi/sk/transaction-device-id
Browse files Browse the repository at this point in the history
add device_id to case transactions for easy access
  • Loading branch information
snopoke authored Aug 29, 2024
2 parents 6a78832 + bcd541c commit fc7f1ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion corehq/form_processor/models/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,10 @@ def is_case_rebuild(self):
def xmlns(self):
return self.details.get('xmlns', None) if self.details else None

@property
def device_id(self):
return self.details.get('device_id', None) if self.details else None

@classmethod
@memoized
def case_rebuild_types(cls):
Expand Down Expand Up @@ -1476,7 +1480,7 @@ def _from_form(cls, case, xform, transaction_type):
server_date=xform.received_on,
type=transaction_type,
revoked=not xform.is_normal,
details=FormSubmissionDetail(xmlns=xform.xmlns).to_json()
details=FormSubmissionDetail(xmlns=xform.xmlns, device_id=xform.device_id).to_json()
)

@classmethod
Expand Down Expand Up @@ -1544,6 +1548,7 @@ def __eq__(self, other):
class FormSubmissionDetail(CaseTransactionDetail):
_type = CaseTransaction.TYPE_FORM
xmlns = StringProperty()
device_id = StringProperty()


class RebuildWithReason(CaseTransactionDetail):
Expand Down
8 changes: 8 additions & 0 deletions corehq/form_processor/models/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ def history(self):
return operations

@property
@memoized
def metadata(self):
from ..utils import clean_metadata
if const.TAG_META in self.form_data:
Expand All @@ -650,6 +651,13 @@ def type(self):
def name(self):
return self.form_data.get(const.TAG_NAME, "")

@property
def device_id(self):
try:
return self.metadata and self.metadata.deviceID
except MissingFormXml:
pass

@memoized
def get_sync_token(self):
from casexml.apps.phone.exceptions import MissingSyncLog
Expand Down
5 changes: 4 additions & 1 deletion corehq/form_processor/tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ def test_create_case(self):
case_id = uuid.uuid4().hex
modified_on = datetime.utcnow()
xmlns = 'http://commcare.org/test_xmlns'
device_id = "a.b.c.DemoDevice"
_submit_case_block(
True, case_id, user_id='user1', owner_id='owner1', case_type='demo',
case_name='create_case', date_modified=modified_on, date_opened=modified_on, update={
'dynamic': '123'
},
xmlns=xmlns
xmlns=xmlns,
device_id=device_id
)

case = self.casedb.get_case(case_id, DOMAIN)
Expand All @@ -149,6 +151,7 @@ def test_create_case(self):
transactions = case.get_form_transactions()
self.assertEqual(1, len(transactions))
self.assertEqual(transactions[0].xmlns, xmlns)
self.assertEqual(transactions[0].device_id, device_id)

def test_create_case_unicode_name(self):
"""
Expand Down

0 comments on commit fc7f1ec

Please sign in to comment.