-
-
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?
Conversation
mongo_uuid
field to XForm model and populate it
80734f0
to
47abf96
Compare
mongo_uuid
field to XForm model and populate itmongo_uuid
field to XForm model and populate it
…ment-mongo-owner-form-id
@@ -500,17 +501,79 @@ def test_data_accessible_to_new_user(self): | |||
for attachment in response.data['results'][0]['_attachments']: | |||
assert attachment['filename'].startswith('anotheruser/') | |||
|
|||
# Get the mongo_uuid for the transferred asset (XForm) | |||
xform = XForm.objects.get(kpi_asset_uid=self.asset.uid) |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
KeyError
? when can this error happen?
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.
Thanks for the review Olivier. The KeyError
can occur when the formid
key is missing from the backend_response
dictionary. This is typically the case in certain test scenarios, such as ConflictingVersionsMockDataExports
, where mock data doesn't include the formid
key.
When self.xform
is accessed, it attempts to retrieve the formid
from backend_response
, which raises a KeyError
if it's not present.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep methods sorted alphabetically and let's use KpiUidField.generate_unique_id()
which already generates a random string (don't forget to clean imports after).
c85a06d
to
98694f2
Compare
Checklist
./python-format.sh
to make sure that your code lints and that you've followed our coding styleDescription
This PR introduces a new field, mongo_uuid, to the XForm model in KoboToolbox. This unique identifier is designed to enhance the ownership transfer process of XForms by ensuring that each form is linked to a distinct UUID in MongoDB. The addition of mongo_uuid will streamline how XForms are managed, especially during ownership transfers, by reducing unnecessary updates to the MongoDB documents related to the XForm.
Notes
Ownership Transfer: The logic for transferring an XForm's ownership has been updated to ensure that the mongo_uuid is populated if it is currently null.