cablegates/pandora/api/views.py

93 lines
3.1 KiB
Python
Raw Normal View History

2010-11-08 16:34:25 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2011-02-22 14:54:38 +00:00
from __future__ import division, with_statement
2010-11-08 16:34:25 +00:00
2011-01-22 19:29:56 +00:00
import os
2011-01-01 11:44:42 +00:00
from django.shortcuts import render_to_response
2010-11-08 16:34:25 +00:00
from django.template import RequestContext
from django.conf import settings
2011-01-24 02:35:52 +00:00
from django.db.models import Max, Sum
2010-11-08 16:34:25 +00:00
2011-01-01 11:44:42 +00:00
from ox.django.shortcuts import render_to_json_response, json_response
2011-01-22 19:29:56 +00:00
from ox.utils import json
2010-11-08 16:34:25 +00:00
2011-01-25 15:23:17 +00:00
from user.models import get_user_json
from item.models import ItemSort
2010-11-08 16:34:25 +00:00
from actions import actions
2011-01-01 11:44:42 +00:00
2010-11-08 16:34:25 +00:00
def api(request):
if request.META['REQUEST_METHOD'] == "OPTIONS":
2011-01-01 11:44:42 +00:00
response = render_to_json_response({'status': {'code': 200,
'text': 'use POST'}})
2010-11-08 16:34:25 +00:00
response['Access-Control-Allow-Origin'] = '*'
return response
if not 'action' in request.POST:
methods = actions.keys()
api = []
for f in sorted(methods):
2011-01-01 11:44:42 +00:00
api.append({'name': f,
'doc': actions.doc(f).replace('\n', '<br>\n')})
context = RequestContext(request, {'api': api,
2011-01-01 11:44:42 +00:00
'sitename': settings.SITENAME})
return render_to_response('api.html', context)
2010-11-08 16:34:25 +00:00
function = request.POST['action']
#FIXME: possible to do this in f
#data = json.loads(request.POST['data'])
2011-04-22 23:34:01 +00:00
f = actions.get(function)
2010-11-08 16:34:25 +00:00
if f:
response = f(request)
else:
response = render_to_json_response(json_response(status=400,
text='Unknown function %s' % function))
response['Access-Control-Allow-Origin'] = '*'
return response
2011-01-01 11:44:42 +00:00
2011-01-22 19:29:56 +00:00
def init(request):
2010-11-08 16:34:25 +00:00
'''
return {'status': {'code': int, 'text': string},
'data': {user: object}}
'''
#data = json.loads(request.POST['data'])
response = json_response({})
with open(settings.SITE_CONFIG) as f:
2011-01-25 15:23:17 +00:00
config = json.load(f)
2011-01-23 04:58:40 +00:00
2011-01-25 15:23:17 +00:00
config['site']['id'] = settings.SITEID
config['site']['name'] = settings.SITENAME
config['site']['sectionName'] = settings.SITENAME
config['site']['url'] = settings.URL
#populate max values for percent requests
for key in filter(lambda k: 'format' in k, config['itemKeys']):
if key['format']['type'] == 'percent' and key['format']['args'][0] == 'auto':
2011-01-24 02:35:52 +00:00
name = key['id']
if name == 'popularity':
name = 'item__accessed__accessed'
value = ItemSort.objects.aggregate(Sum(name))['%s__sum'%name]
else:
value = ItemSort.objects.aggregate(Max(name))['%s__max'%name]
2011-01-25 15:23:17 +00:00
key['format']['args'][0] = value
2011-05-28 11:18:28 +00:00
response['data']['site'] = config
2011-01-23 04:58:40 +00:00
if request.user.is_authenticated():
response['data']['user'] = get_user_json(request.user)
else:
2011-05-28 11:18:28 +00:00
response['data']['user'] = response['data']['site']['user']
2011-01-23 04:58:40 +00:00
2010-11-08 16:34:25 +00:00
return render_to_json_response(response)
2011-01-22 19:29:56 +00:00
actions.register(init)
2010-11-08 16:34:25 +00:00
2011-01-01 11:44:42 +00:00
def error(request):
2010-11-08 16:34:25 +00:00
'''
2011-01-01 11:44:42 +00:00
this action is used to test api error codes, it should return a 503 error
2010-11-08 16:34:25 +00:00
'''
success = error_is_success
return render_to_json_response({})
actions.register(error)