api.api add cache info, add list type

This commit is contained in:
j 2011-01-13 08:33:14 +00:00
parent b99931b3ab
commit 839c9761fd
11 changed files with 68 additions and 51 deletions

View file

@ -70,7 +70,7 @@ def addAnnotation(request):
response = {'status': {'code': 501, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addAnnotation) actions.register(addAnnotation, cache=False)
@login_required_json @login_required_json
@ -86,7 +86,7 @@ def removeAnnotation(request):
''' '''
response = {'status': {'code': 501, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeAnnotation) actions.register(removeAnnotation, cache=False)
@login_required_json @login_required_json
@ -114,4 +114,4 @@ def editAnnotation(request):
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editAnnotation) actions.register(editAnnotation, cache=False)

View file

@ -48,7 +48,7 @@ def trim(docstring):
class ApiActions(dict): class ApiActions(dict):
properties = {}
def __init__(self): def __init__(self):
def api(request): def api(request):
@ -57,11 +57,14 @@ class ApiActions(dict):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {actions: ['api', 'hello', ...]}} 'data': {actions: ['api', 'hello', ...]}}
''' '''
actions = self.keys() _actions = self.keys()
actions.sort() _actions.sort()
actions = {}
for a in _actions:
actions[a] = self.properties[a]
response = json_response({'actions': actions}) response = json_response({'actions': actions})
return render_to_json_response(response) return render_to_json_response(response)
self['api'] = api self.register(api)
def apidoc(request): def apidoc(request):
''' '''
@ -74,15 +77,16 @@ class ApiActions(dict):
docs[f] = self.doc(f) docs[f] = self.doc(f)
return render_to_json_response(json_response({'actions': docs})) return render_to_json_response(json_response({'actions': docs}))
self['apidoc'] = apidoc self.register(apidoc)
def doc(self, f): def doc(self, f):
return trim(self[f].__doc__) return trim(self[f].__doc__)
def register(self, method, action=None): def register(self, method, action=None, cache=True):
if not action: if not action:
action = method.func_name action = method.func_name
self[action] = method self[action] = method
self.properties[action] = {'cache': cache}
def unregister(self, action): def unregister(self, action):
if action in self: if action in self:

View file

@ -34,7 +34,7 @@ def removeVolume(request):
except models.Volume.DoesNotExist: except models.Volume.DoesNotExist:
response = json_response(status=404, text='volume not found') response = json_response(status=404, text='volume not found')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeVolume) actions.register(removeVolume, cache=False)
@login_required_json @login_required_json
@ -99,7 +99,7 @@ def update(request):
response['data']['file'] = [f.file.oshash for f in files.filter(file__is_subtitle=True)] response['data']['file'] = [f.file.oshash for f in files.filter(file__is_subtitle=True)]
return render_to_json_response(response) return render_to_json_response(response)
actions.register(update) actions.register(update, cache=False)
@login_required_json @login_required_json
@ -147,7 +147,7 @@ def upload(request):
else: else:
response = json_response(status=403, text='permissino denied') response = json_response(status=403, text='permissino denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(upload) actions.register(upload, cache=False)
class VideoChunkForm(forms.Form): class VideoChunkForm(forms.Form):
@ -216,7 +216,7 @@ def taskStatus(request):
task_id = data['task_id'] task_id = data['task_id']
response = task_status(request, task_id) response = task_status(request, task_id)
return render_to_json_response(response) return render_to_json_response(response)
actions.register(taskStatus) actions.register(taskStatus, cache=False)
@login_required_json @login_required_json
@ -251,7 +251,7 @@ def editFile(request):
#FIXME: other things might need updating here #FIXME: other things might need updating here
response = json_response(text='updated') response = json_response(text='updated')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editFile) actions.register(editFile, cache=False)
def lookup_file(request, oshash): def lookup_file(request, oshash):

View file

@ -20,7 +20,7 @@ def addDate(request):
else: else:
response = json_response(status=403, text='place name exists') response = json_response(status=403, text='place name exists')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addDate) actions.register(addDate, cache=False)
@login_required_json @login_required_json
@ -51,14 +51,14 @@ def editDate(request):
else: else:
response = json_response(status=403, text='permission denied') response = json_response(status=403, text='permission denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editDate) actions.register(editDate, cache=False)
@login_required_json @login_required_json
def removeDate(request): def removeDate(request):
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeDate) actions.register(removeDate, cache=False)
def findDate(request): def findDate(request):

View file

