sortlists
This commit is contained in:
parent
786a2d59bd
commit
b2dde1cd10
4 changed files with 81 additions and 19 deletions
|
@ -134,7 +134,7 @@ class ListManager(Manager):
|
||||||
if conditions:
|
if conditions:
|
||||||
qs = qs.filter(conditions)
|
qs = qs.filter(conditions)
|
||||||
|
|
||||||
if user.is_anonymous:
|
if user.is_anonymous():
|
||||||
qs = qs.filter(Q(status='public') | Q(status='featured'))
|
qs = qs.filter(Q(status='public') | Q(status='featured'))
|
||||||
else:
|
else:
|
||||||
qs = qs.filter(Q(status='public') | Q(status='featured') | Q(user=user))
|
qs = qs.filter(Q(status='public') | Q(status='featured') | Q(user=user))
|
||||||
|
|
|
@ -53,10 +53,10 @@ class List(models.Model):
|
||||||
self.ListItem.objects.all().filter(item=item, list=self).delete()
|
self.ListItem.objects.all().filter(item=item, list=self).delete()
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u'%s (%s)' % (self.name, self.user)
|
return self.get_id()
|
||||||
|
|
||||||
def get_id(self):
|
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):
|
def editable(self, user):
|
||||||
#FIXME: make permissions work
|
#FIXME: make permissions work
|
||||||
|
@ -76,8 +76,6 @@ class List(models.Model):
|
||||||
elif key == 'query':
|
elif key == 'query':
|
||||||
if not self.query.get('static', False):
|
if not self.query.get('static', False):
|
||||||
response[key] = self.query
|
response[key] = self.query
|
||||||
elif key == 'ui':
|
|
||||||
response[key] = site_conf['uiDefaults']['list']
|
|
||||||
else:
|
else:
|
||||||
response[key] = getattr(self, key)
|
response[key] = getattr(self, key)
|
||||||
return response
|
return response
|
||||||
|
@ -91,3 +89,14 @@ class ListItem(models.Model):
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u'%s in %s' % (self.item, self.list)
|
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)
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,10 @@ import models
|
||||||
from api.actions import actions
|
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):
|
def _order_query(qs, sort):
|
||||||
order_by = []
|
order_by = []
|
||||||
for e in sort:
|
for e in sort:
|
||||||
|
@ -26,7 +30,8 @@ def _order_query(qs, sort):
|
||||||
def _parse_query(data, user):
|
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':'+'}]
|
||||||
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'):
|
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'):
|
||||||
if key in data:
|
if key in data:
|
||||||
query[key] = data[key]
|
query[key] = data[key]
|
||||||
|
@ -186,8 +191,7 @@ def editList(request):
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
username, listname = data['id'].split(':')
|
list = get_list_or_404_json(data['id'])
|
||||||
list = get_object_or_404_json(models.List, user__username=username, name=listname)
|
|
||||||
if list.editable(request.user):
|
if list.editable(request.user):
|
||||||
response = json_response()
|
response = json_response()
|
||||||
for key in data:
|
for key in data:
|
||||||
|
@ -250,8 +254,7 @@ def subscribeToList(request):
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
username, listname = data['id'].split(':')
|
list = get_list_or_404_json(data['id'])
|
||||||
list = get_object_or_404_json(models.List, user__username=username, name=listname)
|
|
||||||
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)
|
||||||
|
@ -273,10 +276,40 @@ def unsubscribeFromList(request):
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
username, listname = data['id'].split(':')
|
list = get_list_or_404_json(data['id'])
|
||||||
list = get_object_or_404_json(models.List, user__username=username, name=listname)
|
|
||||||
user = request.user
|
user = request.user
|
||||||
list.subscribed_users.remove(user)
|
list.subscribed_users.remove(user)
|
||||||
response = json_response()
|
response = json_response()
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
actions.register(unsubscribeFromList)
|
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)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ from ox.utils import json
|
||||||
from ox.django.fields import DictField
|
from ox.django.fields import DictField
|
||||||
|
|
||||||
from app.models import site_config
|
from app.models import site_config
|
||||||
from itemlist.models import List
|
from itemlist.models import List, Position
|
||||||
|
|
||||||
|
|
||||||
class UserProfile(models.Model):
|
class UserProfile(models.Model):
|
||||||
|
@ -35,12 +35,32 @@ class UserProfile(models.Model):
|
||||||
if not 'lists' in ui:
|
if not 'lists' in ui:
|
||||||
ui['lists'] = {}
|
ui['lists'] = {}
|
||||||
ui['lists'][''] = site_config['uiDefaults']['list']
|
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()]
|
def add(lists, section):
|
||||||
ids += [l.get_id() for l in List.objects.filter(status='featured').exclude(user=self.user)]
|
print lists, section
|
||||||
for l in ids:
|
ids = [l.get_id() for l in lists]
|
||||||
if l not in ui['lists']:
|
in_list = filter(lambda l: l in ui['lists'], ids)
|
||||||
ui['lists'][l] = ui['lists']['']
|
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
|
return ui
|
||||||
|
|
||||||
def user_post_save(sender, instance, **kwargs):
|
def user_post_save(sender, instance, **kwargs):
|
||||||
|
|
Loading…
Reference in a new issue