remove to archive,
[audio.git] / apps / archive / views.py
index cfed4a6..59239d2 100644 (file)
@@ -6,7 +6,7 @@ import os.path
 
 from archive import settings
 from django.contrib.auth import logout
 
 from archive import settings
 from django.contrib.auth import logout
-from django.contrib.auth.decorators import login_required
+from django.contrib.auth.decorators import login_required, permission_required
 from django.core.urlresolvers import reverse
 from django.db.models import Q, Max
 from django.http import Http404
 from django.core.urlresolvers import reverse
 from django.db.models import Q, Max
 from django.http import Http404
@@ -31,7 +31,7 @@ def list_new(request):
     return render(request, "archive/list_new.html", locals())
 
 
     return render(request, "archive/list_new.html", locals())
 
 
-@login_required
+@permission_required('archive.change_audiobook')
 def file_new(request, filename):
     division = 'new'
 
 def file_new(request, filename):
     division = 'new'
 
@@ -70,7 +70,7 @@ def file_new(request, filename):
 
 
 @require_POST
 
 
 @require_POST
-@login_required
+@permission_required('archive.change_audiobook')
 def move_to_archive(request, filename):
     """ move a new file to the unmanaged files dir """
 
 def move_to_archive(request, filename):
     """ move a new file to the unmanaged files dir """
 
@@ -96,7 +96,35 @@ def move_to_archive(request, filename):
 
 
 @require_POST
 
 
 @require_POST
-@login_required
+@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):
     """ move a unmanaged file to new files dir """
 
 def move_to_new(request, filename):
     """ move a unmanaged file to new files dir """
 
@@ -122,7 +150,7 @@ def move_to_new(request, filename):
 
 
 @require_POST
 
 
 @require_POST
-@login_required
+@permission_required('archive.change_audiobook')
 def publish(request, aid):
     """ mark file for publishing """
     audiobook = get_object_or_404(models.Audiobook, id=aid)
 def publish(request, aid):
     """ mark file for publishing """
     audiobook = get_object_or_404(models.Audiobook, id=aid)
@@ -144,7 +172,7 @@ def publish(request, aid):
 
 
 @require_POST
 
 
 @require_POST
-@login_required
+@permission_required('archive.change_audiobook')
 def cancel_publishing(request, aid):
     """ cancel scheduled publishing """
     audiobook = get_object_or_404(models.Audiobook, id=aid)
 def cancel_publishing(request, aid):
     """ cancel scheduled publishing """
     audiobook = get_object_or_404(models.Audiobook, id=aid)
@@ -189,7 +217,7 @@ def list_published(request):
     return render(request, "archive/list_published.html", locals())
 
 
     return render(request, "archive/list_published.html", locals())
 
 
-@login_required
+@permission_required('archive.change_audiobook')
 def file_managed(request, id):
     audiobook = get_object_or_404(models.Audiobook, id=id)
 
 def file_managed(request, id):
     audiobook = get_object_or_404(models.Audiobook, id=id)
 
@@ -231,9 +259,3 @@ def file_unmanaged(request, filename):
     
     err_exists = request.GET.get('exists')
     return render(request, "archive/file_unmanaged.html", locals())
     
     err_exists = request.GET.get('exists')
     return render(request, "archive/file_unmanaged.html", locals())
-
-
-@login_required
-def logout_view(request):
-    logout(request)
-    return redirect(list_new)