more lists

This commit is contained in:
j 2011-01-12 01:42:35 +05:30
parent b2dde1cd10
commit d8ad8cf9fb
5 changed files with 85 additions and 29 deletions

View file

@ -4,9 +4,9 @@ from datetime import datetime
from django.db.models import Q, Manager from django.db.models import Q, Manager
from itemlist.models import List
import models import models
def parseCondition(condition): def parseCondition(condition):
''' '''
condition: { condition: {
@ -41,8 +41,10 @@ def parseCondition(condition):
'text': 'string', 'text': 'string',
'year': 'string', 'year': 'string',
'length': 'string', 'length': 'string',
'list': 'list'
}.get(key_type, key_type) }.get(key_type, key_type)
if k == 'list':
key_type = 'list'
if key_type == "string": if key_type == "string":
in_find=True in_find=True
value_key = 'find__value' value_key = 'find__value'
@ -79,6 +81,19 @@ def parseCondition(condition):
else: else:
q = Q(**{k: v}) q = Q(**{k: v})
return q return q
elif key_type == 'list':
l = v.split(".")
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'],
data.get('operator', '&'))
else:
q = Q(id__in=lqs[0].items.all())
else:
q = Q(itemId=False)
return q
else: #number or date else: #number or date
def parseDate(d): def parseDate(d):
@ -184,7 +199,7 @@ class ItemManager(Manager):
if len(l) == 2: if len(l) == 2:
lqs = models.List.objects.filter(name=l[1], user__username=l[0]) lqs = models.List.objects.filter(name=l[1], user__username=l[0])
if only_public: if only_public:
lqs = lqs.filter(public=True) lqs = lqs.filter(Q(status='public')|Q(status='featured'))
if lqs.count() == 1: if lqs.count() == 1:
if lqs[0].query: if lqs[0].query:
data = lqs[0].query data = lqs[0].query

View file

@ -21,7 +21,10 @@ def parseCondition(condition, user):
... ...
''' '''
k = condition.get('key', 'name') k = condition.get('key', 'name')
k = {'user': 'user__username'}.get(k, k) k = {
'user': 'user__username',
'position': 'position__position',
}.get(k, k)
if not k: if not k:
k = 'name' k = 'name'
v = condition['value'] v = condition['value']

View file

@ -7,7 +7,6 @@ from django.contrib.auth.models import User
from ox.django.fields import DictField from ox.django.fields import DictField
from item.models import Item
import managers import managers
@ -39,6 +38,7 @@ class List(models.Model):
if self.query.get('static', False): if self.query.get('static', False):
return self.items.count() return self.items.count()
else: else:
from item.models import Item
return Item.objects.find({'query': self.query}, user).count() return Item.objects.find({'query': self.query}, user).count()
def add(self, item): def add(self, item):
@ -56,7 +56,7 @@ class List(models.Model):
return self.get_id() return self.get_id()
def get_id(self): def get_id(self):
return u'%s:%s' % (self.user.username, self.name) return u'%s.%s' % (self.user.username, self.name)
def editable(self, user): def editable(self, user):
#FIXME: make permissions work #FIXME: make permissions work
@ -100,3 +100,6 @@ class Position(models.Model):
section = models.CharField(max_length='255') section = models.CharField(max_length='255')
position = models.IntegerField(default=0) position = models.IntegerField(default=0)
def __unicode__(self):
return u'%s/%s/%s' % (self.section, self.position, self.list)

View file

