<commit_before># vim: ai ts=4 sts=4 et sw=4 encoding=utf-8

from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.db import transaction

from .instance import Instance
from .xform import XForm
from .attachment import Attachment
from .survey_type import SurveyType


@transaction.commit_on_success
def create_instance(username, xml_file, media_files, status=u'submitted_via_web'):
    """
    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.
    """
    xml = xml_file.read()
    xml_file.close()
    user = get_object_or_404(User, username=username)
    
    instance, created = Instance.objects.get_or_create(xml=xml, user=user, status=status)
    if created:
        for f in media_files:
            Attachment.objects.get_or_create(instance=instance, media_file=f)
        return instance
    return None
<commit_msg>no longer breaks when there are 2 or more identical XML documents in the database (rare cases)<commit_after># vim: ai ts=4 sts=4 et sw=4 encoding=utf-8

from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.db import transaction

from .instance import Instance
from .xform import XForm
from .attachment import Attachment
from .survey_type import SurveyType


@transaction.commit_on_success
def create_instance(username, xml_file, media_files, status=u'submitted_via_web'):
    """
    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.
    """
    xml = xml_file.read()
    xml_file.close()
    user = get_object_or_404(User, username=username)
    existing_instance_count = Instance.objects.filter(xml=xml, user=user).count()
    if existing_instance_count == 0:
        instance = Instance.objects.create(xml=xml, user=user, status=status)
        for f in media_files:
            Attachment.objects.get_or_create(instance=instance, media_file=f)
        return instance
    return None
{'fraction_non_alphanumeric': 0.07187222715173026, 'fraction_numerical': 0.009760425909494233, 'mean_word_length': 3.9343544857768054, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5423534', 'n_tokens_mistral': 667, 'n_tokens_neox': 652, 'n_words': 229}