This commit is contained in:
j 2017-02-15 14:08:35 +00:00
parent dc5fd74d9f
commit 596d10ce83
2 changed files with 69 additions and 23 deletions

View file

@ -7,9 +7,13 @@ from six.moves.urllib.parse import quote
from django.db import models from django.db import models
from django.db.models import Max from django.db.models import Max
from django.db.models.signals import pre_delete
import ox import ox
from oxdjango import fields from oxdjango import fields
from itemlist.models import List
from edit.models import Edit
from documentcollection.models import Collection
class Item(models.Model): class Item(models.Model):
@ -43,6 +47,11 @@ class Item(models.Model):
if not isinstance(data[key], string_types): if not isinstance(data[key], string_types):
return False return False
self.data[key] = data[key] self.data[key] = data[key]
if key == 'contentid':
try:
content = self.get_content()
except:
return False
changed = True changed = True
if 'type' in data: if 'type' in data:
if data['type'] == 'custom': if data['type'] == 'custom':
@ -56,6 +65,11 @@ class Item(models.Model):
changed = True changed = True
if 'active' in data: if 'active' in data:
self.active = data['active'] is True self.active = data['active'] is True
idx = Item.objects.filter(active=self.active).aggregate(Max('index'))['index__max']
if idx is None:
self.index = 0
else:
self.index = idx + 1
changed = True changed = True
if changed: if changed:
self.save() self.save()
@ -74,6 +88,30 @@ class Item(models.Model):
def get_id(self): def get_id(self):
return ox.toAZ(self.id) return ox.toAZ(self.id)
def get_content(self):
content_keys = [
'description',
'modified',
'name',
'user',
]
type = self.data.get('type')
contentid = self.data.get('contentid')
if not contentid:
return None
if type == 'list':
content = List.get(contentid).json(keys=content_keys)
content['link'] = '/list==' + quote(content['user'] + ':' + content['name'])
elif type == 'edit':
content = Edit.get(contentid).json(keys=content_keys)
content['link'] = '/edits' + quote(content['user'] + ':' + content['name'])
elif type == 'collection':
content = Collection.get(contentid).json(keys=content_keys)
content['link'] = '/documents/collection==' + quote(content['user'] + ':' + content['name'])
else:
content = None
return content
def json(self, keys=None): def json(self, keys=None):
j = { j = {
'id': self.get_id(), 'id': self.get_id(),
@ -82,29 +120,18 @@ class Item(models.Model):
} }
j.update(self.data) j.update(self.data)
if 'contentid' in j: if 'contentid' in j:
content_keys = [ try:
'description', content = self.get_content()
'modified', if content:
'name', j['title'] = content['name']
'user', j['text'] = content['description']
] j['link'] = content['link']
type = j.get('type') j['image'] = '/' + '/'.join([
if type == 'list': j['type'], quote(content['user'] + ':' + content['name']),
from itemlist.models import List 'icon256.jpg?%s' % content['modified'].strftime('%Y-%m-%dT%H:%M:%SZ')
content = List.get(j['contentid']).json(keys=content_keys) ])
elif type == 'edit': except:
from edit.models import Edit pass
content = Edit.get(j['contentid']).json(keys=content_keys)
elif type == 'collection':
from documentcollection.models import Collection
content = Collection.get(j['contentid']).json(keys=content_keys)
j['title'] = content['name']
j['text'] = content['description']
j['image'] = '/' + '/'.join([
type, quote(content['user'] + ':' + content['name']),
'icon256.jpg?%s' % content['modified'].strftime('%Y-%m-%dT%H:%M:%SZ')
])
if keys: if keys:
for key in list(j): for key in list(j):
if key not in keys: if key not in keys:
@ -113,3 +140,20 @@ class Item(models.Model):
def __unicode__(self): def __unicode__(self):
return u"%s" % (self.get_id()) return u"%s" % (self.get_id())
def delete_item(type, contentid):
for home in Item.objects.all():
if type == home.data.get('type') and contentid == home.data.get('contentid'):
home.delete()
def delete_list(sender, **kwargs):
delete_item('list', kwargs['instance'].get_id())
pre_delete.connect(delete_list, sender=List)
def delete_edit(sender, **kwargs):
delete_item('edit', kwargs['instance'].get_id())
pre_delete.connect(delete_edit, sender=Edit)
def delete_collection(sender, **kwargs):
delete_item('collection', kwargs['instance'].get_id())
pre_delete.connect(delete_collection, sender=Collection)

View file

@ -23,6 +23,8 @@ def addHomeItem(request, data):
''' '''
item = models.Item() item = models.Item()
response = json_response() response = json_response()
if not 'type' in data:
data['type'] = 'custom'
if not item.edit(data): if not item.edit(data):
response = json_response(status=500, text='invalid data') response = json_response(status=500, text='invalid data')
else: else: