add option to add documents to all items in list

This commit is contained in:
j 2014-01-09 08:05:35 +00:00
parent 8be4b16e48
commit 3ff178ed83
2 changed files with 46 additions and 21 deletions

View file

@ -12,6 +12,7 @@ from django.db.models import Sum
from item import utils from item import utils
from item.models import Item from item.models import Item
from itemlist.models import List
import models import models
def get_document_or_404_json(id): def get_document_or_404_json(id):
@ -23,6 +24,9 @@ def addDocument(request):
add document(s) to item add document(s) to item
takes { takes {
item: string item: string
or:
list: string
id: string id: string
or or
ids: [string] ids: [string]
@ -36,13 +40,21 @@ def addDocument(request):
ids = data['ids'] ids = data['ids']
else: else:
ids = [data['id']] ids = [data['id']]
item = Item.objects.get(itemId=data['item']) if 'item' in data:
if item.editable(request.user): item = Item.objects.get(itemId=data['item'])
for id in ids: if item.editable(request.user):
document = models.Document.get(id) for id in ids:
document.add(item) document = models.Document.get(id)
else: document.add(item)
response = json_response(status=403, text='permission denied') else:
response = json_response(status=403, text='permission denied')
elif 'list' in data:
list = List.get(data['list'])
for item in list.get_items(request.user):
if item.editable(request.user):
for id in ids:
document = models.Document.get(id)
document.add(item)
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addDocument, cache=False) actions.register(addDocument, cache=False)
@ -258,6 +270,7 @@ def upload(request):
if 'id' in request.GET: if 'id' in request.GET:
file = models.Document.get(request.GET['id']) file = models.Document.get(request.GET['id'])
else: else:
file = None
extension = request.POST['filename'].split('.') extension = request.POST['filename'].split('.')
name = '.'.join(extension[:-1]) name = '.'.join(extension[:-1])
extension = extension[-1].lower() extension = extension[-1].lower()
@ -279,19 +292,25 @@ def upload(request):
return render_to_json_response(response) return render_to_json_response(response)
#init upload #init upload
else: else:
created = False if not file:
num = 1 created = False
_name = name num = 1
while not created: _name = name
file, created = models.Document.objects.get_or_create( while not created:
user=request.user, name=name, extension=extension) file, created = models.Document.objects.get_or_create(
if not created: user=request.user, name=name, extension=extension)
num += 1 if not created:
name = _name + ' [%d]' % num num += 1
file.name = name name = _name + ' [%d]' % num
file.extension = extension file.name = name
file.uploading = True file.extension = extension
file.save() file.uploading = True
file.save()
else:
#replace existing file
file.file.delete()
file.uploading = True
file.save()
upload_url = request.build_absolute_uri('/api/upload/document?id=%s' % file.get_id()) upload_url = request.build_absolute_uri('/api/upload/document?id=%s' % file.get_id())
return render_to_json_response({ return render_to_json_response({
'uploadUrl': upload_url, 'uploadUrl': upload_url,

View file

@ -17,7 +17,6 @@ from ox.django.fields import DictField, TupleField
from archive import extract from archive import extract
import managers import managers
class List(models.Model): class List(models.Model):
class Meta: class Meta:
@ -59,6 +58,13 @@ class List(models.Model):
self.numberofitems = self.get_numberofitems(self.user) self.numberofitems = self.get_numberofitems(self.user)
super(List, self).save(*args, **kwargs) super(List, self).save(*args, **kwargs)
@classmethod
def get(cls, id):
id = id.split(':')
username = id[0]
listname = ":".join(id[1:])
return cls.objects.get(user__username=username, name=listname)
def get_items(self, user=None): def get_items(self, user=None):
if self.query.get('static', False): if self.query.get('static', False):
return self.items return self.items