remove to archive,
[audio.git] / apps / archive / views.py
index 8841dbb..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
@@ -19,6 +19,7 @@ from archive.constants import status
 from archive import models
 from archive.forms import AudiobookForm
 from archive import tasks
 from archive import models
 from archive.forms import AudiobookForm
 from archive import tasks
+from archive.utils import all_files
 
 
 @login_required
 
 
 @login_required
@@ -26,15 +27,16 @@ def list_new(request):
     division = 'new'
 
     path = settings.NEW_PATH
     division = 'new'
 
     path = settings.NEW_PATH
-    objects = sorted(os.listdir(path))
+    objects = sorted(all_files(path))
     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'
 
-    filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
+    filepath = filename.encode('utf-8')
+    root_filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
     if request.POST:
         form = AudiobookForm(request.POST)
         if form.is_valid():
     if request.POST:
         form = AudiobookForm(request.POST)
         if form.is_valid():
@@ -45,21 +47,22 @@ def file_new(request, filename):
             return redirect(list_new)
 
     try:
             return redirect(list_new)
 
     try:
-        tags = mutagen.File(filepath)
+        tags = mutagen.File(root_filepath)
     except IOError:
         raise Http404
     d = {}
     except IOError:
         raise Http404
     d = {}
-    for tag in tags:
-        value = tags[tag]
-        if isinstance(value, list):
-            d[tag] = value[0]
-        else:
-            d[tag] = value
-        if tag == 'project':
-            try:
-                d[tag] = models.Project.objects.get(name=d[tag]).pk
-            except models.Project.DoesNotExist:
-                d[tag] = None
+    if tags:
+        for tag in tags:
+            value = tags[tag]
+            if isinstance(value, list):
+                d[tag] = value[0]
+            else:
+                d[tag] = value
+            if tag == 'project':
+                try:
+                    d[tag] = models.Project.objects.get(name=d[tag]).pk
+                except models.Project.DoesNotExist:
+                    d[tag] = None
 
     if not request.POST:
         form = AudiobookForm(d)
 
     if not request.POST:
         form = AudiobookForm(d)
@@ -67,15 +70,16 @@ 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 """
 
     filename_str = filename.encode('utf-8')
     old_path = os.path.join(settings.NEW_PATH, filename_str)
 def move_to_archive(request, filename):
     """ move a new file to the unmanaged files dir """
 
     filename_str = filename.encode('utf-8')
     old_path = os.path.join(settings.NEW_PATH, filename_str)
-    if not os.path.isdir(settings.UNMANAGED_PATH):
-        os.makedirs(settings.UNMANAGED_PATH)
     new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
     new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
+    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
 
     if not os.path.isfile(old_path):
         raise Http404
@@ -92,15 +96,44 @@ 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 """
 
     filename_str = filename.encode('utf-8')
     old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
 def move_to_new(request, filename):
     """ move a unmanaged file to new files dir """
 
     filename_str = filename.encode('utf-8')
     old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
-    if not os.path.isdir(settings.NEW_PATH):
-        os.makedirs(settings.NEW_PATH)
     new_path = os.path.join(settings.NEW_PATH, filename_str)
     new_path = os.path.join(settings.NEW_PATH, filename_str)
+    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
 
     if not os.path.isfile(old_path):
         raise Http404
@@ -117,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)
@@ -139,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)
@@ -184,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)
 
@@ -197,9 +230,12 @@ def file_managed(request, id):
                 raise Http404
 
     division = 'published' if audiobook.published() else 'unpublished'
                 raise Http404
 
     division = 'published' if audiobook.published() else 'unpublished'
+    path = audiobook.source_file.path[len(settings.FILES_PATH):].lstrip('/')
 
     # for tags update
     tags = mutagen.File(audiobook.source_file.path)
 
     # for tags update
     tags = mutagen.File(audiobook.source_file.path)
+    if not tags:
+        tags = {}
     form = AudiobookForm(instance=audiobook)
 
     return render(request, "archive/file_managed.html", locals())
     form = AudiobookForm(instance=audiobook)
 
     return render(request, "archive/file_managed.html", locals())
@@ -209,7 +245,7 @@ def file_managed(request, id):
 def list_unmanaged(request):
     division = 'unmanaged'
 
 def list_unmanaged(request):
     division = 'unmanaged'
 
-    objects = sorted(os.listdir(settings.UNMANAGED_PATH))
+    objects = sorted(all_files(settings.UNMANAGED_PATH))
     return render(request, "archive/list_unmanaged.html", locals())
 
 
     return render(request, "archive/list_unmanaged.html", locals())
 
 
@@ -218,11 +254,8 @@ def file_unmanaged(request, filename):
     division = 'unmanaged'
 
     tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
     division = 'unmanaged'
 
     tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
+    if not tags:
+        tags = {}
+    
     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)