Skip to content

Commit

Permalink
move extraction of params from request to outside SubmissionPost
Browse files Browse the repository at this point in the history
breaks SubmissionPost API
  • Loading branch information
dannyroberts committed Dec 18, 2013
1 parent e3954a7 commit e4b4af9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
2 changes: 2 additions & 0 deletions couchforms/__init__.py
@@ -0,0 +1,2 @@
from .getters import *
from .util import SubmissionPost
4 changes: 3 additions & 1 deletion couchforms/const.py
Expand Up @@ -11,4 +11,6 @@
TAG_FORM = 'form'

ATTACHMENT_NAME = "form.xml"


MULTIPART_FILENAME_ERROR = object()
MAGIC_PROPERTY = 'xml_submission_file'
29 changes: 29 additions & 0 deletions couchforms/getters.py
@@ -0,0 +1,29 @@
from django.utils.datastructures import MultiValueDictKeyError
from couchforms.const import MAGIC_PROPERTY, MULTIPART_FILENAME_ERROR

__all__ = ['get_path', 'get_instance_and_attachment']


def get_path(request):
return request.path


def get_instance_and_attachment(request):
attachments = {}
if request.META['CONTENT_TYPE'].startswith('multipart/form-data'):
# it's an standard form submission (eg ODK)
# this does an assumption that ODK submissions submit using the form parameter xml_submission_file
# todo: this should be made more flexibly to handle differeing params for xform submission
try:
instance = request.FILES[MAGIC_PROPERTY].read()
except MultiValueDictKeyError:
instance = MULTIPART_FILENAME_ERROR
else:
for key, item in request.FILES.items():
if key != MAGIC_PROPERTY:
attachments[key] = item
else:
#else, this is a raw post via a j2me client of xml (or touchforms)
#todo, multipart raw submissions need further parsing capacity.
instance = request.raw_post_data
return instance, attachments
53 changes: 7 additions & 46 deletions couchforms/util.py
@@ -1,7 +1,7 @@
import hashlib
from django.conf import settings
from django.http import HttpResponse, HttpResponseServerError, HttpResponseBadRequest, HttpResponseForbidden
from django.utils.datastructures import MultiValueDictKeyError
from django.http import HttpResponse, HttpResponseServerError, HttpResponseBadRequest, HttpResponseForbidden, HttpRequest
from couchforms.const import MAGIC_PROPERTY, MULTIPART_FILENAME_ERROR
from dimagi.utils.mixins import UnicodeMixIn
import urllib
try:
Expand Down Expand Up @@ -203,54 +203,15 @@ def _log_hard_failure(instance, attachments, error):
return SubmissionErrorLog.from_instance(instance, message)


MULTIPART_FILENAME_ERROR = object()
MAGIC_PROPERTY = 'xml_submission_file'


def extract_instance_from_request(request):
attachments = {}
if request.META['CONTENT_TYPE'].startswith('multipart/form-data'):
# it's an standard form submission (eg ODK)
# this does an assumption that ODK submissions submit using the form parameter xml_submission_file
# todo: this should be made more flexibly to handle differeing params for xform submission
try:
instance = request.FILES[MAGIC_PROPERTY].read()
except MultiValueDictKeyError:
instance = MULTIPART_FILENAME_ERROR
else:
for key, item in request.FILES.items():
if key != MAGIC_PROPERTY:
attachments[key] = item
else:
#else, this is a raw post via a j2me client of xml (or touchforms)
#todo, multipart raw submissions need further parsing capacity.
instance = request.raw_post_data
return instance, attachments


class SubmissionPost(object):

def __init__(self, request=None, instance=None, attachments=None,
def __init__(self, instance=None, attachments=None,
auth_context=None, path=None):
"""
you can either pass in request or give explicit values for
instance and attachments (defaults to {})
(path is optional)
if you pass in all required values explicitly, request can be omitted
"""
attachments = attachments or {}
assert request or None not in (path, instance, attachments)
assert instance and not isinstance(instance, HttpRequest), instance
self.instance = instance
self.attachments = attachments or {}
self.auth_context = auth_context or DefaultAuthContext()

self.path = path or (request.path if request else None)
if instance:
self.instance = instance
self.attachments = attachments
else:
self.instance, self.attachments = \
extract_instance_from_request(request)
self.path = path

def get_response(self):
if not self.auth_context.is_valid():
Expand Down
8 changes: 7 additions & 1 deletion couchforms/views.py
@@ -1,6 +1,7 @@
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.http import HttpResponse, Http404
import couchforms
from couchforms.models import XFormInstance
from couchforms.util import SubmissionPost

Expand All @@ -16,7 +17,12 @@ def post(request):
a different signature as play, only passing in the document
(since we don't know what xform was being posted to)
"""
return SubmissionPost(request).get_response()
instance, attachments = couchforms.get_instance_and_attachment(request)
return SubmissionPost(
instance=instance,
attachments=attachments,
path=couchforms.get_path(request),
).get_response()


def download_form(request, instance_id):
Expand Down

0 comments on commit e4b4af9

Please sign in to comment.