From 66e6fb812ef2a7623415c38b0ff42ae39fa6cad5 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 30 Jul 2011 12:51:23 +0200 Subject: [PATCH] new api get({id, keys}, one poster per service --- pandora/item/models.py | 37 +++++++++++++++++++++++++------------ pandora/item/views.py | 32 ++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 72b4ab8..42be297 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -308,17 +308,27 @@ class Item(models.Model): return poster def get_posters(self): - posters = { - 'local': [{ + url = self.prefered_poster_url() + posters = [ + { 'url': '/%s/poster.pandora.jpg' % self.itemId, 'width': 640, 'height': 1024, - }] - } - for p in self.poster_urls.all(): - if p.service not in posters: - posters[p.service] = [] - posters[p.service].append({'url': p.url, 'width': p.width, 'height': p.height}) + 'service': settings.URL, + 'selected': url == None + } + ] + got = {} + for p in self.poster_urls.all().order_by('-height'): + if p.service not in got: + got[p.service] = 1 + posters.append({ + 'url': p.url, + 'width': p.width, + 'height': p.height, + 'service': p.service, + 'selected': p.url == url + }) return posters def get_stream(self): @@ -369,10 +379,13 @@ class Item(models.Model): if value: i[key] = value - i['poster'] = self.get_poster() - i['posters'] = self.get_posters() - i['posterFrames'] = ['/%s/frame/poster/%d.jpg' %(self.itemId, p) - for p in range(0, len(self.poster_frames()))] + if not fields or 'poster' in fields: + i['poster'] = self.get_poster() + if not fields or 'posters' in fields: + i['posters'] = self.get_posters() + if not fields or 'frames' in fields: + i['frames'] = ['/%s/frame/poster/%d.jpg' %(self.itemId, p) + for p in range(0, len(self.poster_frames()))] return i diff --git a/pandora/item/views.py b/pandora/item/views.py index 441dc4e..f78e9b7 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -326,6 +326,31 @@ def autocomplete(request): actions.register(autocomplete) +def get(request): + ''' + param data { + id: string + keys: array + } + return item array + ''' + response = json_response({}) + data = json.loads(request.POST['data']) + item = get_object_or_404_json(models.Item, itemId=data['id']) + if item.access(request.user): + info = item.get_json(data['keys']) + if 'stream' in data['keys']: + info['stream'] = item.get_stream() + if 'layers' in data['keys']: + info['layers'] = item.get_layers(request.user) + response['data'] = {} + for key in data['keys']: + response['data'][key] = info[key] + else: + response = json_response(status=403, text='permission denied') + return render_to_json_response(response) +actions.register(get) + def getItem(request): ''' param data @@ -450,7 +475,7 @@ def setPoster(request): #parse path and return info item = get_object_or_404_json(models.Item, itemId=data['id']) response = json_response() if item.editable(request.user): - valid_urls = [p.url for p in item.poster_urls.all()] + valid_urls = [p['url'] for p in item.get_posters()] if data['url'] in valid_urls: item.poster_url = data['url'] if item.poster: @@ -459,11 +484,6 @@ def setPoster(request): #parse path and return info tasks.update_poster.delay(item.itemId) response = json_response() response['data']['poster'] = item.get_poster() - elif data['url'].endswith('/poster.pandora.jpg'): - item.poster_url = '' - item.save() - tasks.update_poster.delay(item.itemId) - response['data']['poster'] = item.get_poster() else: response = json_response(status=403, text='invalid poster url') else: