From 3ec896aa49d59202abfc0174dba3f4d8bdc014c6 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 15 Feb 2012 13:15:00 +0530 Subject: [PATCH] initial json/xml export --- pandora/item/urls.py | 4 ++++ pandora/item/views.py | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/pandora/item/urls.py b/pandora/item/urls.py index e5ac889ec..bd32102be 100644 --- a/pandora/item/urls.py +++ b/pandora/item/urls.py @@ -18,6 +18,10 @@ urlpatterns = patterns("item.views", #torrent (r'^(?P[A-Z0-9].*)/torrent/(?P.*?)$', 'torrent'), + #export + (r'^(?P[A-Z0-9].*)/json$', 'item_json'), + (r'^(?P[A-Z0-9].*)/xml$', 'item_xml'), + #srt export (r'^(?P[A-Z0-9].*)/(?P.+)\.srt$', 'srt'), diff --git a/pandora/item/views.py b/pandora/item/views.py index d02036282..cf3bb1b1f 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -996,6 +996,53 @@ def sitemap_xml(request): response['Content-Type'] = 'application/xml' return response +def item_json(request, id): + level = settings.CONFIG['capabilities']['canSeeItem']['guest'] + if not request.user.is_anonymous(): + level = request.user.get_profile().level + qs = models.Item.objects.filter(itemId=id, level__lte=level) + if qs.count() == 0: + response = json_response(status=404, text='not found') + else: + item = qs[0] + j = item.get_json() + j['layers'] = item.get_layers(request.user) + response = render_to_json_response(j) + return response + +def item_xml(request, id): + level = settings.CONFIG['capabilities']['canSeeItem']['guest'] + if not request.user.is_anonymous(): + level = request.user.get_profile().level + qs = models.Item.objects.filter(itemId=id, level__lte=level) + if qs.count() == 0: + response = json_response(status=404, text='not found') + else: + item = qs[0] + j = item.get_json() + j['layers'] = item.get_layers(request.user) + j['resolution'] = {'width': j['resolution'][0], 'height':j['resolution'][1]} + def xmltree(root, key, data): + if isinstance(data, list) or \ + isinstance(data, tuple): + for value in data: + xmltree(root, key, value) + elif isinstance(data, dict): + for k in data: + if data[k]: + xmltree(root, k, data[k]) + else: + e = ET.SubElement(root, key) + e.text = unicode(data) + + oxml = ET.Element('item') + xmltree(oxml, 'item', j) + response = HttpResponse( + '\n' + ET.tostring(oxml), + 'application/xml' + ) + return response + def item(request, id): id = id.split('/')[0] template = 'index.html'