@ -11,7 +11,7 @@ from api.actions import actions
def get_list_or_404_json(id): def get_list_or_404_json(id):
username, listname = id.split(':') username, listname = id.split('.')
return get_object_or_404_json(models.List, user__username=username, name=listname) return get_object_or_404_json(models.List, user__username=username, name=listname)
def _order_query(qs, sort): def _order_query(qs, sort):
@ -31,8 +31,8 @@ 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__section', 'operator':'+'}, {'key':'position__position', 'operator':'+'}] query['sort'] = [{'key':'position__position', 'operator':'+'}]
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'): for key in ('keys', 'group', 'list', 'range', 'ids'):
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)
@ -103,9 +103,9 @@ def addListItem(request):
} }
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
list = get_object_or_404_json(models.List, pk=data['list']) list = get_list_or_404_json(data['list'])
if 'item' in data: if 'item' in data:
item = get_object_or_404_json(models.Item, pk=data['item']) item = get_object_or_404_json(models.Item, itemId=data['item'])
if list.editable(request.user): if list.editable(request.user):
list.add(item) list.add(item)
response = json_response(status=200, text='item added') response = json_response(status=200, text='item added')
@ -135,9 +135,9 @@ def removeListItem(request):
} }
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
list = get_object_or_404_json(models.List, pk=data['list']) list = get_list_or_404_json(data['list'])
if 'item' in data: if 'item' in data:
item = get_object_or_404_json(models.Item, pk=data['item']) item = get_object_or_404_json(models.Item, itemId=data['item'])
if list.editable(request.user): if list.editable(request.user):
list.remove(item) list.remove(item)
response = json_response(status=200, text='item removed') response = json_response(status=200, text='item removed')
@ -155,8 +155,10 @@ actions.register(removeListItem)
@login_required_json @login_required_json
def addList(request): 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},
data: { data: {
@ -168,6 +170,9 @@ def addList(request):
if models.List.objects.filter(name=data['name'], user=request.user).count() == 0: if models.List.objects.filter(name=data['name'], user=request.user).count() == 0:
list = models.List(name = data['name'], user=request.user) list = models.List(name = data['name'], user=request.user)
list.save() list.save()
pos, created = models.Position.objects.get_or_create(list=list, user=request.user, section='my')
pos.position = data['position']
pos.save()
response = json_response(status=200, text='created') response = json_response(status=200, text='created')
response['data']['list'] = list.json() response['data']['list'] = list.json()
else: else:
@ -182,6 +187,7 @@ def editList(request):
''' '''
param data { param data {
key: value key: value
position: int
} }
keys: name, public, query, featured (if admin) keys: name, public, query, featured (if admin)
return { return {
@ -206,8 +212,15 @@ def editList(request):
setattr(list, key, value) setattr(list, key, value)
else: else:
setattr(list, key, data[key]) setattr(list, key, data[key])
if request.user.is_staff and 'featured' in data:
list.featured = data['featured'] if 'position' in data:
pos, created = models.Position.objects.get_or_create(list=list, user=request.user)
pos.position = data['position']
pos.section = 'featured'
if list.status == 'private':
pos.section = 'my'
pos.save()
list.save()
else: else:
response = json_response(status=403, text='not allowed') response = json_response(status=403, text='not allowed')
return render_to_json_response(response) return render_to_json_response(response)
@ -258,6 +271,9 @@ def subscribeToList(request):
user = request.user user = request.user
if list.subscribed_users.filter(username=user.username).count() == 0: if list.subscribed_users.filter(username=user.username).count() == 0:
list.subscribed_users.add(user) list.subscribed_users.add(user)
pos, created = models.Position.objects.get_or_create(list=list, user=request.user, section='public')
pos.position = data['position']
pos.save()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(subscribeToList) actions.register(subscribeToList)
@ -279,6 +295,7 @@ def unsubscribeFromList(request):
list = get_list_or_404_json(data['id']) list = get_list_or_404_json(data['id'])
user = request.user user = request.user
list.subscribed_users.remove(user) list.subscribed_users.remove(user)
models.Position.objects.filter(list=list, user=request.user, section='public').delete()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(unsubscribeFromList) actions.register(unsubscribeFromList)
@ -300,16 +317,32 @@ def sortLists(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
position = 0 position = 0
section = data['section'] section = data['section']
#FIXME: featured list needs fixing here if section == 'featured' and not request.user.is_staff:
user = request.user response = json_response(status=403, text='not allowed')
for i in data['ids']: else:
list = get_list_or_404_json(i) user = request.user
pos, created = models.Position.objects.get_or_create(list=list, user=request.user, section=data['section']) if section == 'featured':
pos.position = position for i in data['ids']:
pos.save() list = get_list_or_404_json(i)
position += 1 qs = models.Position.objects.filter(section=section, list=list)
if qs.count() > 0:
pos = qs[0]
else:
pos = models.Position(list=list, user=user, section=section)
pos.position = position
pos.save()
position += 1
models.Position.objects.filter(section=section, list=list).exclude(id=pos.id).delete()
else:
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=section)
pos.position = position
pos.save()
position += 1
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(sortLists) actions.register(sortLists)

View file

@ -41,8 +41,10 @@ class UserProfile(models.Model):
ids = [l.get_id() for l in lists] ids = [l.get_id() for l in lists]
in_list = filter(lambda l: l in ui['lists'], ids) in_list = filter(lambda l: l in ui['lists'], ids)
for l in lists: for l in lists:
print l if section == 'featured':
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section) pos, created = Position.objects.get_or_create(list=l, section=section)
else:
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section)
if created: if created:
pos.position = len(in_list) pos.position = len(in_list)
pos.save() pos.save()