@ -207,7 +207,7 @@ Positions
response['data']['items'] = [only_p(m['json']) for m in qs.values('json')] response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]
else: # otherwise stats else: # otherwise stats
items = query['qs'] items = query['qs']
files = File.objects.all().filter(item__in=items) files = File.objects.all().filter(item__in=items).exclude(size__gt=0)
r = files.aggregate( r = files.aggregate(
Sum('duration'), Sum('duration'),
Sum('pixels'), Sum('pixels'),
@ -217,7 +217,7 @@ Positions
response['data']['files'] = files.count() response['data']['files'] = files.count()
response['data']['items'] = items.count() response['data']['items'] = items.count()
response['data']['pixels'] = r['pixels__sum'] response['data']['pixels'] = r['pixels__sum']
response['data']['runtime'] = items.filter(sort__runtime__gt=0).aggregate(Sum('sort__runtime'))['sort__runtime__sum'] response['data']['runtime'] = items.filter(sort__runtime_desc__gt=0).aggregate(Sum('sort__runtime_desc'))['sort__runtime_desc__sum']
if response['data']['runtime'] == None: if response['data']['runtime'] == None:
response['data']['runtime'] = 1337 response['data']['runtime'] = 1337
response['data']['size'] = r['size__sum'] response['data']['size'] = r['size__sum']
@ -309,7 +309,7 @@ def editItem(request):
else: else:
response = json_response(status=403, text='permissino denied') response = json_response(status=403, text='permissino denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editItem) actions.register(editItem, cache=False)
@login_required_json @login_required_json
def removeItem(request): def removeItem(request):
@ -327,7 +327,7 @@ def removeItem(request):
else: else:
response = json_response(status=403, text='permission denied') response = json_response(status=403, text='permission denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeItem) actions.register(removeItem, cache=False)
''' '''
Poster API Poster API
@ -372,7 +372,7 @@ def setPosterFrame(request): #parse path and return info
else: else:
response = json_response(status=403, text='permissino denied') response = json_response(status=403, text='permissino denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(setPosterFrame) actions.register(setPosterFrame, cache=False)
def setPoster(request): #parse path and return info def setPoster(request): #parse path and return info
''' '''
@ -404,7 +404,7 @@ def setPoster(request): #parse path and return info
else: else:
response = json_response(status=403, text='permission denied') response = json_response(status=403, text='permission denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(setPoster) actions.register(setPoster, cache=False)
def getImdbId(request): def getImdbId(request):
''' '''

View file

@ -23,6 +23,7 @@ class List(models.Model):
status = models.CharField(max_length=20, default='private') status = models.CharField(max_length=20, default='private')
_status = ['private', 'public', 'featured'] _status = ['private', 'public', 'featured']
query = DictField(default={"static": True}) query = DictField(default={"static": True})
type= models.CharField(max_length=255, default='static')
items = models.ManyToManyField('item.Item', related_name='lists', items = models.ManyToManyField('item.Item', related_name='lists',
through='ListItem') through='ListItem')
@ -32,6 +33,10 @@ class List(models.Model):
objects = managers.ListManager() objects = managers.ListManager()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.query.get('static', False):
self.type = 'static'
else:
self.type = 'smart'
super(List, self).save(*args, **kwargs) super(List, self).save(*args, **kwargs)
def get_number_of_items(self, user=None): def get_number_of_items(self, user=None):
@ -64,7 +69,7 @@ class List(models.Model):
return True return True
return False return False
def json(self, keys=['id', 'name', 'user', 'query', 'status'], user=None): def json(self, keys=['id', 'name', 'user', 'type', 'query', 'status'], user=None):
response = {} response = {}
for key in keys: for key in keys:
if key == 'items': if key == 'items':

View file

@ -122,7 +122,7 @@ def addListItem(request):
else: else:
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addListItem) actions.register(addListItem, cache=False)
@login_required_json @login_required_json
@ -154,7 +154,7 @@ def removeListItem(request):
else: else:
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeListItem) actions.register(removeListItem, cache=False)
@login_required_json @login_required_json
@ -184,7 +184,7 @@ def addList(request):
response = json_response(status=200, text='list already exists') response = json_response(status=200, text='list already exists')
response['data']['errors'] = {'name': 'List already exists'} response['data']['errors'] = {'name': 'List already exists'}
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addList) actions.register(addList, cache=False)
@login_required_json @login_required_json
@ -208,10 +208,18 @@ def editList(request):
if list.editable(request.user): if list.editable(request.user):
response = json_response() response = json_response()
for key in data: for key in data:
if key in ('name', 'status', 'query'): if key in ('name', 'status', 'query', 'type'):
if key in data: if key in data:
if key == 'query' and not data['query']: if key == 'query' and not data['query']:
setattr(list, key, {"static":True}) setattr(list, key, {"static":True})
elif key == 'type':
if data[key] == 'static':
list.query = {"static":True}
list.type = 'static'
else:
list.type = 'dynamic'
if list.query.get('static', False):
list.query = {}
elif key == 'status': elif key == 'status':
value = data[key] value = data[key]
if value not in list._status: if value not in list._status:
@ -239,7 +247,7 @@ def editList(request):
else: else:
response = json_response(status=403, text='not allowed') response = json_response(status=403, text='not allowed')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editList) actions.register(editList, cache=False)
@login_required_json @login_required_json
@ -265,7 +273,7 @@ def removeList(request):
else: else:
response = json_response(status=403, text='not allowed') response = json_response(status=403, text='not allowed')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeList) actions.register(removeList, cache=False)
@login_required_json @login_required_json
@ -291,7 +299,7 @@ def subscribeToList(request):
pos.save() pos.save()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(subscribeToList) actions.register(subscribeToList, cache=False)
@login_required_json @login_required_json
def unsubscribeFromList(request): def unsubscribeFromList(request):
@ -313,7 +321,7 @@ def unsubscribeFromList(request):
models.Position.objects.filter(list=list, user=request.user, section='public').delete() models.Position.objects.filter(list=list, user=request.user, section='public').delete()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(unsubscribeFromList) actions.register(unsubscribeFromList, cache=False)
@login_required_json @login_required_json
@ -359,5 +367,5 @@ def sortLists(request):
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(sortLists) actions.register(sortLists, cache=False)

View file

@ -36,7 +36,7 @@ def addPlace(request):
else: else:
response = json_response(status=403, text='place name exists') response = json_response(status=403, text='place name exists')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addPlace) actions.register(addPlace, cache=False)
@login_required_json @login_required_json
@ -67,14 +67,14 @@ def editPlace(request):
else: else:
response = json_response(status=403, text='permission denied') response = json_response(status=403, text='permission denied')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editPlace) actions.register(editPlace, cache=False)
@login_required_json @login_required_json
def removePlace(request): def removePlace(request):
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removePlace) actions.register(removePlace, cache=False)
def findPlace(request): def findPlace(request):

