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
|
2010-12-22 07:45:37 +00:00
|
|
|
from api.actions import actions
|
2010-11-26 15:42:54 +00:00
|
|
|
|
2010-12-28 10:16:16 +00:00
|
|
|
|
2010-11-26 15:42:54 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def addListItem(request):
|
2010-11-26 15:42:54 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{list: listId,
|
|
|
|
item: itemId,
|
|
|
|
quert: ...
|
|
|
|
}
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {}}
|
|
|
|
'''
|
|
|
|
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)
|
2010-12-22 07:45:37 +00:00
|
|
|
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
|
2010-12-22 07:45:37 +00:00
|
|
|
def removeListItem(request):
|
2010-11-26 15:42:54 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{list: listId,
|
|
|
|
item: itemId,
|
|
|
|
quert: ...
|
|
|
|
}
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {}}
|
|
|
|
'''
|
|
|
|
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)
|
2010-12-22 07:45:37 +00:00
|
|
|
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
|
2010-12-22 07:45:37 +00:00
|
|
|
def addList(request):
|
2010-11-26 15:42:54 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{name: value}
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {}}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
if models.List.filter(name=data['name'], user=request.user).count() == 0:
|
|
|
|
list = models.List(name = data['name'], user=request.user)
|
|
|
|
list.save()
|
|
|
|
response = json_response(status=200, text='created')
|
|
|
|
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)
|
2010-12-22 07:45:37 +00:00
|
|
|
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
|
2010-12-22 07:45:37 +00:00
|
|
|
def editList(request):
|
2010-11-26 15:42:54 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{key: value}
|
|
|
|
keys: name, public
|
|
|
|
return {'status': {'code': int, 'text': string},
|
2010-12-28 10:16:16 +00:00
|
|
|
'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 list.editable(request.user):
|
|
|
|
for key in data:
|
2011-01-06 07:04:10 +00:00
|
|
|
if key in ('name', 'public', 'query'):
|
2011-01-06 07:07:25 +00:00
|
|
|
if key in data:
|
|
|
|
setattr(list, key, data[key])
|
|
|
|
if user.has_perm('Ox.admin') and 'featured' in data:
|
|
|
|
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)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(editList)
|
2010-11-26 15:42:54 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def removeList(request):
|
2010-11-26 15:42:54 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{key: value}
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {}}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
list = get_object_or_404_json(models.List, pk=data['list'])
|
|
|
|
if list.editable(request.user):
|
|
|
|
list.delete()
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='not allowed')
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(removeList)
|