even more improvements to lists

This commit is contained in:
rlx 2011-01-13 19:40:50 +00:00
parent 1a8a478256
commit 3a8f437f96
7 changed files with 154 additions and 1748 deletions

View file

@ -87,12 +87,13 @@ def parseCondition(condition):
if len(l) == 2:
lqs = List.objects.filter(name=l[1], user__username=l[0])
if lqs.count() == 1:
if lqs[0].query.get('static', False) == False:
data = lqs[0].query
q = parseConditions(data['conditions'],
l = lqs[0]
if l.query.get('static', False) == False:
data = l.query
q = parseConditions(data.get('conditions', []),
data.get('operator', '&'))
else:
q = Q(id__in=lqs[0].items.all())
q = Q(id__in=l.items.all())
return q
else: #number or date
@ -237,7 +238,7 @@ class ItemManager(Manager):
qs = self.get_query_set()
#only include items that have hard metadata
qs = qs.filter(available=True)
conditions = parseConditions(data['query']['conditions'],
conditions = parseConditions(data['query'].get('conditions', []),
data['query'].get('operator', '&'))
if conditions:
qs = qs.filter(conditions)

View file

@ -206,3 +206,16 @@ def sort_title(title):
#pad numbered titles
title = re.sub('(\d+)', lambda x: '%010d' % int(x.group(0)), title)
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

View file

@ -70,16 +70,6 @@ def _parse_query(data, user):
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):
'''
param data {
@ -184,7 +174,7 @@ Positions
#FIXME: this does not scale for larger results
response['data']['positions'] = {}
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:
qs = qs[query['range'][0]:query['range'][1]]

View file

@ -25,6 +25,7 @@ class List(models.Model):
query = DictField(default={"static": True})
type= models.CharField(max_length=255, default='static')
#is through table still required?
items = models.ManyToManyField('item.Item', related_name='lists',
through='ListItem')

View file

@ -2,12 +2,14 @@
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
from django.db.models import Max
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
import models
from api.actions import actions
from item import utils
def get_list_or_404_json(id):
@ -31,17 +33,15 @@ def _parse_query(data, user):
query = {}
query['range'] = [0, 100]
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'):
if key in data:
query[key] = data[key]
query['qs'] = models.List.objects.find(data, user)
return query
def findLists(request):
'''
FIXME: support key: subscribed
param data {
query: {
conditions: [
@ -78,12 +78,17 @@ def findLists(request):
#order
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:
qs = query['qs']
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')
else:
qs = _order_query(query['qs'], query['sort'])
@ -94,6 +99,9 @@ def findLists(request):
qs = qs[query['range'][0]:query['range'][1]]
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:
response['data']['items'] = qs.count()
return render_to_json_response(response)
@ -105,7 +113,7 @@ def addListItem(request):
param data
{list: listId,
item: itemId,
quert: ...
query: ...
}
return {
status: {'code': int, 'text': string},
@ -124,7 +132,6 @@ def addListItem(request):
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)
@ -168,7 +175,6 @@ def addList(request):
'''
param data {
name: value,
position: int
}
return {
status: {'code': int, 'text': string},
@ -185,9 +191,31 @@ def addList(request):
name = data['name'] + ' (%d)' % num
list = models.List(name = name, user=request.user)
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,
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()
response = json_response(status=200, text='created')
response['data'] = list.json()
@ -216,35 +244,35 @@ def editList(request):
if list.editable(request.user):
response = json_response()
for key in data:
if key in ('name', 'status', 'query', 'type'):
if key in data:
if key == 'query' and not data['query']:
setattr(list, key, {"static":True})
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)
if value == 'private':
for user in list.subscribed_users.all():
list.subscribed_users.remove(user)
elif key == 'name':
name = data['name']
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
setattr(list, key, name)
else:
setattr(list, key, data[key])
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)
if value == 'private':
for user in list.subscribed_users.all():
list.subscribed_users.remove(user)
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
setattr(list, key, name)
if 'position' in data:
pos, created = models.Position.objects.get_or_create(list=list, user=request.user)

View file

@ -68,14 +68,14 @@ function constructList() {
if(!data.keys) {
app.api.api(function(results) {
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}};
callback(result);
});
} else {
app.api.api(function(results) {
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}};
callback(result);
});

File diff suppressed because it is too large Load diff