run bulk update as task

This commit is contained in:
j 2020-09-11 13:55:01 +02:00
parent 2d8a3f24dc
commit ca7741f92c
2 changed files with 24 additions and 6 deletions

View file

@ -1,4 +1,4 @@
# -*- coding: utf-8 -*- import ox
from celery.task import task from celery.task import task
@task(queue="encoding") @task(queue="encoding")
@ -6,3 +6,20 @@ def extract_fulltext(id):
from . import models from . import models
d = models.Document.objects.get(id=id) d = models.Document.objects.get(id=id)
d.update_fulltext() 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 {}

View file

@ -23,6 +23,7 @@ from archive.chunk import process_chunk
from changelog.models import add_changelog from changelog.models import add_changelog
from . import models from . import models
from . import tasks
def get_document_or_404_json(request, id): def get_document_or_404_json(request, id):
response = {'status': {'code': 404, 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 item = 'item' in data and Item.objects.get(public_id=data['item']) or None
if data['id']: if data['id']:
if isinstance(data['id'], list): 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: else:
documents = [models.Document.get(data['id'])] document = models.Document.get(data['id'])
for document in documents:
if document.editable(request.user, item): 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.edit(data, request.user, item)
document.save() document.save()
response['data'] = document.json(user=request.user, item=item) response['data'] = document.json(user=request.user, item=item)