refactor addList/editList
This commit is contained in:
parent
9377f772e4
commit
ff0dcf5028
4 changed files with 95 additions and 115 deletions
|
@ -2,6 +2,7 @@
|
|||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
from glob import glob
|
||||
|
||||
|
@ -99,6 +100,90 @@ class List(models.Model):
|
|||
return True
|
||||
return False
|
||||
|
||||
def edit(self, data, user):
|
||||
for key in data:
|
||||
if key == 'query' and not data['query']:
|
||||
setattr(self, key, {"static":True})
|
||||
elif key == 'query':
|
||||
setattr(self, key, data[key])
|
||||
elif key == 'type':
|
||||
if data[key] == 'static':
|
||||
self.query = {"static":True}
|
||||
self.type = 'static'
|
||||
else:
|
||||
self.type = 'dynamic'
|
||||
if self.query.get('static', False):
|
||||
self.query = {}
|
||||
elif key == 'status':
|
||||
value = data[key]
|
||||
if value not in self._status:
|
||||
value = self._status[0]
|
||||
if value == 'private':
|
||||
for user in self.subscribed_users.all():
|
||||
self.subscribed_users.remove(user)
|
||||
qs = Position.objects.filter(user=user,
|
||||
section='section', list=self)
|
||||
if qs.count() > 1:
|
||||
pos = qs[0]
|
||||
pos.section = 'personal'
|
||||
pos.save()
|
||||
elif value == 'featured':
|
||||
if user.get_profile().capability('canEditFeaturedLists'):
|
||||
pos, created = Position.objects.get_or_create(list=self, user=user,
|
||||
section='featured')
|
||||
if created:
|
||||
qs = Position.objects.filter(user=user, section='featured')
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
Position.objects.filter(list=self).exclude(id=pos.id).delete()
|
||||
else:
|
||||
value = self.status
|
||||
elif self.status == 'featured' and value == 'public':
|
||||
Position.objects.filter(list=self).delete()
|
||||
pos, created = Position.objects.get_or_create(list=self,
|
||||
user=self.user, section='personal')
|
||||
qs = Position.objects.filter(user=self.user,
|
||||
section='personal')
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
for u in self.subscribed_users.all():
|
||||
pos, created = Position.objects.get_or_create(list=self, user=u,
|
||||
section='public')
|
||||
qs = Position.objects.filter(user=u, section='public')
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
|
||||
self.status = value
|
||||
elif key == 'name':
|
||||
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
|
||||
name = data['name']
|
||||
if not name:
|
||||
name = "Untitled"
|
||||
num = 1
|
||||
while List.objects.filter(name=name, user=self.user).exclude(id=self.id).count()>0:
|
||||
num += 1
|
||||
name = data['name'] + ' [%d]' % num
|
||||
self.name = name
|
||||
elif key == 'description':
|
||||
self.description = ox.sanitize_html(data['description'])
|
||||
|
||||
if 'position' in data:
|
||||
pos, created = Position.objects.get_or_create(list=self, user=user)
|
||||
pos.position = data['position']
|
||||
pos.section = 'featured'
|
||||
if self.status == 'private':
|
||||
pos.section = 'personal'
|
||||
pos.save()
|
||||
if 'posterFrames' in data:
|
||||
self.poster_frames = tuple(data['posterFrames'])
|
||||
if 'view' in data:
|
||||
self.view = data['view']
|
||||
if 'sort' in data:
|
||||
self.sort= tuple(data['sort'])
|
||||
self.save()
|
||||
if 'posterFrames' in data:
|
||||
self.update_icon()
|
||||
|
||||
def json(self, keys=None, user=None):
|
||||
if not keys:
|
||||
keys=['id', 'name', 'user', 'type', 'query', 'status', 'subscribed', 'posterFrames', 'description']
|
||||
|
|
|
@ -214,7 +214,7 @@ def addList(request):
|
|||
}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
|
||||
data['name'] = re.sub(' \[\d+\]$', '', data.get('name', 'Untitled')).strip()
|
||||
name = data['name']
|
||||
if not name:
|
||||
name = "Untitled"
|
||||
|
@ -225,34 +225,10 @@ def addList(request):
|
|||
num += 1
|
||||
name = data['name'] + ' [%d]' % num
|
||||
|
||||
if 'query' in data and data['query']:
|
||||
setattr(list, 'query', data['query'])
|
||||
del data['name']
|
||||
if data:
|
||||
list.edit(data, request.user)
|
||||
else:
|
||||
setattr(list, 'query', {"static":True})
|
||||
if 'type' in data:
|
||||
if data['type'] == 'static':
|
||||
list.query = {"static":True}
|
||||
list.type = 'static'
|
||||
else:
|
||||
list.type = 'dynamic'
|
||||
if list.query.get('static', False):
|
||||
list.query = {}
|
||||
if 'status' in data:
|
||||
value = data['status']
|
||||
if value not in list._status:
|
||||
value = list._status[0]
|
||||
if value == 'featured' and request.user.get_profile().capability('canEditFeaturedLists'):
|
||||
value = list.status
|
||||
list.status = value
|
||||
if 'description' in data:
|
||||
list.description = ox.sanitize_html(data['description'])
|
||||
if 'view' in data:
|
||||
list.view = data['view']
|
||||
if 'sort' in data:
|
||||
list.sort= tuple(data['sort'])
|
||||
if 'posterFrames' in data:
|
||||
list.poster_frames = tuple(data['posterFrames'])
|
||||
|
||||
list.save()
|
||||
update_numberoflists.delay(request.user.username)
|
||||
|
||||
|
@ -260,9 +236,6 @@ def addList(request):
|
|||
for item in Item.objects.filter(itemId__in=data['items']):
|
||||
list.add(item)
|
||||
|
||||
if 'posterFrames' in data:
|
||||
list.update_icon()
|
||||
|
||||
if list.status == 'featured':
|
||||
pos, created = models.Position.objects.get_or_create(list=list,
|
||||
user=request.user, section='featured')
|
||||
|
@ -301,87 +274,7 @@ def editList(request):
|
|||
list = get_list_or_404_json(data['id'])
|
||||
if list.editable(request.user):
|
||||
response = json_response()
|
||||
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]
|
||||
if value == 'private':
|
||||
for user in list.subscribed_users.all():
|
||||
list.subscribed_users.remove(user)
|
||||
qs = models.Position.objects.filter(user=request.user,
|
||||
section='section', list=list)
|
||||
if qs.count() > 1:
|
||||
pos = qs[0]
|
||||
pos.section = 'personal'
|
||||
pos.save()
|
||||
elif value == 'featured':
|
||||
if request.user.get_profile().capability('canEditFeaturedLists'):
|
||||
pos, created = models.Position.objects.get_or_create(list=list, user=request.user,
|
||||
section='featured')
|
||||
if created:
|
||||
qs = models.Position.objects.filter(user=request.user, section='featured')
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
models.Position.objects.filter(list=list).exclude(id=pos.id).delete()
|
||||
else:
|
||||
value = list.status
|
||||
elif list.status == 'featured' and value == 'public':
|
||||
models.Position.objects.filter(list=list).delete()
|
||||
pos, created = models.Position.objects.get_or_create(list=list,
|
||||
user=list.user,section='personal')
|
||||
qs = models.Position.objects.filter(user=list.user,
|
||||
section='personal')
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
for u in list.subscribed_users.all():
|
||||
pos, created = models.Position.objects.get_or_create(list=list, user=u,
|
||||
section='public')
|
||||
qs = models.Position.objects.filter(user=u, section='public')
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
|
||||
list.status = value
|
||||
elif key == 'name':
|
||||
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
|
||||
name = data['name']
|
||||
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
|
||||
list.name = name
|
||||
elif key == 'description':
|
||||
list.description = ox.sanitize_html(data['description'])
|
||||
|
||||
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 = 'personal'
|
||||
pos.save()
|
||||
if 'posterFrames' in data:
|
||||
list.poster_frames = tuple(data['posterFrames'])
|
||||
list.update_icon()
|
||||
if 'view' in data:
|
||||
list.view = data['view']
|
||||
if 'sort' in data:
|
||||
list.sort= tuple(data['sort'])
|
||||
list.save()
|
||||
list.edit(data, request.user)
|
||||
response['data'] = list.json(user=request.user)
|
||||
else:
|
||||
response = json_response(status=403, text='not allowed')
|
||||
|
|
|
@ -138,8 +138,9 @@ class Text(models.Model):
|
|||
self.type = data['type'] == 'pdf' and 'pdf' or 'html'
|
||||
if 'posterFrames' in data:
|
||||
self.poster_frames = tuple(data['posterFrames'])
|
||||
self.update_icon()
|
||||
self.save()
|
||||
if 'posterFrames' in data:
|
||||
self.update_icon()
|
||||
|
||||
def json(self, keys=None, user=None):
|
||||
if not keys:
|
||||
|
|
|
@ -49,11 +49,12 @@ def addText(request):
|
|||
text, created = models.Text.objects.get_or_create(name=name, user=request.user)
|
||||
num += 1
|
||||
name = data['name'] + ' [%d]' % num
|
||||
text.save()
|
||||
|
||||
del data['name']
|
||||
if data:
|
||||
text.edit(data, request.user)
|
||||
else:
|
||||
text.save()
|
||||
|
||||
if text.status == 'featured':
|
||||
pos, created = models.Position.objects.get_or_create(text=text,
|
||||
|
|
Loading…
Reference in a new issue