pandora/pandora/itemlist/views.py

443 lines
14 KiB
Python
Raw Normal View History

2010-11-26 15:42:54 +00:00
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
2011-10-22 16:07:05 +00:00
import os
import re
2010-11-26 15:42:54 +00:00
2011-09-29 13:05:34 +00:00
from django.db.models import Max, Sum
2011-12-19 17:20:06 +00:00
from django.db import transaction
2011-10-22 16:07:05 +00:00
from django.conf import settings
2010-11-26 15:42:54 +00:00
from ox.utils import json
2016-02-20 09:06:41 +00:00
from oxdjango.decorators import login_required_json
from oxdjango.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from oxdjango.http import HttpFileResponse
2010-11-26 15:42:54 +00:00
2011-10-22 16:07:05 +00:00
from . import models
2016-02-20 09:06:41 +00:00
from oxdjango.api 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
from user.tasks import update_numberoflists
from changelog.models import add_changelog
2010-12-28 10:16:16 +00:00
2011-01-11 16:19:27 +00:00
def get_list_or_404_json(id):
2011-09-28 17:32:03 +00:00
id = id.split(':')
username = id[0]
listname = ":".join(id[1:])
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 = {
2011-09-29 13:05:34 +00:00
'subscribed': 'subscribed_users',
'items': 'numberofitems'
2011-01-14 06:24:40 +00:00
}.get(e['key'], e['key'])
2011-01-11 10:18:18 +00:00
order = '%s%s' % (operator, key)
order_by.append(order)
2011-09-29 13:05:34 +00:00
if key == 'subscribers':
qs = qs.annotate(subscribers=Sum('subscribed_users'))
2011-01-11 10:18:18 +00:00
if order_by:
2014-01-07 11:05:42 +00:00
qs = qs.order_by(*order_by, nulls_last=True)
2011-10-03 16:09:11 +00:00
qs = qs.distinct()
2011-01-11 10:18:18 +00:00
return qs
2011-02-24 18:39:58 +00:00
def parse_query(data, user):
2011-01-11 10:18:18 +00:00
query = {}
query['range'] = [0, 100]
2011-01-13 11:54:52 +00:00
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
2011-06-01 11:03:43 +00:00
for key in ('keys', 'group', 'list', 'range', 'position', 'positions', '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
2014-10-06 08:26:43 +00:00
def findLists(request, data):
2011-01-11 10:18:18 +00:00
'''
2014-12-19 15:05:08 +00:00
Finds lists for a given query
2014-12-18 19:26:37 +00:00
takes {
query: object, // query object, see `find`
sort: [], // list of sort objects, see `find`
2014-12-19 15:05:08 +00:00
range: [int, int], // range of results to return
keys: [string] // list of properties to return
2014-12-18 19:26:37 +00:00
}
returns {
2014-12-19 15:05:08 +00:00
items: [object] // list of list objects
2014-12-18 19:26:37 +00:00
}
2014-12-19 15:05:08 +00:00
notes: Possible query keys are 'featured', 'name', 'subscribed' and 'user',
possible keys are 'featured', 'name', 'query', 'subscribed' and 'user'.
see: addList, editList, find, getList, removeList, sortLists
2011-01-11 10:18:18 +00:00
'''
2011-02-24 18:39:58 +00:00
query = parse_query(data, request.user)
2011-01-11 10:18:18 +00:00
#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 \
2012-02-15 09:23:30 +00:00
x['operator'] in ('=', '==')
is_featured = any(
is_featured_condition(x)
for x in data.get('query', {}).get('conditions', [])
)
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
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-06-01 11:03:43 +00:00
elif 'position' in data:
#FIXME: actually implement position requests
response['data']['position'] = 0
elif 'positions' in data:
2011-01-13 19:40:50 +00:00
ids = [i.get_id() for i in qs]
2011-06-01 11:03:43 +00:00
response['data']['positions'] = utils.get_positions(ids, query['positions'])
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
2014-10-06 08:26:43 +00:00
def getList(request, data):
2013-08-26 17:05:49 +00:00
'''
2014-12-18 19:26:37 +00:00
Gets a list by id
takes {
2014-12-19 15:31:57 +00:00
id: string // list id
2014-12-18 19:26:37 +00:00
}
returns {
2014-12-19 15:31:57 +00:00
id: string, // list id
section: string, // lists section (like 'personal')
... // more key/value pairs
2014-12-18 19:26:37 +00:00
}
see: addList, editList, findLists, removeList, sortLists
2013-08-26 17:05:49 +00:00
'''
if 'id' in data:
response = json_response()
list = get_list_or_404_json(data['id'])
if list.accessible(request.user):
response['data'] = list.json(user=request.user)
else:
response = json_response(status=403, text='not allowed')
else:
response = json_response(status=404, text='not found')
return render_to_json_response(response)
actions.register(getList)
2010-11-26 15:42:54 +00:00
@login_required_json
2014-10-06 08:26:43 +00:00
def addListItems(request, data):
2010-11-26 15:42:54 +00:00
'''
2014-12-19 12:59:10 +00:00
Adds one or more items to a static list
2014-12-18 19:26:37 +00:00
takes {
2014-12-18 20:16:41 +00:00
list: string, // list id
items: [string], // either list of item ids
query: object // or query object, see `find`
2014-12-18 19:26:37 +00:00
}
2014-12-18 20:16:41 +00:00
returns {}
see: find, orderListItems, removeListItems
2010-11-26 15:42:54 +00:00
'''
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):
2016-02-19 16:25:09 +00:00
with transaction.atomic():
2014-09-19 12:26:46 +00:00
for item in Item.objects.filter(public_id__in=data['items']):
2011-12-19 17:20:06 +00:00
list.add(item)
2011-01-15 06:09:31 +00:00
response = json_response(status=200, text='items added')
add_changelog(request, data, data['list'])
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
2014-10-06 08:26:43 +00:00
def removeListItems(request, data):
2010-11-26 15:42:54 +00:00
'''
2014-12-19 12:59:10 +00:00
Removes one or more items from a static list
2014-12-18 19:26:37 +00:00
takes {
2014-12-18 20:16:41 +00:00
list: string, // list id
items: [itemId], // either list of item ids
query: object // or query object, see `find`
2014-12-18 19:26:37 +00:00
}
2014-12-18 20:16:41 +00:00
returns {}
see: addListItems, find, orderListItems
2010-11-26 15:42:54 +00:00
'''
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-12-19 17:20:06 +00:00
list.remove(items=data['items'])
2011-01-15 06:09:31 +00:00
response = json_response(status=200, text='items removed')
add_changelog(request, data, data['list'])
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
@login_required_json
2014-10-06 08:26:43 +00:00
def orderListItems(request, data):
'''
Sets the manual ordering of items in a given list
2014-12-18 19:26:37 +00:00
takes {
2014-12-18 20:16:41 +00:00
list: string, // list id
ids: [string] // ordered list of item ids
2014-12-18 19:26:37 +00:00
}
2014-12-18 20:16:41 +00:00
returns {}
2014-12-18 19:26:37 +00:00
notes: There is no UI for this yet.
2014-12-18 20:16:41 +00:00
see: addListItems, removeListItems
'''
list = get_list_or_404_json(data['list'])
response = json_response()
if list.editable(request.user) and list.type == 'static':
index = 0
2016-02-19 16:25:09 +00:00
with transaction.atomic():
for i in data['ids']:
2014-09-19 12:26:46 +00:00
models.ListItem.objects.filter(list=list, item__public_id=i).update(index=index)
index += 1
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
actions.register(orderListItems, cache=False)
2011-01-01 11:44:42 +00:00
2010-11-26 15:42:54 +00:00
@login_required_json
2014-10-06 08:26:43 +00:00
def addList(request, data):
2010-11-26 15:42:54 +00:00
'''
2014-12-18 19:26:37 +00:00
Adds a new list
takes {
2014-12-18 20:16:41 +00:00
name: value, // list name (optional)
... // more key/value pairs
2014-12-18 19:26:37 +00:00
}
returns {
2014-12-18 20:16:41 +00:00
id: string, // list id
name: string, // list name
... // more key/value pairs
2014-12-18 19:26:37 +00:00
}
2014-12-18 20:16:41 +00:00
notes: Possible keys are 'description', 'items', 'name', 'query', 'sort',
'type' and 'view'.
2014-12-18 19:26:37 +00:00
see: editList, findLists, getList, removeList, sortLists
2010-11-26 15:42:54 +00:00
'''
2013-02-20 11:44:55 +00:00
data['name'] = re.sub(' \[\d+\]$', '', data.get('name', 'Untitled')).strip()
name = data['name']
2011-01-14 06:24:40 +00:00
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
2011-09-29 13:05:34 +00:00
name = data['name'] + ' [%d]' % num
2011-01-14 06:24:40 +00:00
2013-02-20 11:44:55 +00:00
del data['name']
if data:
list.edit(data, request.user)
else:
2013-02-20 11:44:55 +00:00
list.save()
update_numberoflists.delay(request.user.username)
2011-01-13 19:40:50 +00:00
2011-09-29 13:05:34 +00:00
if 'items' in data:
2014-09-19 12:26:46 +00:00
for item in Item.objects.filter(public_id__in=data['items']):
2011-09-29 13:05:34 +00:00
list.add(item)
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,
2011-01-21 05:13:41 +00:00
user=request.user, section='personal')
qs = models.Position.objects.filter(user=request.user, section='personal')
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')
2018-01-15 11:20:01 +00:00
response['data'] = list.json(user=request.user)
add_changelog(request, data, list.get_id())
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
2014-10-06 08:26:43 +00:00
def editList(request, data):
2010-11-26 15:42:54 +00:00
'''
2014-12-18 19:26:37 +00:00
Edits a list
takes {
2014-12-18 20:16:41 +00:00
id: string, // list id
key: value, // property id and new value
... // more key/value pairs
2014-12-18 19:26:37 +00:00
}
returns {
2014-12-18 20:16:41 +00:00
id: string, // list id
... // more key/value pairs
2014-12-18 19:26:37 +00:00
}
2014-12-18 20:16:41 +00:00
notes: Possible keys are 'name', 'position', 'posterFrames', 'query' and
'status'. 'posterFrames' is an array of {item, position}. If you change
'status', you have to pass 'position' (the position of the list in its new
list folder).
2014-12-18 19:26:37 +00:00
see: addList, findLists, getList, removeList, sortLists
2010-11-26 15:42:54 +00:00
'''
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()
2013-02-20 11:44:55 +00:00
list.edit(data, request.user)
2011-01-13 01:58:11 +00:00
response['data'] = list.json(user=request.user)
add_changelog(request, data)
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
2014-10-06 08:26:43 +00:00
def removeList(request, data):
2010-11-26 15:42:54 +00:00
'''
2014-12-18 19:26:37 +00:00
Removes a list
takes {
id: string // list id
}
returns {}
see: addList, editList, findLists, getList, sortLists
2010-11-26 15:42:54 +00:00
'''
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):
add_changelog(request, data)
2010-11-26 15:42:54 +00:00
list.delete()
update_numberoflists.delay(request.user.username)
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(removeList, cache=False)
2011-01-11 14:56:08 +00:00
@login_required_json
2014-10-06 08:26:43 +00:00
def subscribeToList(request, data):
2011-01-11 14:56:08 +00:00
'''
2014-12-18 19:26:37 +00:00
Adds a list to favorites
takes {
2014-12-18 20:16:41 +00:00
id: string, // list id
user: string // username (admin-only)
2014-12-18 19:26:37 +00:00
}
returns {}
see: unsubscribeFromList
2011-01-11 14:56:08 +00:00
'''
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()
add_changelog(request, data)
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
2014-10-06 08:26:43 +00:00
def unsubscribeFromList(request, data):
2011-01-11 14:56:08 +00:00
'''
2014-12-18 19:26:37 +00:00
Removes a list from favorites
takes {
2014-12-18 20:16:41 +00:00
id: string, // list id
user: string // username (admin-only)
2014-12-18 19:26:37 +00:00
}
returns {}
see: subscribeToList
2011-01-11 14:56:08 +00:00
'''
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()
add_changelog(request, data)
2011-01-11 14:56:08 +00:00
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
2014-10-06 08:26:43 +00:00
def sortLists(request, data):
2011-01-11 16:19:27 +00:00
'''
2014-12-19 15:31:57 +00:00
Sets the order of lists in a given section
2014-12-18 19:26:37 +00:00
takes {
2014-12-18 20:16:41 +00:00
section: string, // lists section
ids: [string] // ordered list of lists
2014-12-18 19:26:37 +00:00
}
returns {}
2014-12-18 20:16:41 +00:00
notes: Possible sections are 'personal', 'favorite' and 'featured'. Setting
the order of featured lists requires the appropriate capability.
2014-12-19 15:31:57 +00:00
see: addList, editList, findLists, getList, removeList
2011-01-11 16:19:27 +00:00
'''
position = 0
section = data['section']
section = {
'favorite': 'public'
}.get(section,section)
2011-01-21 05:13:41 +00:00
#ids = list(set(data['ids']))
ids = data['ids']
2016-02-19 16:34:15 +00:00
if section == 'featured' and not request.user.profile.capability('canEditFeaturedLists'):
2011-01-11 20:12:35 +00:00
response = json_response(status=403, text='not allowed')
else:
user = request.user
if section == 'featured':
2011-01-21 05:13:41 +00:00
for i in ids:
l = get_list_or_404_json(i)
qs = models.Position.objects.filter(section=section, list=l)
2011-01-11 20:12:35 +00:00
if qs.count() > 0:
pos = qs[0]
else:
2011-01-21 05:13:41 +00:00
pos = models.Position(list=l, 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
2011-01-21 05:13:41 +00:00
models.Position.objects.filter(section=section, list=l).exclude(id=pos.id).delete()
2011-01-11 20:12:35 +00:00
else:
2011-01-21 05:13:41 +00:00
for i in ids:
l = get_list_or_404_json(i)
pos, created = models.Position.objects.get_or_create(list=l,
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)
2011-09-29 13:05:34 +00:00
def icon(request, id, size=16):
if not size:
size = 16
2011-10-22 16:07:05 +00:00
id = id.split(':')
username = id[0]
listname = ":".join(id[1:])
qs = models.List.objects.filter(user__username=username, name=listname)
if qs.count() == 1 and qs[0].accessible(request.user):
list = qs[0]
icon = list.get_icon(int(size))
else:
2011-10-27 22:15:37 +00:00
icon = os.path.join(settings.STATIC_ROOT, 'jpg/list256.jpg')
2011-10-22 16:07:05 +00:00
return HttpFileResponse(icon, content_type='image/jpeg')