sortlists

This commit is contained in:
j 2011-01-11 21:49:27 +05:30
parent c577e6930b
commit 270f618347
4 changed files with 81 additions and 19 deletions

View file

@ -134,7 +134,7 @@ class ListManager(Manager):
if conditions:
qs = qs.filter(conditions)
if user.is_anonymous:
if user.is_anonymous():
qs = qs.filter(Q(status='public') | Q(status='featured'))
else:
qs = qs.filter(Q(status='public') | Q(status='featured') | Q(user=user))

View file

@ -53,10 +53,10 @@ class List(models.Model):
self.ListItem.objects.all().filter(item=item, list=self).delete()
def __unicode__(self):
return u'%s (%s)' % (self.name, self.user)
return self.get_id()
def get_id(self):
return '%s:%s' % (self.user.username, self.name)
return u'%s:%s' % (self.user.username, self.name)
def editable(self, user):
#FIXME: make permissions work
@ -76,8 +76,6 @@ class List(models.Model):
elif key == 'query':
if not self.query.get('static', False):
response[key] = self.query
elif key == 'ui':
response[key] = site_conf['uiDefaults']['list']
else:
response[key] = getattr(self, key)
return response
@ -91,3 +89,14 @@ class ListItem(models.Model):
def __unicode__(self):
return u'%s in %s' % (self.item, self.list)
class Position(models.Model):
class Meta:
unique_together = ("user", "list", "section")
list = models.ForeignKey(List, related_name='position')
user = models.ForeignKey(User)
section = models.CharField(max_length='255')
position = models.IntegerField(default=0)

View file

@ -10,6 +10,10 @@ import models
from api.actions import actions
def get_list_or_404_json(id):
username, listname = id.split(':')
return get_object_or_404_json(models.List, user__username=username, name=listname)
def _order_query(qs, sort):
order_by = []
for e in sort:
@ -26,7 +30,8 @@ def _order_query(qs, sort):
def _parse_query(data, user):
query = {}
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':'+'}]
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'):
if key in data:
query[key] = data[key]
@ -186,8 +191,7 @@ def editList(request):
}
'''
data = json.loads(request.POST['data'])
username, listname = data['id'].split(':')
list = get_object_or_404_json(models.List, user__username=username, name=listname)
list = get_list_or_404_json(data['id'])
if list.editable(request.user):
response = json_response()
for key in data:
@ -250,8 +254,7 @@ def subscribeToList(request):
}
'''
data = json.loads(request.POST['data'])
username, listname = data['id'].split(':')
list = get_object_or_404_json(models.List, user__username=username, name=listname)
list = get_list_or_404_json(data['id'])
user = request.user
if list.subscribed_users.filter(username=user.username).count() == 0:
list.subscribed_users.add(user)
@ -273,10 +276,40 @@ def unsubscribeFromList(request):
}
'''
data = json.loads(request.POST['data'])
username, listname = data['id'].split(':')
list = get_object_or_404_json(models.List, user__username=username, name=listname)
list = get_list_or_404_json(data['id'])
user = request.user
list.subscribed_users.remove(user)
response = json_response()
return render_to_json_response(response)
actions.register(unsubscribeFromList)
@login_required_json
def sortLists(request):
'''
param data {
section: 'my',
ids: [1,2,4,3]
}
return {
status: {'code': int, 'text': string},
data: {
}
}
'''
data = json.loads(request.POST['data'])
position = 0
section = data['section']
#FIXME: featured list needs fixing here
user = request.user
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=data['section'])
pos.position = position
pos.save()
position += 1
response = json_response()
return render_to_json_response(response)
actions.register(sortLists)

View file

@ -11,7 +11,7 @@ from ox.utils import json
from ox.django.fields import DictField
from app.models import site_config
from itemlist.models import List
from itemlist.models import List, Position
class UserProfile(models.Model):
@ -35,12 +35,32 @@ class UserProfile(models.Model):
if not 'lists' in ui:
ui['lists'] = {}
ui['lists'][''] = site_config['uiDefaults']['list']
ids = [l.get_id() for l in self.user.lists.all()]
ids += [l.get_id() for l in self.user.subscribed_lists.all()]
ids += [l.get_id() for l in List.objects.filter(status='featured').exclude(user=self.user)]
for l in ids:
if l not in ui['lists']:
ui['lists'][l] = ui['lists']['']
def add(lists, section):
print lists, section
ids = [l.get_id() for l in lists]
in_list = filter(lambda l: l in ui['lists'], ids)
for l in lists:
print l
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section)
if created:
pos.position = len(in_list)
pos.save()
id = l.get_id()
if id not in in_list:
ui['lists'][id] = {}
ui['lists'][id].update(ui['lists'][''])
in_list.append(id)
ui['lists'][id]['position'] = pos.position
return ids
ids = ['']
ids += add(self.user.lists.exclude(status="featured"), 'my')
ids += add(self.user.subscribed_lists.all(), 'public')
ids += add(List.objects.filter(status='featured'), 'featured')
for i in ui['lists'].keys():
if i not in ids:
del ui['lists'][i]
return ui
def user_post_save(sender, instance, **kwargs):