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.
    """
    with transaction.atomic():
        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:
            # ensure we have saved the extra attachments
            for f in media_files:
                Attachment.objects.get_or_create(
                    instance=duplicate_instances[0],
                    media_file=f, mimetype=f.content_type)
        else:
            instance = save_submission(xform, xml, media_files, new_uuid,
                                       submitted_by, status,
                                       date_created_override)
            return instance

    if duplicate_instances:
        # We are now outside the atomic block, so we can raise an exception
        # without rolling back the extra attachments we created earlier
        # NB: Since `ATOMIC_REQUESTS` is set at the database level, everything
        # could still be rolled back if the calling view fails to handle an
        # exception
        raise DuplicateInstance()
dataset/ETHPy150Open kobotoolbox/kobocat/onadata/libs/utils/logger_tools.py/create_instance/original