From 84511cb22d1c1c89f91982501f1593f5ca83ec64 Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 11 Oct 2012 00:29:34 +0530 Subject: [PATCH] add json api to sms app to expose message log for phone numbers --- smsBEST/mumbai/views.py | 17 +++++++++++++++++ smsBEST/settings.py | 2 ++ smsBEST/urls.py | 1 + 3 files changed, 20 insertions(+) diff --git a/smsBEST/mumbai/views.py b/smsBEST/mumbai/views.py index 60f00ef..8b77f2d 100644 --- a/smsBEST/mumbai/views.py +++ b/smsBEST/mumbai/views.py @@ -1 +1,18 @@ # Create your views here. +from rapidsms.contrib.messagelog.models import Message +from ox.django.shortcuts import render_to_json_response + +def messages_json(request): + phone_no = request.GET.get("phone_no", None) + #TODO: validate phone no + if not phone_no: + return render_to_json_response({'error': 'no phone number provided'}) + messages = Message.objects.filter(connection__identity__endswith=phone_no) + ret = [] + for m in messages: + ret.append({ + 'text': m.text, + 'direction': m.direction, + 'datetime': m.date.isoformat() + }) + return render_to_json_response(ret) diff --git a/smsBEST/settings.py b/smsBEST/settings.py index a4476a7..547d078 100644 --- a/smsBEST/settings.py +++ b/smsBEST/settings.py @@ -21,6 +21,8 @@ DATABASES = { MEDIA_ROOT = join(PROJECT_PATH, 'static') LOCAL_DEVELOPMENT = True +JSON_DEBUG = False + # the rapidsms backend configuration is designed to resemble django's # database configuration, as a nested dict of (name, configuration). # diff --git a/smsBEST/urls.py b/smsBEST/urls.py index 7609259..834fe91 100644 --- a/smsBEST/urls.py +++ b/smsBEST/urls.py @@ -28,6 +28,7 @@ urlpatterns = patterns('', (r'^messaging/', include('rapidsms.contrib.messaging.urls')), (r'^registration/', include('rapidsms.contrib.registration.urls')), (r'^scheduler/', include('rapidsms.contrib.scheduler.urls')), + (r'^messages_json/', 'mumbai.views.messages_json'), ) if settings.LOCAL_DEVELOPMENT: