def create_instance(username, xml_file, media_files,
status=u'submitted_via_web', uuid=None,
date_created_override=None, request=None):
"""
I used to check if this file had been submitted already, I've
taken this out because it was too slow. Now we're going to create
a way for an admin to mark duplicate instances. This should
simplify things a bit.
Submission cases:
If there is a username and no uuid, submitting an old ODK form.
If there is a username and a uuid, submitting a new ODK form.
"""
try:
instance = None
submitted_by = request.user \
if request and request.user.is_authenticated() else None
if username:
username = username.lower()
xml = xml_file.read()
xform = get_xform_from_submission(xml, username, uuid)
check_submission_permissions(request, xform)
existing_instance_count = Instance.objects.filter(
xml=xml, xform__user=xform.user).count()
if existing_instance_count > 0:
existing_instance = Instance.objects.filter(
xml=xml, xform__user=xform.user)[0]
if not existing_instance.xform or\
existing_instance.xform.has_start_time:
# Ignore submission as a duplicate IFF
# * a submission's XForm collects start time
# * the submitted XML is an exact match with one that
# has already been submitted for that user.
raise DuplicateInstance()
# get new and depracated uuid's
new_uuid = get_uuid_from_xml(xml)
duplicate_instances = Instance.objects.filter(uuid=new_uuid)
if duplicate_instances:
for f in media_files:
Attachment.objects.get_or_create(
instance=duplicate_instances[0],
media_file=f, mimetype=f.content_type)
# ensure we have saved the extra attachments
transaction.commit()
raise DuplicateInstance()
instance = save_submission(xform, xml, media_files, new_uuid,
submitted_by, status, date_created_override)
# commit all changes
transaction.commit()
return instance
except Exception:
transaction.rollback()
raise
评论列表
文章目录