move buld item metadata edits to tasks too

This commit is contained in:
j 2020-09-13 16:41:34 +02:00
parent 945ac98dad
commit d00cf08638
3 changed files with 35 additions and 14 deletions

View file

@ -5,6 +5,7 @@ from urllib.parse import quote
import gzip
import os
import random
import logging
from celery.task import task, periodic_task
from django.conf import settings
@ -16,6 +17,9 @@ from app.utils import limit_rate
from taskqueue.models import Task
logger = logging.getLogger(__name__)
@periodic_task(run_every=timedelta(days=1), queue='encoding')
def cronjob(**kwargs):
if limit_rate('item.tasks.cronjob', 8 * 60 * 60):
@ -350,3 +354,18 @@ def update_sitemap(base_url):
f.write(data)
with gzip.open(sitemap, 'wb') as f:
f.write(data)
@task(queue='default')
def bulk_edit(data, username):
from django.db import transaction
from . import models
from .views import edit_item
user = models.User.objects.get(username=username)
items = models.Item.objects.filter(public_id__in=data['id'])
for item in items:
if item.editable(user):
with transaction.atomic():
item.refresh_from_db()
response = edit_item(user, item, data)
return {}

View file

@ -533,17 +533,18 @@ def get(request, data):
return render_to_json_response(response)
actions.register(get)
def edit_item(request, item, data):
def edit_item(user, item, data):
data = data.copy()
update_clips = False
response = json_response(status=200, text='ok')
if 'rightslevel' in data:
if request.user.profile.capability('canEditRightsLevel'):
if user.profile.capability('canEditRightsLevel'):
item.level = int(data['rightslevel'])
else:
response = json_response(status=403, text='permission denied')
del data['rightslevel']
if 'user' in data:
if request.user.profile.get_level() in ('admin', 'staff') and \
if user.profile.get_level() in ('admin', 'staff') and \
models.User.objects.filter(username=data['user']).exists():
new_user = models.User.objects.get(username=data['user'])
if new_user != item.user:
@ -551,10 +552,10 @@ def edit_item(request, item, data):
update_clips = True
del data['user']
if 'groups' in data:
if not request.user.profile.capability('canManageUsers'):
if not user.profile.capability('canManageUsers'):
# Users wihtout canManageUsers can only add/remove groups they are not in
groups = set([g.name for g in item.groups.all()])
user_groups = set([g.name for g in request.user.groups.all()])
user_groups = set([g.name for g in user.groups.all()])
other_groups = list(groups - user_groups)
data['groups'] = [g for g in data['groups'] if g in user_groups] + other_groups
r = item.edit(data)
@ -597,7 +598,7 @@ def add(request, data):
i.make_poster()
del data['title']
if data:
response = edit_item(request, item, data)
response = edit_item(request.user, item, data)
response['data'] = item.json()
add_changelog(request, request_data, item.public_id)
return render_to_json_response(response)
@ -619,16 +620,16 @@ def edit(request, data):
see: add, find, get, lookup, remove, upload
'''
if isinstance(data['id'], list):
items = models.Item.objects.filter(public_id__in=data['id'])
add_changelog(request, data)
t = tasks.bulk_edit.delay(data, request.user.username)
response = json_response(status=200, text='ok')
response['data']['taskId'] = t.task_id
else:
items = [get_object_or_404_json(models.Item, public_id=data['id'])]
for item in items:
item = get_object_or_404_json(models.Item, public_id=data['id'])
if item.editable(request.user):
request_data = data.copy()
response = edit_item(request, item, data)
add_changelog(request, data)
response = edit_item(request.user, item, data)
response['data'] = item.json()
if item == items[0]:
add_changelog(request, request_data)
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)

View file

@ -15,7 +15,8 @@ pandora.cleanupDate = function(value) {
};
pandora.renderRightsLevel = function(that, $rightsLevel, data, isMixed, isMultiple, canEdit) {
var rightsLevels = pandora.site.rightsLevels.map(function(rightsLevel) {
var ui = pandora.user.ui,
rightsLevels = pandora.site.rightsLevels.map(function(rightsLevel) {
return rightsLevel.name;
}).concat(isMultiple ? ['Mixed'] : []),
rightsLevel = isMixed.rightslevel ? rightsLevels.length - 1 : data.rightslevel,