From a9398240d39f637119d0b32437c482928f42e0c0 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 29 Jan 2010 16:50:24 +0530 Subject: [PATCH] json debug, 404 --- oxdjango/shortcuts.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/oxdjango/shortcuts.py b/oxdjango/shortcuts.py index 7a07819..a00e240 100644 --- a/oxdjango/shortcuts.py +++ b/oxdjango/shortcuts.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 -from django.http import HttpResponse +from django.http import HttpResponse, Http404 try: import simplejson except ImportError: @@ -13,8 +13,20 @@ def render_to_json_response(dictionary, content_type="text/json", status=200): if settings.DEBUG: content_type = "text/javascript" indent = 2 - print simplejson.dumps(dictionary, indent=indent) + if settings.JSON_DEBUG: + print simplejson.dumps(dictionary, indent=2) if 'status' in dictionary and 'code' in dictionary['status']: status = dictionary['status']['code'] - return HttpResponse(simplejson.dumps(dictionary, indent=indent), content_type=content_type, status=status) + return HttpResponse(simplejson.dumps(dictionary, indent=indent), + content_type=content_type, status=status) +def get_object_or_404_json(klass, *args, **kwargs): + from django.shortcuts import _get_queryset + queryset = _get_queryset(klass) + try: + return queryset.get(*args, **kwargs) + except queryset.model.DoesNotExist: + #FIXME: how to raise exception that will only output given result + results = simplejson.dumps({'status': {'code': 404, + 'text': '%s not found' % queryset.model._meta.object_name}}) + raise Http404(results)