import logging
from django.http import HttpResponse
from couchforms.views import post as couchforms_post
from corehq.apps.receiver.signals import post_received, ReceiverResult

def post(request, domain):
    def callback(doc):
        doc['#export_tag'] = ["domain", "xmlns"]
        doc['submit_ip'] = request.META['REMOTE_ADDR']
        doc['domain'] = domain
        doc.save()
        feedback = post_received.send_robust(sender="receiver", xform=doc)
        responses = []
        for func, resp in feedback:
            if resp and isinstance(resp, Exception):
                logging.error("Receiver app: problem sending post-save signal %s for xform %s" \
                              % (func, doc._id))
                logging.exception(resp)
            elif resp and isinstance(resp, ReceiverResult):
                # use the first valid response if we get one 
                responses.append(resp)
        if responses:
            responses.sort()
            return HttpResponse(responses[-1].response)
        # default to something generic
        return HttpResponse("Success! Received XForm id is: %s\n" % doc['_id'])
    return couchforms_post(request, callback)
@@ -355,16 +355,1167 @@
  domain%0A
+        def _scrub_meta(doc):%0A            property_map = %7B%22TimeStart%22: %22timeStart%22,%0A                            %22TimeEnd%22: %22timeEnd%22,%0A                            %22chw_id%22: %22userID%22,%0A                            %22DeviceID%22: %22deviceID%22,%0A                            %22uid%22: %22instanceID%22%7D%0A            %0A            # hack to make sure uppercase meta still ends up in the right place%0A            if %22Meta%22 in doc.form:%0A                doc.form%5B%22meta%22%5D = doc.form%5B%22Meta%22%5D%0A                del doc.form%5B%22Meta%22%5D%0A                logging.error(%22form %25s contains old-format metadata.  You should update it!!%22 %25 doc.get_id)%0A            if %22meta%22 in doc.form:%0A                # scrub values from 0.9 to 1.0%0A                for key in doc.form%5B%22meta%22%5D:%0A                    if key in property_map and property_map%5Bkey%5D not in doc.form%5B%22meta%22%5D:%0A                        logging.error(%22Moving deprecated meta property %25s to %25s.  You should update your forms to be standards compliant!%22 %25 (key, property_map%5Bkey%5D))%0A                        doc.form%5B%22meta%22%5D%5Bproperty_map%5Bkey%5D%5D = doc.form%5B%22meta%22%5D%5Bkey%5D%0A                        del doc.form%5B%22meta%22%5D%5Bkey%5D%0A        _scrub_meta(doc)%0A
         

add hacky scrubbing logic so hq 1.0 can parse old (0.9) forms like they're compliant with the new spec