diff --git a/pandora/document/tasks.py b/pandora/document/tasks.py index f08983bb2..b75fbefe0 100644 --- a/pandora/document/tasks.py +++ b/pandora/document/tasks.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +import ox from celery.task import task @task(queue="encoding") @@ -6,3 +6,20 @@ def extract_fulltext(id): from . import models d = models.Document.objects.get(id=id) d.update_fulltext() + + +@task(queue='default') +def builk_edit(data, username): + from django.db import transaction + from . import models + from item.models import Item + user = models.User.objects.get(username=username) + item = 'item' in data and Item.objects.get(public_id=data['item']) or None + documents = models.Document.objects.filter(pk__in=map(ox.fromAZ, data['id'])) + for document in documents: + if document.editable(user, item): + with transaction.atomic(): + document.refresh_from_db() + document.edit(data, user, item) + document.save() + return {} diff --git a/pandora/document/views.py b/pandora/document/views.py index 84279ac9c..f22bed81e 100644 --- a/pandora/document/views.py +++ b/pandora/document/views.py @@ -23,6 +23,7 @@ from archive.chunk import process_chunk from changelog.models import add_changelog from . import models +from . import tasks def get_document_or_404_json(request, id): response = {'status': {'code': 404, @@ -131,13 +132,13 @@ def editDocument(request, data): item = 'item' in data and Item.objects.get(public_id=data['item']) or None if data['id']: if isinstance(data['id'], list): - documents = models.Document.objects.filter(pk__in=map(ox.fromAZ, data['id'])) + add_changelog(request, data) + t = tasks.builk_edit.delay(data, request.user.username) + response['data']['taskId'] = t.task_id else: - documents = [models.Document.get(data['id'])] - for document in documents: + document = models.Document.get(data['id']) if document.editable(request.user, item): - if document == documents[0]: - add_changelog(request, data) + add_changelog(request, data) document.edit(data, request.user, item) document.save() response['data'] = document.json(user=request.user, item=item)