From 3ac08aa8069c319608cda31082572bd214a25ba4 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 8 Sep 2016 00:29:31 +0200 Subject: [PATCH] make moveMedia async --- pandora/archive/tasks.py | 46 ++++++++++++++++++++++++++++++++++++- pandora/archive/views.py | 43 +++++++--------------------------- pandora/changelog/models.py | 8 +++++-- static/js/mediaView.js | 26 +++++++++++---------- 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/pandora/archive/tasks.py b/pandora/archive/tasks.py index a924d462..d5f99f16 100644 --- a/pandora/archive/tasks.py +++ b/pandora/archive/tasks.py @@ -4,8 +4,8 @@ from __future__ import division, print_function, absolute_import from glob import glob +from six import string_types from celery.task import task - from django.conf import settings from django.db.models import Q @@ -201,3 +201,47 @@ def update_stream(id): @task(queue="encoding") def download_media(item_id, url): return external.download(item_id, url) + +@task(queue='default') +def move_media(data, user): + user = models.User.objects.get(username=user) + from changelog.models import add_changelog + from item.models import get_item, Item + import item.tasks + if Item.objects.filter(public_id=data['item']).count() == 1: + i = Item.objects.get(public_id=data['item']) + else: + data['public_id'] = data.pop('item').strip() + if len(data['public_id']) != 7: + del data['public_id'] + if 'director' in data and isinstance(data['director'], string_types): + if data['director'] == '': + data['director'] = [] + else: + data['director'] = data['director'].split(', ') + i = get_item(data, user=user) + else: + i = get_item({'imdbId': data['public_id']}, user=user) + changed = [i.public_id] + for f in models.File.objects.filter(oshash__in=data['ids']): + if f.item.id != i.public_id and f.editable(user): + if f.item.public_id not in changed: + changed.append(f.item.public_id) + f.item = i + f.save() + for public_id in changed: + c = Item.objects.get(public_id=public_id) + if c.files.count() == 0 and settings.CONFIG['itemRequiresVideo']: + c.delete() + else: + c.rendered = False + c.save() + Task.start(c, user) + item.tasks.update_timeline.delay(public_id) + add_changelog({ + 'user': user, + 'action': 'moveMedia' + }, data, i.public_id) + return { + 'item': i.public_id + } diff --git a/pandora/archive/views.py b/pandora/archive/views.py index 93160273..b770aea0 100644 --- a/pandora/archive/views.py +++ b/pandora/archive/views.py @@ -378,9 +378,12 @@ def getTaskStatus(request, data): res = default_backend.get_result(task_id) response['data'] = { 'id': task_id, - 'status': status, - 'result': res + 'status': status } + if isinstance(res, dict): + response['data'].update(res) + else: + response['data']['result'] = res if status in default_backend.EXCEPTION_STATES: traceback = default_backend.get_traceback(task_id) response['data'].update({ @@ -405,39 +408,9 @@ def moveMedia(request, data): notes: This will *not* (yet) shift the corresponding annotations. see: addMedia, editMedia, findMedia, removeMedia ''' - if Item.objects.filter(public_id=data['item']).count() == 1: - i = Item.objects.get(public_id=data['item']) - else: - data['public_id'] = data.pop('item').strip() - if len(data['public_id']) != 7: - del data['public_id'] - if 'director' in data and isinstance(data['director'], string_types): - if data['director'] == '': - data['director'] = [] - else: - data['director'] = data['director'].split(', ') - i = get_item(data, user=request.user) - else: - i = get_item({'imdbId': data['public_id']}, user=request.user) - changed = [i.public_id] - for f in models.File.objects.filter(oshash__in=data['ids']): - if f.item.id != i.public_id and f.editable(request.user): - if f.item.public_id not in changed: - changed.append(f.item.public_id) - f.item = i - f.save() - for public_id in changed: - c = Item.objects.get(public_id=public_id) - if c.files.count() == 0 and settings.CONFIG['itemRequiresVideo']: - c.delete() - else: - c.rendered = False - c.save() - Task.start(c, request.user) - item.tasks.update_timeline.delay(public_id) - response = json_response(text='updated') - response['data']['item'] = i.public_id - add_changelog(request, data, i.public_id) + response = json_response() + t = tasks.move_media.delay(data, request.user.username) + response['data']['taskId'] = t.task_id return render_to_json_response(response) actions.register(moveMedia, cache=False) diff --git a/pandora/changelog/models.py b/pandora/changelog/models.py index 0083ef6e..a40057a8 100644 --- a/pandora/changelog/models.py +++ b/pandora/changelog/models.py @@ -28,8 +28,12 @@ class Changelog(models.Model): return self.value def add_changelog(request, data, id=None): - user = request.user - action = request.POST['action'] + if isinstance(request, dict): + user = request['user'] + action = request['action'] + else: + user = request.user + action = request.POST['action'] c = Log(user=user, action=action, data=data) if id and isinstance(id, list): id = ', '.join(id) diff --git a/static/js/mediaView.js b/static/js/mediaView.js index e5400676..3da11c0b 100644 --- a/static/js/mediaView.js +++ b/static/js/mediaView.js @@ -550,19 +550,21 @@ pandora.ui.mediaView = function(options) { {disabled: true, title: Ox._('Moving Files...')} ); pandora.api.moveMedia(data, function(result) { - if (isActive()) { - Ox.Request.clearCache(); // fixme: remove - if (self.$switch.value()) { - pandora.UI.set({item: result.data.item}); - pandora.updateItemContext(); - } else { - self.$filesList.reloadList(); - self.$instancesList.reloadList(); - self.$moveButton.options( - {disabled: false, title: Ox._('Move Files')} - ); + pandora.wait(result.data.taskId, function(result) { + if (isActive()) { + Ox.Request.clearCache(); // fixme: remove + if (self.$switch.value()) { + pandora.UI.set({item: result.data.item}); + pandora.updateItemContext(); + } else { + self.$filesList.reloadList(); + self.$instancesList.reloadList(); + self.$moveButton.options( + {disabled: false, title: Ox._('Move Files')} + ); + } } - } + }); }); }