From 2e507e9e3e55298867ce89cb14d8ed73fe620203 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 18 Feb 2012 16:59:26 +0530 Subject: [PATCH] news --- pandora/0xdb.jsonc | 2 +- pandora/news/models.py | 17 +++---- pandora/news/views.py | 100 ++++++----------------------------------- 3 files changed, 21 insertions(+), 98 deletions(-) diff --git a/pandora/0xdb.jsonc b/pandora/0xdb.jsonc index f37a0c65..357ba08a 100644 --- a/pandora/0xdb.jsonc +++ b/pandora/0xdb.jsonc @@ -31,7 +31,7 @@ "canEditPlaces": {"staff": true, "admin": true}, "canEditSitePages": {"staff": true, "admin": true}, "canEditUsers": {"admin": true}, - "canImportAnnotations": {"staff": true, "admin": true}, + "canImportAnnotations": {}, "canPlayClips": {"guest": 2, "member": 2, "friend": 4, "staff": 4, "admin": 4}, "canPlayVideo": {"guest": 1, "member": 1, "friend": 4, "staff": 4, "admin": 4}, "canSeeDebugMenu": {"staff": true, "admin": true}, diff --git a/pandora/news/models.py b/pandora/news/models.py index 71d18e37..3d6b18bb 100644 --- a/pandora/news/models.py +++ b/pandora/news/models.py @@ -14,18 +14,13 @@ class News(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) - user = models.ForeignKey(User, related_name='news') title = models.TextField() - content = models.TextField() + date = models.TextField() + text = models.TextField() def editable(self, user): - if user.is_authenticated(): - if user.get_profile().get_level() in ('staff', 'admin') or \ - self.user == user or \ - user.groups.filter(id__in=self.groups.all()).count() > 0: - return True - return False + return user.is_authenticated() and user.get_profile().capability("canEditSitePages") ''' def save(self, *args, **kwargs): @@ -34,10 +29,10 @@ class News(models.Model): def json(self, keys=None): j = { - 'user': self.user.username, 'id': ox.toAZ(self.id), + 'date': self.date, 'title': self.title, - 'content': self.content, + 'text': self.text, } if keys: for key in j.keys(): @@ -46,5 +41,5 @@ class News(models.Model): return j def __unicode__(self): - return u"%s/%s" %(self.created, self.title) + return u"%s/%s" %(self.date, self.title) diff --git a/pandora/news/views.py b/pandora/news/views.py index 7a2e2f79..17a78f87 100644 --- a/pandora/news/views.py +++ b/pandora/news/views.py @@ -18,101 +18,31 @@ from item.models import Item import models - -def parse_query(data, user): - query = {} - query['range'] = [0, 100] - query['sort'] = [{'key':'in', 'operator':'+'}] - for key in ('keys', 'group', 'range', 'sort', 'query'): - if key in data: - query[key] = data[key] - query['qs'] = models.News.objects.find(query, user) - if 'itemsQuery' in data: - item_query = Item.objects.find({'query': data['itemsQuery']}, user) - query['qs'] = query['qs'].filter(item__in=item_query) - return query - -def news_sort_key(key): - return { - 'text': 'value', - 'position': 'start', - }.get(key, key) - -def order_query(qs, sort): - order_by = [] - print sort - for e in sort: - operator = e['operator'] - if operator != '-': - operator = '' - key = { - 'duration': 'clip__duration', - 'in': 'start', - 'lightness': 'clip__lightness', - 'out': 'end', - 'saturation': 'clip__saturation', - 'volume': 'clip__volume', - }.get(e['key'], e['key']) - if key.startswith('clip:'): - key = news_sort_key(e['key'][len('clip:'):]) - elif key not in ('start', 'end', 'value') and not key.startswith('clip__'): - #key mgith need to be changed, see order_sort in item/views.py - key = "item__sort__%s" % key - order = '%s%s' % (operator, key) - order_by.append(order) - if order_by: - qs = qs.order_by(*order_by, nulls_last=True) - return qs - -def findNews(request): +def getNews(request): ''' param data { - query: ... - itemsQuery: ... } return { 'status': {'code': int, 'text': string} 'data': { - newss = [{..}, {...}, ...] + items = [{..}, {...}, ...] } } ''' data = json.loads(request.POST['data']) response = json_response() - - query = parse_query(data, request.user) - qs = order_query(query['qs'], query['sort']) - if 'keys' in data: - qs = qs[query['range'][0]:query['range'][1]] - response['data']['items'] = [p.json(keys=data['keys']) for p in qs] - elif 'position' in query: - ids = [ox.toAZ(i.id) for i in qs] - data['conditions'] = data['conditions'] + { - 'value': data['position'], - 'key': query['sort'][0]['key'], - 'operator': '^' - } - query = parse_query(data, request.user) - qs = order_query(query['qs'], query['sort']) - if qs.count() > 0: - response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0] - elif 'positions' in data: - ids = [ox.toAZ(i.id) for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) - else: - response['data']['items'] = qs.count() - return render_to_json_response(response) -actions.register(findNews) - + qs = models.News.objects.all().order_by('-date') + response['data']['items'] = [p.json() for p in qs] +actions.register(getNews) @login_required_json def addNews(request): ''' param data { title: string, - content: text, - public: boolean + date: string, + text: text, } return {'status': {'code': int, 'text': string}, 'data': { @@ -123,11 +53,10 @@ def addNews(request): ''' data = json.loads(request.POST['data']) - news = models.News( - user=request.user, - title=data['title'], - content=data['content'], - public=data['content']) + news = models.News() + for key in ('title', 'text', 'date'): + if key in data: + setattr(news, key, data[key]) news.save() response = json_response(news.json()) return render_to_json_response(response) @@ -160,15 +89,14 @@ def removeNews(request): return render_to_json_response(response) actions.register(removeNews, cache=False) - @login_required_json def editNews(request): ''' param data { id:, title: - content: - public: + text: + date: } return {'status': {'code': int, 'text': string}, 'data': { @@ -181,7 +109,7 @@ def editNews(request): data = json.loads(request.POST['data']) n = get_object_or_404_json(models.News, id=ox.fromAZ(data['id'])) if n.editable(request.user): - for key in ('title', 'content', 'public'): + for key in ('title', 'text', 'date'): if key in data: setattr(n, key, data[key]) n.save()