Commits

Matthew Schinckel committed d7ed6b2

Better handling of when django decides to put my query terms into lists.

Comments (0)

Files changed (1)

 def update_delivery_status(request):
     logging.debug("Status update received.")
     logging.debug("%s" % request.GET)
-    msg = Message.objects.get_matching_message(request.GET)
+    data = {}
+    for k,v in request.GET:
+        if isinstance(v, list) and len(v) == 1:
+            data[k] = v[0]
+        else:
+            data[k] = v
+            
+    msg = Message.objects.get_matching_message(data)
     
     if not msg:
-        logging.error("%s" % request.GET)
+        logging.error("%s" % data)
         logging.error("Message could not be found.")
         return HttpResponse('OK')
         
     ):
         msg.status = "Failed"
     else:
-        msg.status = gateway.status_mapping[request.GET.get(gateway.status_status)]
+        msg.status = gateway.status_mapping[data.get(gateway.status_status)]
     logging.debug("Updated status to %s" % msg.status)
     if msg.status == "Delivered":
         if gateway.status_date_format:
             msg.delivery_date = datetime.datetime.strptime(
-                request.GET.get(gateway.status_date),
+                data.get(gateway.status_date),
                 gateway.status_date_format,
             )
         else:
             msg.delivery_date = datetime.datetime.fromtimestamp(
-                float(request.GET.get(gateway.status_date))
+                float(data.get(gateway.status_date))
             )
         logging.debug("Message was delivered at %s" % msg.delivery_date)
     else:
-        msg.status_message = request.GET.get(gateway.status_error_code)
+        msg.status_message = data.get(gateway.status_error_code)
         logging.debug("Error message was %s" % msg.status_message)
     msg.save()
     return HttpResponse('OK')
 @csrf_exempt
 def handle_reply(request):
     logging.debug("SMS Reply received.")
-    msg = Message.objects.get_original_for_reply(request.GET)
+    data = {}
+    for k,v in request.GET:
+        if isinstance(v, list) and len(v) == 1:
+            data[k] = v[0]
+        else:
+            data[k] = v
+    
+    msg = Message.objects.get_original_for_reply(data)
     
     if not msg:
-        logging.error("%s" % request.GET)
+        logging.error("%s" % data)
         logging.error("Original message could not be found.")
         return HttpResponse('OK')
         
     logging.debug("Found message %i" % msg.pk)
     gateway = msg.gateway
     reply = msg.replies.create(
-        content=request.GET.get(gateway.reply_content),
+        content=data.get(gateway.reply_content),
     )
     if gateway.reply_date_format:
         reply.date = datetime.datetime.strptime(
-            request.GET.get(gateway.reply_date),
+            data.get(gateway.reply_date),
             gateway.reply_date_format,
         )
     else:
         reply.date = datetime.datetime.fromtimestamp(float(
-            request.GET.get(gateway.reply_date)
+            data.get(gateway.reply_date)
         ))
     logging.debug("Message content was: %s" % reply.content)