-
-
Notifications
You must be signed in to change notification settings - Fork 181
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
[TASK-957] Add mongo_uuid
field to XForm model and populate it
#5196
base: main
Are you sure you want to change the base?
Changes from 5 commits
47abf96
f41e936
9d95827
a0ceeeb
ad19fdc
98694f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 4.2.15 on 2024-10-24 20:16 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('logger', '0037_remove_xform_has_kpi_hooks_and_instance_posted_to_kpi'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='xform', | ||
name='mongo_uuid', | ||
field=models.CharField( | ||
db_index=True, max_length=100, null=True, unique=True | ||
), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
except ImportError: | ||
from backports.zoneinfo import ZoneInfo | ||
|
||
import secrets | ||
import string | ||
import redis.exceptions | ||
import requests | ||
from defusedxml import ElementTree as DET | ||
|
@@ -731,7 +733,13 @@ def get_validation_status( | |
|
||
@property | ||
def mongo_userform_id(self): | ||
return '{}_{}'.format(self.asset.owner.username, self.xform_id_string) | ||
try: | ||
return ( | ||
self.xform.mongo_uuid | ||
or f'{self.asset.owner.username}_{self.xform_id_string}' | ||
) | ||
except KeyError: | ||
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.
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. Thanks for the review Olivier. The When |
||
return f'{self.asset.owner.username}_{self.xform_id_string}' | ||
|
||
@staticmethod | ||
def nlp_tracking_data( | ||
|
@@ -1218,6 +1226,7 @@ def xform(self): | |
'attachment_storage_bytes', | ||
'require_auth', | ||
'uuid', | ||
'mongo_uuid', | ||
) | ||
.select_related('user') # Avoid extra query to validate username below | ||
.first() | ||
|
@@ -1234,6 +1243,20 @@ def xform(self): | |
self._xform = xform | ||
return self._xform | ||
|
||
def generate_unique_key(self, length=20): | ||
|
||
characters = string.ascii_letters + string.digits | ||
# Generate a secure key with the specified length | ||
return ''.join(secrets.choice(characters) for _ in range(length)) | ||
|
||
def set_mongo_uuid(self): | ||
""" | ||
Set the `mongo_uuid` for the associated XForm if it's not already set. | ||
""" | ||
if not self.xform.mongo_uuid: | ||
self.xform.mongo_uuid = self.generate_unique_key() | ||
self.xform.save(update_fields=['mongo_uuid']) | ||
|
||
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. Please keep methods sorted alphabetically and let's use |
||
@property | ||
def xform_id(self): | ||
return self.xform.pk | ||
|
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.
You can use the
XForm
object from the deployment, i.e.:self.asset.deployment.xform