cablegates/pandora/itemlist/views.py

316 lines
9 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
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
2010-11-26 15:42:54 +00:00
2010-12-28 10:16:16 +00:00
2011-01-11 16:19:27 +00:00
def get_list_or_404_json(id):
username, listname = id.split(':')
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 = ''
key = e['key']
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-11 16:19:27 +00:00
#query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
query['sort'] = [{'key':'position__section', 'operator':'+'}, {'key':'position__position', 'operator':'+'}]
2011-01-11 10:18:18 +00:00
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'):
if key in data:
query[key] = data[key]
query['qs'] = models.List.objects.find(data, user)
return query
2011-01-11 11:51:22 +00:00
def findLists(request):
2011-01-11 10:18:18 +00:00
'''
FIXME: support key: subscribed
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
qs = _order_query(query['qs'], query['sort'])
#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]
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
def addListItem(request):
2010-11-26 15:42:54 +00:00
'''
param data
{list: listId,
item: itemId,
quert: ...
}
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'])
list = get_object_or_404_json(models.List, pk=data['list'])
if 'item' in data:
item = get_object_or_404_json(models.Item, pk=data['item'])
if list.editable(request.user):
list.add(item)
2010-12-28 10:16:16 +00:00
response = json_response(status=200, text='item 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')
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)
actions.register(addListItem)
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 removeListItem(request):
2010-11-26 15:42:54 +00:00
'''
param data
{list: listId,
item: itemId,
quert: ...
}
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'])
list = get_object_or_404_json(models.List, pk=data['list'])
if 'item' in data:
item = get_object_or_404_json(models.Item, pk=data['item'])
if list.editable(request.user):
list.remove(item)
response = json_response(status=200, text='item removed')
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)
actions.register(removeListItem)
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
'''
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-11 14:56:08 +00:00
if models.List.objects.filter(name=data['name'], user=request.user).count() == 0:
2010-11-26 15:42:54 +00:00
list = models.List(name = data['name'], user=request.user)
list.save()
response = json_response(status=200, text='created')
2011-01-11 10:18:18 +00:00
response['data']['list'] = list.json()
2010-11-26 15:42:54 +00:00
else:
2010-12-28 10:16:16 +00:00
response = json_response(status=200, text='list already exists')
2011-01-01 11:44:42 +00:00
response['data']['errors'] = {'name': 'List already exists'}
2010-11-26 15:42:54 +00:00
return render_to_json_response(response)
actions.register(addList)
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 {
key: value
}
keys: name, public, query, featured (if admin)
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-11 11:51:22 +00:00
if key in ('name', 'status', 'query'):
2011-01-06 07:07:25 +00:00
if key in data:
2011-01-11 11:51:22 +00:00
if key == 'query' and not data['query']:
setattr(list, key, {"static":True})
elif key == 'status':
value = data[key]
if value not in list._status:
value = list._status[0]
setattr(list, key, value)
else:
setattr(list, key, data[key])
2011-01-11 14:56:08 +00:00
if request.user.is_staff and 'featured' in data:
2011-01-06 07:07:25 +00:00
list.featured = data['featured']
2010-11-26 15:42:54 +00:00
else:
response = json_response(status=403, text='not allowed')
return render_to_json_response(response)
actions.register(editList)
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 {
name: value,
user: username(only admins)
}
return {
status: {'code': int, 'text': string},
data: {
}
}
2010-11-26 15:42:54 +00:00
'''
data = json.loads(request.POST['data'])
2011-01-11 10:18:18 +00:00
user = request.user.username
2011-01-11 14:56:08 +00:00
if user.is_staff and 'user' in data:
2011-01-11 10:18:18 +00:00
user = data.get('user')
list = get_object_or_404_json(models.List, name=data['name'], user__username=user)
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)
actions.register(removeList)
2011-01-11 14:56:08 +00:00
@login_required_json
def subscribeToList(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
if list.subscribed_users.filter(username=user.username).count() == 0:
list.subscribed_users.add(user)
response = json_response()
return render_to_json_response(response)
actions.register(subscribeToList)
@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)
response = json_response()
return render_to_json_response(response)
actions.register(unsubscribeFromList)
2011-01-11 16:19:27 +00:00
@login_required_json
def sortLists(request):
'''
param data {
section: 'my',
ids: [1,2,4,3]
}
return {
status: {'code': int, 'text': string},
data: {
}
}
'''
data = json.loads(request.POST['data'])
position = 0
section = data['section']
#FIXME: featured list needs fixing here
user = request.user
for i in data['ids']:
list = get_list_or_404_json(i)
pos, created = models.Position.objects.get_or_create(list=list, user=request.user, section=data['section'])
pos.position = position
pos.save()
position += 1
response = json_response()
return render_to_json_response(response)
actions.register(sortLists)