2010-11-08 16:34:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
from datetime import datetime
|
|
|
|
from urllib2 import unquote
|
|
|
|
import mimetypes
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q, Avg, Count, Sum
|
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404, redirect
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.conf import settings
|
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
from ox.utils import json
|
2010-11-23 09:53:12 +00:00
|
|
|
from ox.django.decorators import login_required_json
|
|
|
|
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
|
|
|
from ox.django.http import HttpFileResponse
|
2010-11-08 16:34:25 +00:00
|
|
|
import ox
|
|
|
|
|
|
|
|
import models
|
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
from pandora.user.models import get_user_json
|
2010-11-08 16:34:25 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
from pandora.archive.models import File
|
|
|
|
from pandora.archive import extract
|
2010-11-08 16:34:25 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
|
|
|
|
from actions import actions
|
|
|
|
|
2010-11-08 16:34:25 +00:00
|
|
|
def api(request):
|
|
|
|
if request.META['REQUEST_METHOD'] == "OPTIONS":
|
|
|
|
response = HttpResponse('')
|
|
|
|
response = render_to_json_response({'status': {'code': 200, 'text': 'use POST'}})
|
|
|
|
response['Access-Control-Allow-Origin'] = '*'
|
|
|
|
return response
|
|
|
|
if not 'action' in request.POST:
|
2010-12-22 07:45:37 +00:00
|
|
|
methods = actions.keys()
|
|
|
|
api = []
|
|
|
|
for f in sorted(methods):
|
|
|
|
api.append({
|
|
|
|
'name': f,
|
|
|
|
'doc': actions.doc(f).replace('\n', '<br>\n')
|
|
|
|
})
|
|
|
|
context = RequestContext(request, {'api': api,
|
|
|
|
'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'])
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
f = actions.get(function, None)
|
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
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def hello(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({})
|
|
|
|
if request.user.is_authenticated():
|
2010-11-27 12:12:53 +00:00
|
|
|
response['data']['user'] = get_user_json(request.user)
|
2010-11-08 16:34:25 +00:00
|
|
|
else:
|
|
|
|
response['data']['user'] = {'name': 'Guest', 'group': 'guest', 'preferences': {}}
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(hello)
|
2010-11-08 16:34:25 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def error(request):
|
2010-11-08 16:34:25 +00:00
|
|
|
'''
|
2010-12-22 07:45:37 +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({})
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(error)
|
2010-11-08 16:34:25 +00:00
|
|
|
|