def _fix_replacement_form_problem_in_couch(doc): """Fix replacement form created by swap_duplicate_xforms The replacement form was incorrectly created with "problem" text, which causes it to be counted as an error form, and that messes up the diff counts at the end of this migration. NOTE the replacement form's _id does not match instanceID in its form.xml. That issue is not resolved here. See: - corehq/apps/cleanup/management/commands/swap_duplicate_xforms.py - couchforms/_design/views/all_submissions_by_domain/map.js """ problem = doc["problem"] assert problem.startswith(PROBLEM_TEMPLATE_START), doc assert doc["doc_type"] == "XFormInstance", doc deprecated_id = problem[len(PROBLEM_TEMPLATE_START):].split(" on ", 1)[0] form = XFormInstance.wrap(doc) form.deprecated_form_id = deprecated_id form.history.append( XFormOperation( user="******", date=datetime.utcnow(), operation="Resolved bad duplicate form during couch-to-sql " "migration. Original problem: %s" % problem, )) form.problem = None old_form = XFormInstance.get(deprecated_id) if old_form.initial_processing_complete and not form.initial_processing_complete: form.initial_processing_complete = True form.save() return form.to_json()
def modify_attachment_xml_and_metadata(form_data, form_attachment_new_xml, new_username): # Update XML form_data.put_attachment(form_attachment_new_xml, name="form.xml", content_type='text/xml') operation = XFormOperation(user_id=SYSTEM_USER_ID, date=datetime.utcnow(), operation='gdpr_scrub') form_data.history.append(operation) # Update metadata form_data.form['meta']['username'] = new_username form_data.save()
def apply_deprecation(cls, existing_xform, new_xform): # swap the revs new_xform._rev, existing_xform._rev = existing_xform._rev, new_xform._rev existing_xform.doc_type = XFormDeprecated.__name__ deprecated = XFormDeprecated.wrap(existing_xform.to_json()) assert not existing_xform.persistent_blobs, "some blobs would be lost" if existing_xform._deferred_blobs: deprecated._deferred_blobs = existing_xform._deferred_blobs.copy() user_id = (new_xform.auth_context and new_xform.auth_context.get('user_id')) or 'unknown' operation = XFormOperation(user=user_id, date=new_xform.edited_on, operation='edit') new_xform.history.append(operation) return deprecated, new_xform
def set_archived_state(form, archive, user_id): operation = "archive" if archive else "unarchive" form.doc_type = "XFormArchived" if archive else "XFormInstance" form.history.append(XFormOperation(operation=operation, user=user_id)) # subclasses explicitly set the doc type so force regular save XFormInstance.save(form)