<commit_before>from django.db import models

# Create your models here.
<commit_msg>added first draft of controller<commit_after>from django.db import models
from django.db.models.signals import post_save

from xml.sax.handler import ContentHandler
from xml.sax import parseString

from odk_dropbox.models import Submission
from eav.models import Attribute, Value

class EAVHandler(ContentHandler):

    def __init__ (self, submission):
        # The stack should start with the ODK Submission
        self._stack = [submission]
        self._objects = []

    def get_objects(self):
        return self._objects

    def startElement(self, name, attrs):
        # Create a new eav.Value
        # the entity is the top of stack
        # the attribute is shares the name of this tag
        a = Attribute.objects.get(slug=name)
        o = Value(entity=self._stack.top(), attribute=a)
        self._stack.append(o)
        self._objects.append(o)

        # make sure there are no attributes
        assert attrs.getLength()==0, "This parser is not equipped to handle attributes."

    def characters(self, content):
        # ignore whitespace
        s = content.strip()
        if s:
            # set the value of the eav.Value on the top of the stack
            self._stack.top().value = content

    def endElement(self, name):
        top = self._stack.pop()
        assert name==top.attribute.slug, "endElement doesn't match last startElement"


def add_to_eav(submission):
    handler = EAVHandler(submission)
    parseString(submission.xml, handler)
    # save all the objects created by parsing the submission's xml
    for o in handler.get_objects:
        o.save()

def _add_to_eav(sender, **kwargs):
    # if this is a new submission add it to the eav
    if kwargs["created"]:
        add_to_eav(kwargs["instance"])

post_save.connect(_add_to_eav, sender=Submission)

{'fraction_non_alphanumeric': 0.08333333333333333, 'fraction_numerical': 0.0005307855626326964, 'mean_word_length': 2.9270833333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7278984', 'n_tokens_mistral': 541, 'n_tokens_neox': 534, 'n_words': 180}