cablegates/pandora/itemlist/views.py

454 lines
15 KiB
Python
Raw Normal View History

2010-11-26 15:42:54 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
2011-01-13 19:40:50 +00:00
from django.db.models import Max
2010-11-26 15:42:54 +00:00
from ox.utils import json
from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
2011-01-01 11:44:42 +00:00
import models
from api.actions import actions
2011-01-13 19:40:50 +00:00
from item import utils
2011-01-14 14:09:21 +00:00
from item.models import Item
2010-12-28 10:16:16 +00:00
2011-01-11 16:19:27 +00:00
def get_list_or_404_json(id):
2011-01-16 00:57:44 +00:00
username, listname = id.split('/')
2011-01-11 16:19:27 +00:00
return get_object_or_404_json(models.List, user__username=username, name=listname)
2011-01-11 10:18:18 +00:00
def _order_query(qs, sort):
order_by = []
for e in sort:
operator = e['operator']
if operator != '-':
operator = ''
2011-01-14 06:24:40 +00:00
key = {
'subscribed': 'subscribed_users'
}.get(e['key'], e['key'])
2011-01-11 10:18:18 +00:00
order = '%s%s' % (operator, key)
order_by.append(order)
if order_by:
qs = qs.order_by(*order_by)
return qs
def _parse_query(data, user):
query = {}
query['range'] = [0, 100]
2011-01-13 11:54:52 +00:00
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
for key in ('keys', 'group', 'list', 'range', 'ids', 'sort'):
2011-01-11 10:18:18 +00:00
if key in data:
query[key] = data[key]
query['qs'] = models.List.objects.find(data, user)
return query
2011-01-13 19:40:50 +00:00
2011-01-11 11:51:22 +00:00
def findLists(request):
2011-01-11 10:18:18 +00:00
'''
param data {
query: {
conditions: [
{
key: 'user',
value: 'something',
operator: '='
}
]
operator: ","
},
sort: [{key: 'name', operator: '+'}],
range: [0, 100]
2011-01-11 10:35:39 +00:00
keys: []
2011-01-11 10:18:18 +00:00
}
possible query keys:
name, user, featured, subscribed
2011-01-11 10:35:39 +00:00
possible keys:
name, user, featured, subscribed, query
2011-01-11 10:18:18 +00:00
}
return {status: {code: int, text: string},
data: {
lists: [
{name:, user:, featured:, public...}
]
}
}
'''
data = json.loads(request.POST['data'])
query = _parse_query(data, request.user)
#order
2011-01-13 11:54:52 +00:00
is_section_request = query['sort'] == [{u'operator': u'+', u'key': u'position'}]
2011-01-13 19:40:50 +00:00
def is_featured_condition(x):
return x['key'] == 'status' and \
x['value'] == 'featured' and \
x['operator'] == '='
is_featured = len(filter(is_featured_condition, data['query'].get('conditions', []))) > 0
2011-01-13 11:54:52 +00:00
if is_section_request:
qs = query['qs']
2011-01-13 20:11:29 +00:00
if not is_featured and not request.user.is_anonymous():
qs = qs.filter(position__in=models.Position.objects.filter(user=request.user))
2011-01-13 11:54:52 +00:00
qs = qs.order_by('position__position')
else:
qs = _order_query(query['qs'], query['sort'])
2011-01-13 01:58:11 +00:00
2011-01-11 10:18:18 +00:00
#range
response = json_response()
2011-01-11 10:35:39 +00:00
if 'keys' in data:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [l.json(data['keys'], request.user) for l in qs]
2011-01-13 19:40:50 +00:00
elif 'ids' in data:
ids = [i.get_id() for i in qs]
response['data']['positions'] = utils.get_positions(ids, query['ids'])
2011-01-11 10:35:39 +00:00
else:
response['data']['items'] = qs.count()
2011-01-11 10:18:18 +00:00
return render_to_json_response(response)
2011-01-11 11:51:22 +00:00
actions.register(findLists)
2011-01-11 10:18:18 +00:00
2010-11-26 15:42:54 +00:00
@login_required_json
2011-01-15 06:09:31 +00:00
def addListItems(request):
2010-11-26 15:42:54 +00:00
'''
2011-01-14 14:09:21 +00:00
param data {
list: listId,
2011-01-15 06:09:31 +00:00
items: [itemId],
2011-01-14 14:09:21 +00:00
query: ...
}
2011-01-11 10:18:18 +00:00
return {
status: {'code': int, 'text': string},
data: {
}
}
2010-11-26 15:42:54 +00:00
'''
data = json.loads(request.POST['data'])
2011-01-11 20:12:35 +00:00
list = get_list_or_404_json(data['list'])
2011-01-15 06:09:31 +00:00
if 'items' in data:
2010-11-26 15:42:54 +00:00
if list.editable(request.user):
2011-01-15 06:09:31 +00:00
for item in Item.objects.filter(itemId__in=data['items']):
list.add(item)
response = json_response(status=200, text='items added')
2010-11-26 15:42:54 +00:00
else:
response = json_response(status=403, text='not allowed')
elif 'query' in data:
response = json_response(status=501, text='not implemented')
else:
response = json_response(status=501, text='not implemented')
return render_to_json_response(response)
2011-01-15 06:09:31 +00:00
actions.register(addListItems, cache=False)
2010-11-26 15:42:54 +00:00
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
@login_required_json
2011-01-15 06:09:31 +00:00
def removeListItems(request):
2010-11-26 15:42:54 +00:00
'''
2011-01-14 14:09:21 +00:00
param data {
list: listId,
2011-01-15 06:09:31 +00:00
items: [itemId],
2010-11-26 15:42:54 +00:00
quert: ...
2011-01-14 14:09:21 +00:00
}
2011-01-11 10:18:18 +00:00
return {
status: {'code': int, 'text': string},
data: {
}
}
2010-11-26 15:42:54 +00:00
'''
data = json.loads(request.POST['data'])
2011-01-11 20:12:35 +00:00
list = get_list_or_404_json(data['list'])
2011-01-15 06:09:31 +00:00
if 'items' in data:
2010-11-26 15:42:54 +00:00
if list.editable(request.user):
2011-01-15 06:09:31 +00:00
for item in list.items.filter(itemId__in=data['items']):
list.remove(item)
response = json_response(status=200, text='items removed')
2010-11-26 15:42:54 +00:00
else:
response = json_response(status=403, text='not allowed')
elif 'query' in data:
response = json_response(status=501, text='not implemented')
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
else:
response = json_response(status=501, text='not implemented')
return render_to_json_response(response)
2011-01-15 06:09:31 +00:00
actions.register(removeListItems, cache=False)
2010-11-26 15:42:54 +00:00
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
@login_required_json
def addList(request):
2010-11-26 15:42:54 +00:00
'''
2011-01-11 20:12:35 +00:00
param data {
name: value,
}
2011-01-11 10:18:18 +00:00
return {
status: {'code': int, 'text': string},
data: {
list:
}
}
2010-11-26 15:42:54 +00:00
'''
data = json.loads(request.POST['data'])
2011-01-14 06:24:40 +00:00
name = data['name'].strip()
if not name:
name = "Untitled"
2011-01-13 15:32:14 +00:00
num = 1
2011-01-14 06:24:40 +00:00
created = False
while not created:
list, created = models.List.objects.get_or_create(name=name, user=request.user)
2011-01-13 15:32:14 +00:00
num += 1
name = data['name'] + ' (%d)' % num
2011-01-14 06:24:40 +00:00
2011-01-13 19:40:50 +00:00
for key in data:
if key == 'query' and not data['query']:
setattr(list, key, {"static":True})
elif key == 'query':
setattr(list, key, data[key])
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':
value = data[key]
if value not in list._status:
value = list._status[0]
2011-01-14 06:24:40 +00:00
if not request.user.is_staff and value == 'featured':
2011-01-14 04:16:41 +00:00
value = 'private'
2011-01-13 19:40:50 +00:00
setattr(list, key, value)
list.save()
2011-01-14 06:24:40 +00:00
if list.status == 'featured':
pos, created = models.Position.objects.get_or_create(list=list,
user=request.user, section='featured')
qs = models.Position.objects.filter(section='featured')
else:
pos, created = models.Position.objects.get_or_create(list=list,
user=request.user, section='my')
qs = models.Position.objects.filter(user=request.user, section='my')
2011-01-13 19:40:50 +00:00
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
2011-01-13 15:32:14 +00:00
pos.save()
response = json_response(status=200, text='created')
response['data'] = list.json()
2010-11-26 15:42:54 +00:00
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(addList, cache=False)
2010-11-26 15:42:54 +00:00
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
@login_required_json
def editList(request):
2010-11-26 15:42:54 +00:00
'''
2011-01-11 10:18:18 +00:00
param data {
2011-01-13 01:58:11 +00:00
id: listId,
key: value,
2011-01-11 10:18:18 +00:00
}
2011-01-13 01:58:11 +00:00
keys: name, status, query, position
if you change status you have to provide position of list
2011-01-11 10:18:18 +00:00
return {
status: {'code': int, 'text': string},
data: {
}
2010-12-28 10:16:16 +00:00
}
2010-11-26 15:42:54 +00:00
'''
data = json.loads(request.POST['data'])
2011-01-11 16:19:27 +00:00
list = get_list_or_404_json(data['id'])
2010-11-26 15:42:54 +00:00
if list.editable(request.user):
2011-01-11 14:56:08 +00:00
response = json_response()
2010-11-26 15:42:54 +00:00
for key in data:
2011-01-13 19:40:50 +00:00
if key == 'query' and not data['query']:
setattr(list, key, {"static":True})
elif key == 'query':
setattr(list, key, data[key])
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':
value = data[key]
if value not in list._status:
value = list._status[0]
if value == 'private':
for user in list.subscribed_users.all():
list.subscribed_users.remove(user)
2011-01-14 04:16:41 +00:00
qs = models.Position.objects.filter(user=request.user, section='section', list=list)
if qs.count() > 1:
pos = qs[0]
pos.section = 'my'
pos.save()
elif value == 'featured':
if not request.user.is_staff:
value = list.status
else:
pos, created = models.Position.objects.get_or_create(list=list, user=request.user,
section='featured')
if created:
qs = models.Position.objects.filter(user=request.user, section='featured')
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
pos.save()
2011-01-14 10:35:53 +00:00
models.Position.objects.filter(list=list).exclude(id=pos.id).delete()
else:
models.Position.objects.filter(list=list).delete()
pos, created = models.Position.objects.get_or_create(list=list, user=list.user,
section='my')
qs = models.Position.objects.filter(user=list.user, section='my')
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
pos.save()
for u in list.subscribed_users.all():
pos, created = models.Position.objects.get_or_create(list=list, user=u,
section='public')
qs = models.Position.objects.filter(user=u, section='public')
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
pos.save()
2011-01-14 04:16:41 +00:00
list.status = value
2011-01-13 19:40:50 +00:00
elif key == 'name':
name = data['name'].strip()
if not name:
name = "Untitled"
num = 1
while models.List.objects.filter(name=name, user=list.user).exclude(id=list.id).count()>0:
num += 1
name = data['name'] + ' (%d)' % num
2011-01-14 04:16:41 +00:00
list.name = name
2011-01-11 20:12:35 +00:00
if 'position' in data:
pos, created = models.Position.objects.get_or_create(list=list, user=request.user)
pos.position = data['position']
pos.section = 'featured'
if list.status == 'private':
pos.section = 'my'
pos.save()
list.save()
2011-01-13 01:58:11 +00:00
response['data'] = list.json(user=request.user)
2010-11-26 15:42:54 +00:00
else:
response = json_response(status=403, text='not allowed')
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(editList, cache=False)
2010-11-26 15:42:54 +00:00
2011-01-01 11:44:42 +00:00
@login_required_json
def removeList(request):
2010-11-26 15:42:54 +00:00
'''
2011-01-11 10:18:18 +00:00
param data {
2011-01-13 15:32:14 +00:00
id: listId,
2011-01-11 10:18:18 +00:00
}
return {
status: {'code': int, 'text': string},
data: {
}
}
2010-11-26 15:42:54 +00:00
'''
data = json.loads(request.POST['data'])
2011-01-13 15:32:14 +00:00
list = get_list_or_404_json(data['id'])
response = json_response()
2010-11-26 15:42:54 +00:00
if list.editable(request.user):
list.delete()
else:
response = json_response(status=403, text='not allowed')
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(removeList, cache=False)
2011-01-11 14:56:08 +00:00
@login_required_json
def subscribeToList(request):
'''
param data {
id: listId,
}
return {
status: {'code': int, 'text': string},
data: {
}
}
'''
data = json.loads(request.POST['data'])
2011-01-11 16:19:27 +00:00
list = get_list_or_404_json(data['id'])
2011-01-11 14:56:08 +00:00
user = request.user
2011-01-14 09:54:35 +00:00
if list.status == 'public' and \
list.subscribed_users.filter(username=user.username).count() == 0:
2011-01-11 14:56:08 +00:00
list.subscribed_users.add(user)
2011-01-14 09:54:35 +00:00
pos, created = models.Position.objects.get_or_create(list=list, user=user, section='public')
2011-01-14 06:24:40 +00:00
if created:
2011-01-14 09:54:35 +00:00
qs = models.Position.objects.filter(user=user, section='public')
2011-01-14 06:24:40 +00:00
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
pos.save()
2011-01-11 14:56:08 +00:00
response = json_response()
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(subscribeToList, cache=False)
2011-01-11 14:56:08 +00:00
2011-01-14 09:54:35 +00:00
2011-01-11 14:56:08 +00:00
@login_required_json
def unsubscribeFromList(request):
'''
param data {
id: listId,
user: username(only admins)
}
return {
status: {'code': int, 'text': string},
data: {
}
}
'''
data = json.loads(request.POST['data'])
2011-01-11 16:19:27 +00:00
list = get_list_or_404_json(data['id'])
2011-01-11 14:56:08 +00:00
user = request.user
list.subscribed_users.remove(user)
2011-01-14 09:54:35 +00:00
models.Position.objects.filter(list=list, user=user, section='public').delete()
2011-01-11 14:56:08 +00:00
response = json_response()
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(unsubscribeFromList, cache=False)
2011-01-11 16:19:27 +00:00
@login_required_json
def sortLists(request):
'''
param data {
section: 'my',
ids: [1,2,4,3]
}
2011-01-14 06:24:40 +00:00
known sections: 'my', 'public', 'featured'
featured can only be edited by admins
2011-01-11 16:19:27 +00:00
return {
status: {'code': int, 'text': string},
data: {
}
}
'''
data = json.loads(request.POST['data'])
position = 0
section = data['section']
2011-01-11 20:12:35 +00:00
if section == 'featured' and not request.user.is_staff:
response = json_response(status=403, text='not allowed')
else:
user = request.user
if section == 'featured':
for i in data['ids']:
list = get_list_or_404_json(i)
qs = models.Position.objects.filter(section=section, list=list)
if qs.count() > 0:
pos = qs[0]
else:
pos = models.Position(list=list, user=user, section=section)
2011-01-14 09:54:35 +00:00
if pos.position != position:
pos.position = position
pos.save()
2011-01-11 20:12:35 +00:00
position += 1
models.Position.objects.filter(section=section, list=list).exclude(id=pos.id).delete()
else:
for i in data['ids']:
list = get_list_or_404_json(i)
pos, created = models.Position.objects.get_or_create(list=list,
2011-01-14 06:24:40 +00:00
user=request.user, section=section)
2011-01-14 09:54:35 +00:00
if pos.position != position:
pos.position = position
pos.save()
2011-01-11 20:12:35 +00:00
position += 1
response = json_response()
2011-01-11 16:19:27 +00:00
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(sortLists, cache=False)