<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 coding=utf-8

import os, glob
from django.core.management.base import BaseCommand
from django.core.files.uploadedfile import InMemoryUploadedFile
from ...models import make_submission, InstanceImage

def django_file(path, field_name, content_type):
    # adapted from here: http://groups.google.com/group/django-users/browse_thread/thread/834f988876ff3c45/
    f = open(path)
    return InMemoryUploadedFile(
        file=f,
        field_name=field_name,
        name=f.name,
        content_type=content_type,
        size=os.path.getsize(path),
        charset=None
        )

class Command(BaseCommand):
    help = "Import a folder of ODK instances."

    def handle(self, *args, **kwargs):
        path = args[0]
        for instance in glob.glob( os.path.join(path, "*") ):
            xml_files = glob.glob( os.path.join(instance, "*.xml") )
            if len(xml_files)<1: continue
            # if len(xml_files)>1: raise Exception(
            #     "Figure out how to handle multiple XML files",
            #     xml_files
            #     )
            if len(xml_files)>1: print xml_files
            f = django_file(xml_files[0],
                            field_name="xml_file",
                            content_type="text/xml")
            s = make_submission(f)

            for jpg in glob.glob( os.path.join(instance, "*.jpg") ):
                f = django_file(jpg,
                                field_name="image",
                                content_type="image/jpeg")
                InstanceImage.objects.get_or_create(
                    instance=s.instance, image=f
                    )
<commit_msg>cleaned up using make submission function<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 coding=utf-8

import os, glob
from django.core.management.base import BaseCommand
from django.core.files.uploadedfile import InMemoryUploadedFile
from ...models import make_submission, InstanceImage

def django_file(path, field_name, content_type):
    # adapted from here: http://groups.google.com/group/django-users/browse_thread/thread/834f988876ff3c45/
    f = open(path)
    return InMemoryUploadedFile(
        file=f,
        field_name=field_name,
        name=f.name,
        content_type=content_type,
        size=os.path.getsize(path),
        charset=None
        )

class Command(BaseCommand):
    help = "Import a folder of ODK instances."

    def handle(self, *args, **kwargs):
        path = args[0]
        for instance in glob.glob( os.path.join(path, "*") ):
            xml_files = glob.glob( os.path.join(instance, "*.xml") )
            if len(xml_files)<1: continue
            # we need to figure out what to do if there are multiple
            # xml files in the same folder.
            xml_file = django_file(xml_files[0],
                                   field_name="xml_file",
                                   content_type="text/xml")
            images = []
            for jpg in glob.glob(os.path.join(instance, "*.jpg")):
                images.append(
                    django_file(jpg,
                                field_name="image",
                                content_type="image/jpeg")
                    )
            make_submission(xml_file, images)

{'fraction_non_alphanumeric': 0.10328214393255043, 'fraction_numerical': 0.012044564890093345, 'mean_word_length': 1.9089316987740805, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 19}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1446857', 'n_tokens_mistral': 991, 'n_tokens_neox': 952, 'n_words': 215}