from datetime import datetime
import os
import os.path
+from urllib import quote
from archive import settings
from django.contrib.auth import logout
-from django.contrib.auth.decorators import login_required, permission_required
+from django.contrib.auth.decorators import permission_required
from django.core.urlresolvers import reverse
from django.db.models import Q, Max
-from django.http import Http404
+from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from archive.utils import all_files
-@login_required
def list_new(request):
division = 'new'
return redirect(list_new)
+@require_POST
+@permission_required('archive.change_audiobook')
+def remove_to_archive(request, aid):
+ """ move a managed file to the unmanaged files dir """
+
+ audiobook = get_object_or_404(models.Audiobook, id=aid)
+ old_path = audiobook.source_file.path
+ new_path = os.path.join(settings.UNMANAGED_PATH,
+ str(audiobook.source_file)[len(settings.FILES_SAVE_PATH):].lstrip('/'))
+ new_dir = os.path.split(new_path)[0]
+ if not os.path.isdir(new_dir):
+ os.makedirs(new_dir)
+
+ if not os.path.isfile(old_path):
+ raise Http404
+
+ try:
+ os.link(old_path, new_path)
+ except OSError:
+ # destination file exists, don't overwrite it
+ # TODO: this should probably be more informative
+ return redirect(file_new, filename)
+ else:
+ os.unlink(old_path)
+ audiobook.delete()
+
+ return redirect(list_unmanaged)
+
@require_POST
@permission_required('archive.change_audiobook')
def move_to_new(request, filename):
@require_POST
@permission_required('archive.change_audiobook')
-def publish(request, aid):
+def publish(request, aid, publish=True):
""" mark file for publishing """
audiobook = get_object_or_404(models.Audiobook, id=aid)
tags = {
audiobook.mp3_status = audiobook.ogg_status = status.WAITING
audiobook.save()
# isn't there a race here?
- audiobook.mp3_task = tasks.Mp3Task.delay(aid).task_id
- audiobook.ogg_task = tasks.OggTask.delay(aid).task_id
+ audiobook.mp3_task = tasks.Mp3Task.delay(aid, publish).task_id
+ audiobook.ogg_task = tasks.OggTask.delay(aid, publish).task_id
audiobook.save()
return redirect(file_managed, aid)
return redirect(file_managed, aid)
-@login_required
+def download(request, aid, which="source"):
+ if which not in ("source", "mp3", "ogg"):
+ raise Http404
+ audiobook = get_object_or_404(models.Audiobook, id=aid)
+ file_ = getattr(audiobook, "%s_file" % which)
+ if not file_:
+ raise Http404
+ ext = file_.path.rsplit('.', 1)[-1]
+ response = HttpResponse(mimetype='application/force-download')
+
+ response['Content-Disposition'] = "attachment; filename*=UTF-8''%s.%s" % (
+ quote(audiobook.title.encode('utf-8'), safe=''), ext)
+ response['X-Sendfile'] = file_.path.encode('utf-8')
+ return response
+
+
def list_unpublished(request):
division = 'unpublished'
return render(request, "archive/list_unpublished.html", locals())
-@login_required
def list_publishing(request):
division = 'publishing'
return render(request, "archive/list_publishing.html", locals())
-@login_required
def list_published(request):
division = 'published'
return render(request, "archive/file_managed.html", locals())
-@login_required
def list_unmanaged(request):
division = 'unmanaged'
return render(request, "archive/list_unmanaged.html", locals())
-@login_required
def file_unmanaged(request, filename):
division = 'unmanaged'