From 3ff178ed83001275bc7ef285eb6ee6da9230edf9 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 9 Jan 2014 08:05:35 +0000 Subject: [PATCH] add option to add documents to all items in list --- pandora/document/views.py | 59 +++++++++++++++++++++++++------------- pandora/itemlist/models.py | 8 +++++- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/pandora/document/views.py b/pandora/document/views.py index 1b6287c1..a3d190da 100644 --- a/pandora/document/views.py +++ b/pandora/document/views.py @@ -12,6 +12,7 @@ from django.db.models import Sum from item import utils from item.models import Item +from itemlist.models import List import models def get_document_or_404_json(id): @@ -23,6 +24,9 @@ def addDocument(request): add document(s) to item takes { item: string + or: + list: string + id: string or ids: [string] @@ -36,13 +40,21 @@ def addDocument(request): ids = data['ids'] else: ids = [data['id']] - item = Item.objects.get(itemId=data['item']) - if item.editable(request.user): - for id in ids: - document = models.Document.get(id) - document.add(item) - else: - response = json_response(status=403, text='permission denied') + if 'item' in data: + item = Item.objects.get(itemId=data['item']) + if item.editable(request.user): + for id in ids: + document = models.Document.get(id) + document.add(item) + 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) actions.register(addDocument, cache=False) @@ -258,6 +270,7 @@ def upload(request): if 'id' in request.GET: file = models.Document.get(request.GET['id']) else: + file = None extension = request.POST['filename'].split('.') name = '.'.join(extension[:-1]) extension = extension[-1].lower() @@ -279,19 +292,25 @@ def upload(request): return render_to_json_response(response) #init upload else: - created = False - num = 1 - _name = name - while not created: - file, created = models.Document.objects.get_or_create( - user=request.user, name=name, extension=extension) - if not created: - num += 1 - name = _name + ' [%d]' % num - file.name = name - file.extension = extension - file.uploading = True - file.save() + if not file: + created = False + num = 1 + _name = name + while not created: + file, created = models.Document.objects.get_or_create( + user=request.user, name=name, extension=extension) + if not created: + num += 1 + name = _name + ' [%d]' % num + file.name = name + file.extension = extension + 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()) return render_to_json_response({ 'uploadUrl': upload_url, diff --git a/pandora/itemlist/models.py b/pandora/itemlist/models.py index fd4c5494..6e3264b4 100644 --- a/pandora/itemlist/models.py +++ b/pandora/itemlist/models.py @@ -17,7 +17,6 @@ from ox.django.fields import DictField, TupleField from archive import extract import managers - class List(models.Model): class Meta: @@ -59,6 +58,13 @@ class List(models.Model): self.numberofitems = self.get_numberofitems(self.user) 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): if self.query.get('static', False): return self.items