1 # Create your views here.
3 from datetime import datetime
7 from archive import settings
8 from django.core.urlresolvers import reverse
9 from django.http import Http404
10 from django.shortcuts import render, redirect, get_object_or_404
11 from django.views.decorators.http import require_POST
15 from archive import models
16 from archive.forms import AudiobookForm
19 def list_new(request):
22 path = settings.NEW_PATH
23 objects = sorted(os.listdir(path))
24 return render(request, "archive/list_new.html", locals())
27 def file_new(request, filename):
30 filepath = os.path.join(settings.NEW_PATH, filename)
32 form = AudiobookForm(request.POST)
35 form.save(path=filepath)
38 return redirect(list_new)
41 tags = mutagen.File(filepath)
47 if isinstance(value, list):
53 d[tag] = models.Project.objects.get(name=d[tag]).pk
54 except models.Project.DoesNotExist:
59 form = AudiobookForm(d)
60 return render(request, "archive/file_new.html", locals())
64 def move_to_archive(request, filename):
65 """ move a new file to the unmanaged files dir """
67 old_path = os.path.join(settings.NEW_PATH, filename)
68 if not os.path.isdir(settings.UNMANAGED_PATH):
69 os.makedirs(settings.UNMANAGED_PATH)
70 new_path = os.path.join(settings.UNMANAGED_PATH, filename)
72 if not os.path.isfile(old_path):
76 os.link(old_path, new_path)
79 # destination file exists, don't overwrite it
80 # TODO: this should probably be more informative
81 return redirect(file_new, filename)
83 return redirect(list_new)
87 def move_to_new(request, filename):
88 """ move a unmanaged file to new files dir """
90 old_path = os.path.join(settings.UNMANAGED_PATH, filename)
91 if not os.path.isdir(settings.NEW_PATH):
92 os.makedirs(settings.NEW_PATH)
93 new_path = os.path.join(settings.NEW_PATH, filename)
95 if not os.path.isfile(old_path):
99 os.link(old_path, new_path)
102 # destination file exists, don't overwrite it
103 # TODO: this should probably be more informative
104 return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
106 return redirect(list_unmanaged)
109 def publish(request, id):
110 """ mark file for publishing """
111 audiobook = get_object_or_404(models.Audiobook, id=id)
112 audiobook.publish_wait = datetime.now()
113 audiobook.publishing_tags = audiobook.new_publish_tags()
115 return redirect(file_managed, id)
118 def cancel_publishing(request, id):
119 """ cancel scheduled publishing """
120 audiobook = get_object_or_404(models.Audiobook, id=id)
121 if not audiobook.publishing:
122 audiobook.publish_wait = None
123 audiobook.publishing_tags = None
125 return redirect(file_managed, id)
128 def list_unpublished(request):
129 division = 'unpublished'
131 objects = models.Audiobook.objects.filter(published=None)
132 return render(request, "archive/list_unpublished.html", locals())
136 def file_managed(request, id):
137 audiobook = get_object_or_404(models.Audiobook, id=id)
138 division = 'published' if audiobook.published else 'unpublished'
141 tags = mutagen.File(audiobook.source_file.path)
142 form = AudiobookForm(instance=audiobook)
144 return render(request, "archive/file_managed.html", locals())
148 def list_published(request):
149 division = 'published'
151 objects = models.Audiobook.objects.exclude(published=None)
152 return render(request, "archive/list_published.html", locals())
157 def list_unmanaged(request):
158 division = 'unmanaged'
160 objects = sorted(os.listdir(settings.UNMANAGED_PATH))
161 return render(request, "archive/list_unmanaged.html", locals())
164 def file_unmanaged(request, filename):
165 division = 'unmanaged'
167 tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename))
168 err_exists = request.GET.get('exists')
169 return render(request, "archive/file_unmanaged.html", locals())