forked from 0x2620/pandora
news
This commit is contained in:
parent
9f76496c67
commit
2e507e9e3e
3 changed files with 21 additions and 98 deletions
|
@ -31,7 +31,7 @@
|
||||||
"canEditPlaces": {"staff": true, "admin": true},
|
"canEditPlaces": {"staff": true, "admin": true},
|
||||||
"canEditSitePages": {"staff": true, "admin": true},
|
"canEditSitePages": {"staff": true, "admin": true},
|
||||||
"canEditUsers": {"admin": true},
|
"canEditUsers": {"admin": true},
|
||||||
"canImportAnnotations": {"staff": true, "admin": true},
|
"canImportAnnotations": {},
|
||||||
"canPlayClips": {"guest": 2, "member": 2, "friend": 4, "staff": 4, "admin": 4},
|
"canPlayClips": {"guest": 2, "member": 2, "friend": 4, "staff": 4, "admin": 4},
|
||||||
"canPlayVideo": {"guest": 1, "member": 1, "friend": 4, "staff": 4, "admin": 4},
|
"canPlayVideo": {"guest": 1, "member": 1, "friend": 4, "staff": 4, "admin": 4},
|
||||||
"canSeeDebugMenu": {"staff": true, "admin": true},
|
"canSeeDebugMenu": {"staff": true, "admin": true},
|
||||||
|
|
|
@ -14,18 +14,13 @@ class News(models.Model):
|
||||||
|
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
modified = models.DateTimeField(auto_now=True)
|
modified = models.DateTimeField(auto_now=True)
|
||||||
user = models.ForeignKey(User, related_name='news')
|
|
||||||
|
|
||||||
title = models.TextField()
|
title = models.TextField()
|
||||||
content = models.TextField()
|
date = models.TextField()
|
||||||
|
text = models.TextField()
|
||||||
|
|
||||||
def editable(self, user):
|
def editable(self, user):
|
||||||
if user.is_authenticated():
|
return user.is_authenticated() and user.get_profile().capability("canEditSitePages")
|
||||||
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
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
@ -34,10 +29,10 @@ class News(models.Model):
|
||||||
|
|
||||||
def json(self, keys=None):
|
def json(self, keys=None):
|
||||||
j = {
|
j = {
|
||||||
'user': self.user.username,
|
|
||||||
'id': ox.toAZ(self.id),
|
'id': ox.toAZ(self.id),
|
||||||
|
'date': self.date,
|
||||||
'title': self.title,
|
'title': self.title,
|
||||||
'content': self.content,
|
'text': self.text,
|
||||||
}
|
}
|
||||||
if keys:
|
if keys:
|
||||||
for key in j.keys():
|
for key in j.keys():
|
||||||
|
@ -46,5 +41,5 @@ class News(models.Model):
|
||||||
return j
|
return j
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u"%s/%s" %(self.created, self.title)
|
return u"%s/%s" %(self.date, self.title)
|
||||||
|
|
||||||
|
|
|
@ -18,101 +18,31 @@ from item.models import Item
|
||||||
|
|
||||||
import models
|
import models
|
||||||
|
|
||||||
|
def getNews(request):
|
||||||
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):
|
|
||||||
'''
|
'''
|
||||||
param data {
|
param data {
|
||||||
query: ...
|
|
||||||
itemsQuery: ...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'status': {'code': int, 'text': string}
|
'status': {'code': int, 'text': string}
|
||||||
'data': {
|
'data': {
|
||||||
newss = [{..}, {...}, ...]
|
items = [{..}, {...}, ...]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
response = json_response()
|
response = json_response()
|
||||||
|
qs = models.News.objects.all().order_by('-date')
|
||||||
query = parse_query(data, request.user)
|
response['data']['items'] = [p.json() for p in qs]
|
||||||
qs = order_query(query['qs'], query['sort'])
|
actions.register(getNews)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
@login_required_json
|
@login_required_json
|
||||||
def addNews(request):
|
def addNews(request):
|
||||||
'''
|
'''
|
||||||
param data {
|
param data {
|
||||||
title: string,
|
title: string,
|
||||||
content: text,
|
date: string,
|
||||||
public: boolean
|
text: text,
|
||||||
}
|
}
|
||||||
return {'status': {'code': int, 'text': string},
|
return {'status': {'code': int, 'text': string},
|
||||||
'data': {
|
'data': {
|
||||||
|
@ -123,11 +53,10 @@ def addNews(request):
|
||||||
'''
|
'''
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
|
|
||||||
news = models.News(
|
news = models.News()
|
||||||
user=request.user,
|
for key in ('title', 'text', 'date'):
|
||||||
title=data['title'],
|
if key in data:
|
||||||
content=data['content'],
|
setattr(news, key, data[key])
|
||||||
public=data['content'])
|
|
||||||
news.save()
|
news.save()
|
||||||
response = json_response(news.json())
|
response = json_response(news.json())
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
|
@ -160,15 +89,14 @@ def removeNews(request):
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
actions.register(removeNews, cache=False)
|
actions.register(removeNews, cache=False)
|
||||||
|
|
||||||
|
|
||||||
@login_required_json
|
@login_required_json
|
||||||
def editNews(request):
|
def editNews(request):
|
||||||
'''
|
'''
|
||||||
param data {
|
param data {
|
||||||
id:,
|
id:,
|
||||||
title:
|
title:
|
||||||
content:
|
text:
|
||||||
public:
|
date:
|
||||||
}
|
}
|
||||||
return {'status': {'code': int, 'text': string},
|
return {'status': {'code': int, 'text': string},
|
||||||
'data': {
|
'data': {
|
||||||
|
@ -181,7 +109,7 @@ def editNews(request):
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
n = get_object_or_404_json(models.News, id=ox.fromAZ(data['id']))
|
n = get_object_or_404_json(models.News, id=ox.fromAZ(data['id']))
|
||||||
if n.editable(request.user):
|
if n.editable(request.user):
|
||||||
for key in ('title', 'content', 'public'):
|
for key in ('title', 'text', 'date'):
|
||||||
if key in data:
|
if key in data:
|
||||||
setattr(n, key, data[key])
|
setattr(n, key, data[key])
|
||||||
n.save()
|
n.save()
|
||||||
|
|
Loading…
Reference in a new issue