View file

@ -39,7 +39,7 @@ def editNews(request):
item = get_object_or_404_json(models.Text, pk=itemId) item = get_object_or_404_json(models.Text, pk=itemId)
response['data']['page'] = item.html() response['data']['page'] = item.html()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editNews) actions.register(editNews, cache=False)
def findNews(request): def findNews(request):
@ -78,7 +78,7 @@ def editText(request):
item = get_object_or_404_json(models.Text, pk=itemId) item = get_object_or_404_json(models.Text, pk=itemId)
response['data']['page'] = item.html() response['data']['page'] = item.html()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editText) actions.register(editText, cache=False)
def findText(request): def findText(request):

View file

@ -37,7 +37,7 @@ def addClip(request):
else: else:
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addClip) actions.register(addClip, cache=False)
@login_required_json @login_required_json
@ -65,7 +65,7 @@ def removeClip(request):
else: else:
response = json_response(status=501, text='not implemented') response = json_response(status=501, text='not implemented')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeClip) actions.register(removeClip, cache=False)
def getTimeline(request): def getTimeline(request):
@ -123,7 +123,7 @@ def addTimeline(request):
'name': 'List already exists' 'name': 'List already exists'
} }
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addTimeline) actions.register(addTimeline, cache=False)
@login_required_json @login_required_json
@ -145,7 +145,7 @@ def editTimeline(request):
else: else:
response = json_response(status=403, text='not allowed') response = json_response(status=403, text='not allowed')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editTimeline) actions.register(editTimeline, cache=False)
@login_required_json @login_required_json
@ -163,4 +163,4 @@ def removeTimeline(request):
else: else:
response = json_response(status=403, text='not allowed') response = json_response(status=403, text='not allowed')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeTimeline) actions.register(removeTimeline, cache=False)

View file

@ -79,7 +79,7 @@ def signin(request):
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(signin) actions.register(signin, cache=False)
def signout(request): def signout(request):
@ -103,7 +103,7 @@ def signout(request):
response['data']['user'] = site_config['user'] response['data']['user'] = site_config['user']
return render_to_json_response(response) return render_to_json_response(response)
actions.register(signout) actions.register(signout, cache=False)
class SignupForm(forms.Form): class SignupForm(forms.Form):
@ -180,7 +180,7 @@ def signup(request):
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(signup) actions.register(signup, cache=False)
def resetPassword(request): def resetPassword(request):
@ -235,7 +235,7 @@ def resetPassword(request):
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(resetPassword) actions.register(resetPassword, cache=False)
def requestToken(request): def requestToken(request):
@ -301,7 +301,7 @@ def requestToken(request):
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(requestToken) actions.register(requestToken, cache=False)
def findUser(request): def findUser(request):
@ -366,7 +366,7 @@ def contact(request):
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(contact) actions.register(contact, cache=False)
def getPositionById(list, key): def getPositionById(list, key):
@ -399,7 +399,7 @@ def setPreferences(request):
profile.save() profile.save()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(setPreferences) actions.register(setPreferences, cache=False)
@login_required_json @login_required_json
@ -430,5 +430,5 @@ def setUI(request):
profile.save() profile.save()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(setUI) actions.register(setUI, cache=False)