Esempio n. 1
0
def submission(request, username):
    # request.FILES is a django.utils.datastructures.MultiValueDict
    # for each key we have a list of values
    xml_file_list = request.FILES.pop("xml_submission_file", [])
    if len(xml_file_list) != 1:
        return HttpResponseBadRequest(
            "There should be a single XML submission file."
            )

    # save this XML file and media files as attachments
    media_files = request.FILES.values()
    create_instance(
        username,
        xml_file_list[0],
        media_files
        )

    # ODK needs two things for a form to be considered successful
    # 1) the status code needs to be 201 (created)
    # 2) The location header needs to be set to the host it posted to
    response = HttpResponse("Your ODK submission was successful.")
    response.status_code = 201
    response['Location'] = request.build_absolute_uri(request.path)
    return response
Esempio n. 2
0
 def callback(xform_fs):
     """
     This callback is passed an instance of a XFormInstanceFS.
     See xform_fs.py for more info.
     """
     xml_file = django_file(xform_fs.path, field_name="xml_file", content_type="text/xml")
     images = [django_file(jpg, field_name="image", content_type="image/jpeg") for jpg in xform_fs.photos]
     # todo: if an instance has been submitted make sure all the
     # files are in the database.
     # there shouldn't be any instances with a submitted status in the
     instance = models.create_instance(user.username, xml_file, images, status)
     # close the files
     xml_file.close()
     for i in images:
         i.close()
     if instance:
         return 1
     else:
         return 0
Esempio n. 3
0
def import_instance(path_to_instance_folder, status, user):
    xml_files = glob.glob(os.path.join(path_to_instance_folder, "*.xml"))
    if len(xml_files) < 1:
        return
    if len(xml_files) > 1:
        raise Exception("Too many XML files.")
    xml_file = django_file(xml_files[0], field_name="xml_file", content_type="text/xml")
    images = []
    for jpg in glob.glob(os.path.join(path_to_instance_folder, "*.jpg")):
        image_file = django_file(jpg, field_name="image", content_type="image/jpeg")
        images.append(image_file)
    # todo: if an instance has been submitted make sure all the
    # files are in the database.
    # there shouldn't be any instances with a submitted status in the
    instance = models.create_instance(user.username, xml_file, images, status)
    # close the files
    xml_file.close()
    for i in images:
        i.close()
    return instance
Esempio n. 4
0
 if len(xml_file_list) != 1:
     return HttpResponseBadRequest(
         "There should be a single XML submission file."
         )
 # save this XML file and media files as attachments
 media_files = request.FILES.values()
 if not username:
     uuid = request.POST.get('uuid')
     if not uuid:
         return HttpResponseBadRequest("Username or ID required.")
     show_options = True
     xform = XForm.objects.get(uuid=uuid)
     username = xform.user.username
 instance = create_instance(
     username,
     xml_file_list[0],
     media_files
     )
 if instance == None:
     return HttpResponseBadRequest("Unable to create submission.")
 # ODK needs two things for a form to be considered successful
 # 1) the status code needs to be 201 (created)
 # 2) The location header needs to be set to the host it posted to
 if show_options:
     context.username = instance.user.username
     context.id_string = instance.xform.id_string
     context.domain = Site.objects.get(id=settings.SITE_ID).domain
     response = render_to_response("submission.html",
         context_instance=context)
 else:
     response = HttpResponse()