even more improvements to lists
This commit is contained in:
parent
a3b65cb046
commit
d4141a1e00
7 changed files with 154 additions and 1748 deletions
|
@ -87,12 +87,13 @@ def parseCondition(condition):
|
||||||
if len(l) == 2:
|
if len(l) == 2:
|
||||||
lqs = List.objects.filter(name=l[1], user__username=l[0])
|
lqs = List.objects.filter(name=l[1], user__username=l[0])
|
||||||
if lqs.count() == 1:
|
if lqs.count() == 1:
|
||||||
if lqs[0].query.get('static', False) == False:
|
l = lqs[0]
|
||||||
data = lqs[0].query
|
if l.query.get('static', False) == False:
|
||||||
q = parseConditions(data['conditions'],
|
data = l.query
|
||||||
|
q = parseConditions(data.get('conditions', []),
|
||||||
data.get('operator', '&'))
|
data.get('operator', '&'))
|
||||||
else:
|
else:
|
||||||
q = Q(id__in=lqs[0].items.all())
|
q = Q(id__in=l.items.all())
|
||||||
return q
|
return q
|
||||||
else: #number or date
|
else: #number or date
|
||||||
|
|
||||||
|
@ -237,7 +238,7 @@ class ItemManager(Manager):
|
||||||
qs = self.get_query_set()
|
qs = self.get_query_set()
|
||||||
#only include items that have hard metadata
|
#only include items that have hard metadata
|
||||||
qs = qs.filter(available=True)
|
qs = qs.filter(available=True)
|
||||||
conditions = parseConditions(data['query']['conditions'],
|
conditions = parseConditions(data['query'].get('conditions', []),
|
||||||
data['query'].get('operator', '&'))
|
data['query'].get('operator', '&'))
|
||||||
if conditions:
|
if conditions:
|
||||||
qs = qs.filter(conditions)
|
qs = qs.filter(conditions)
|
||||||
|
|
|
@ -206,3 +206,16 @@ def sort_title(title):
|
||||||
#pad numbered titles
|
#pad numbered titles
|
||||||
title = re.sub('(\d+)', lambda x: '%010d' % int(x.group(0)), title)
|
title = re.sub('(\d+)', lambda x: '%010d' % int(x.group(0)), title)
|
||||||
return title.strip()
|
return title.strip()
|
||||||
|
|
||||||
|
def get_positions(ids, pos):
|
||||||
|
'''
|
||||||
|
>>> get_positions([1,2,3,4], [2,4])
|
||||||
|
{2: 1, 4: 3}
|
||||||
|
'''
|
||||||
|
positions = {}
|
||||||
|
for i in pos:
|
||||||
|
try:
|
||||||
|
positions[i] = ids.index(i)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return positions
|
||||||
|
|
|
@ -70,16 +70,6 @@ def _parse_query(data, user):
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
def _get_positions(ids, get_ids):
|
|
||||||
positions = {}
|
|
||||||
for i in get_ids:
|
|
||||||
try:
|
|
||||||
positions[i] = ids.index(i)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
return positions
|
|
||||||
|
|
||||||
|
|
||||||
def find(request):
|
def find(request):
|
||||||
'''
|
'''
|
||||||
param data {
|
param data {
|
||||||
|
@ -184,7 +174,7 @@ Positions
|
||||||
#FIXME: this does not scale for larger results
|
#FIXME: this does not scale for larger results
|
||||||
response['data']['positions'] = {}
|
response['data']['positions'] = {}
|
||||||
ids = [j['value'] for j in qs]
|
ids = [j['value'] for j in qs]
|
||||||
response['data']['positions'] = _get_positions(ids, query['ids'])
|
response['data']['positions'] = utils.get_positions(ids, query['ids'])
|
||||||
|
|
||||||
elif 'range' in data:
|
elif 'range' in data:
|
||||||
qs = qs[query['range'][0]:query['range'][1]]
|
qs = qs[query['range'][0]:query['range'][1]]
|
||||||
|
|
|
@ -25,6 +25,7 @@ class List(models.Model):
|
||||||
query = DictField(default={"static": True})
|
query = DictField(default={"static": True})
|
||||||
type= models.CharField(max_length=255, default='static')
|
type= models.CharField(max_length=255, default='static')
|
||||||
|
|
||||||
|
#is through table still required?
|
||||||
items = models.ManyToManyField('item.Item', related_name='lists',
|
items = models.ManyToManyField('item.Item', related_name='lists',
|
||||||
through='ListItem')
|
through='ListItem')
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,14 @@
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
from __future__ import division
|
from __future__ import division
|
||||||
|
|
||||||
|
from django.db.models import Max
|
||||||
from ox.utils import json
|
from ox.utils import json
|
||||||
from ox.django.decorators import login_required_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
|
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
||||||
|
|
||||||
import models
|
import models
|
||||||
from api.actions import actions
|
from api.actions import actions
|
||||||
|
from item import utils
|
||||||
|
|
||||||
|
|
||||||
def get_list_or_404_json(id):
|
def get_list_or_404_json(id):
|
||||||
|
@ -31,17 +33,15 @@ def _parse_query(data, user):
|
||||||
query = {}
|
query = {}
|
||||||
query['range'] = [0, 100]
|
query['range'] = [0, 100]
|
||||||
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
|
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
|
||||||
#query['sort'] = [{'key':'position__position', 'operator':'+'}]
|
|
||||||
query['sort'] = [{'key':'name', 'operator':'+'}]
|
|
||||||
for key in ('keys', 'group', 'list', 'range', 'ids', 'sort'):
|
for key in ('keys', 'group', 'list', 'range', 'ids', 'sort'):
|
||||||
if key in data:
|
if key in data:
|
||||||
query[key] = data[key]
|
query[key] = data[key]
|
||||||
query['qs'] = models.List.objects.find(data, user)
|
query['qs'] = models.List.objects.find(data, user)
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
def findLists(request):
|
def findLists(request):
|
||||||
'''
|
'''
|
||||||
FIXME: support key: subscribed
|
|
||||||
param data {
|
param data {
|
||||||
query: {
|
query: {
|
||||||
conditions: [
|
conditions: [
|
||||||
|
@ -78,12 +78,17 @@ def findLists(request):
|
||||||
|
|
||||||
#order
|
#order
|
||||||
is_section_request = query['sort'] == [{u'operator': u'+', u'key': u'position'}]
|
is_section_request = query['sort'] == [{u'operator': u'+', u'key': u'position'}]
|
||||||
is_featured = len(filter(lambda x: x['key'] == 'status' and x['value'] == 'featured' and x['operator'] == '=', data['query'].get('conditions', []))) > 0
|
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
|
||||||
|
|
||||||
if is_section_request:
|
if is_section_request:
|
||||||
qs = query['qs']
|
qs = query['qs']
|
||||||
if not is_featured:
|
if not is_featured:
|
||||||
qs = qs.filter(position__in=models.Position.objects.filter(user=request.user))
|
if not request.user.is_anonymous():
|
||||||
|
qs = qs.filter(position__in=models.Position.objects.filter(user=request.user))
|
||||||
qs = qs.order_by('position__position')
|
qs = qs.order_by('position__position')
|
||||||
else:
|
else:
|
||||||
qs = _order_query(query['qs'], query['sort'])
|
qs = _order_query(query['qs'], query['sort'])
|
||||||
|
@ -94,6 +99,9 @@ def findLists(request):
|
||||||
qs = qs[query['range'][0]:query['range'][1]]
|
qs = qs[query['range'][0]:query['range'][1]]
|
||||||
|
|
||||||
response['data']['items'] = [l.json(data['keys'], request.user) for l in qs]
|
response['data']['items'] = [l.json(data['keys'], request.user) for l in qs]
|
||||||
|
elif 'ids' in data:
|
||||||
|
ids = [i.get_id() for i in qs]
|
||||||
|
response['data']['positions'] = utils.get_positions(ids, query['ids'])
|
||||||
else:
|
else:
|
||||||
response['data']['items'] = qs.count()
|
response['data']['items'] = qs.count()
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
|
@ -105,7 +113,7 @@ def addListItem(request):
|
||||||
param data
|
param data
|
||||||
{list: listId,
|
{list: listId,
|
||||||
item: itemId,
|
item: itemId,
|
||||||
quert: ...
|
query: ...
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
status: {'code': int, 'text': string},
|
status: {'code': int, 'text': string},
|
||||||
|
@ -124,7 +132,6 @@ def addListItem(request):
|
||||||
response = json_response(status=403, text='not allowed')
|
response = json_response(status=403, text='not allowed')
|
||||||
elif 'query' in data:
|
elif 'query' in data:
|
||||||
response = json_response(status=501, text='not implemented')
|
response = json_response(status=501, text='not implemented')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
response = json_response(status=501, text='not implemented')
|
response = json_response(status=501, text='not implemented')
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
|
@ -168,7 +175,6 @@ def addList(request):
|
||||||
'''
|
'''
|
||||||
param data {
|
param data {
|
||||||
name: value,
|
name: value,
|
||||||
position: int
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
status: {'code': int, 'text': string},
|
status: {'code': int, 'text': string},
|
||||||
|
@ -185,9 +191,31 @@ def addList(request):
|
||||||
name = data['name'] + ' (%d)' % num
|
name = data['name'] + ' (%d)' % num
|
||||||
list = models.List(name = name, user=request.user)
|
list = models.List(name = name, user=request.user)
|
||||||
list.save()
|
list.save()
|
||||||
|
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]
|
||||||
|
setattr(list, key, value)
|
||||||
|
|
||||||
|
list.save()
|
||||||
|
|
||||||
pos, created = models.Position.objects.get_or_create(list=list,
|
pos, created = models.Position.objects.get_or_create(list=list,
|
||||||
user=request.user, section='my')
|
user=request.user, section='my')
|
||||||
pos.position = data['position']
|
qs = models.Position.objects.filter(user=request.user, section='my')
|
||||||
|
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||||
pos.save()
|
pos.save()
|
||||||
response = json_response(status=200, text='created')
|
response = json_response(status=200, text='created')
|
||||||
response['data'] = list.json()
|
response['data'] = list.json()
|
||||||
|
@ -216,35 +244,35 @@ def editList(request):
|
||||||
if list.editable(request.user):
|
if list.editable(request.user):
|
||||||
response = json_response()
|
response = json_response()
|
||||||
for key in data:
|
for key in data:
|
||||||
if key in ('name', 'status', 'query', 'type'):
|
if key == 'query' and not data['query']:
|
||||||
if key in data:
|
setattr(list, key, {"static":True})
|
||||||
if key == 'query' and not data['query']:
|
elif key == 'query':
|
||||||
setattr(list, key, {"static":True})
|
setattr(list, key, data[key])
|
||||||
elif key == 'type':
|
elif key == 'type':
|
||||||
if data[key] == 'static':
|
if data[key] == 'static':
|
||||||
list.query = {"static":True}
|
list.query = {"static":True}
|
||||||
list.type = 'static'
|
list.type = 'static'
|
||||||
else:
|
else:
|
||||||
list.type = 'dynamic'
|
list.type = 'dynamic'
|
||||||
if list.query.get('static', False):
|
if list.query.get('static', False):
|
||||||
list.query = {}
|
list.query = {}
|
||||||
elif key == 'status':
|
elif key == 'status':
|
||||||
value = data[key]
|
value = data[key]
|
||||||
if value not in list._status:
|
if value not in list._status:
|
||||||
value = list._status[0]
|
value = list._status[0]
|
||||||
setattr(list, key, value)
|
setattr(list, key, value)
|
||||||
if value == 'private':
|
if value == 'private':
|
||||||
for user in list.subscribed_users.all():
|
for user in list.subscribed_users.all():
|
||||||
list.subscribed_users.remove(user)
|
list.subscribed_users.remove(user)
|
||||||
elif key == 'name':
|
elif key == 'name':
|
||||||
name = data['name']
|
name = data['name'].strip()
|
||||||
num = 1
|
if not name:
|
||||||
while models.List.objects.filter(name=name, user=list.user).exclude(id=list.id).count()>0:
|
name = "Untitled"
|
||||||
num += 1
|
num = 1
|
||||||
name = data['name'] + ' (%d)' % num
|
while models.List.objects.filter(name=name, user=list.user).exclude(id=list.id).count()>0:
|
||||||
setattr(list, key, name)
|
num += 1
|
||||||
else:
|
name = data['name'] + ' (%d)' % num
|
||||||
setattr(list, key, data[key])
|
setattr(list, key, name)
|
||||||
|
|
||||||
if 'position' in data:
|
if 'position' in data:
|
||||||
pos, created = models.Position.objects.get_or_create(list=list, user=request.user)
|
pos, created = models.Position.objects.get_or_create(list=list, user=request.user)
|
||||||
|
|
|
@ -68,14 +68,14 @@ function constructList() {
|
||||||
if(!data.keys) {
|
if(!data.keys) {
|
||||||
app.api.api(function(results) {
|
app.api.api(function(results) {
|
||||||
var items = [];
|
var items = [];
|
||||||
$.each(results.data.actions, function(i, k) {items.push({'name': k})});
|
$.each(results.data.actions, function(i, k) {items.push({'name': i})});
|
||||||
var result = {'data': {'items': items.length}};
|
var result = {'data': {'items': items.length}};
|
||||||
callback(result);
|
callback(result);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
app.api.api(function(results) {
|
app.api.api(function(results) {
|
||||||
var items = [];
|
var items = [];
|
||||||
$.each(results.data.actions, function(i, k) {items.push({'name': k})});
|
$.each(results.data.actions, function(i, k) {items.push({'name': i})});
|
||||||
var result = {'data': {'items': items}};
|
var result = {'data': {'items': items}};
|
||||||
callback(result);
|
callback(result);
|
||||||
});
|
});
|
||||||
|
|
1757
static/js/pandora.js
1757
static/js/pandora.js
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue