forked from 0x2620/pandora
make moveMedia async
This commit is contained in:
parent
bea661abcf
commit
3ac08aa806
4 changed files with 73 additions and 50 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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')}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue