1 # Create your views here.
3 from datetime import datetime
6 from urllib import quote
8 from archive import settings
9 from django.contrib.auth import logout
10 from django.contrib.auth.decorators import permission_required
11 from django.core.urlresolvers import reverse
12 from django.db.models import Q, Max
13 from django.http import Http404, HttpResponse
14 from django.shortcuts import render, redirect, get_object_or_404
15 from django.views.decorators.http import require_POST
19 from archive.constants import status
20 from archive import models
21 from archive.forms import AudiobookForm
22 from archive import tasks
23 from archive.utils import all_files
26 def list_new(request):
29 path = settings.NEW_PATH
30 objects = sorted(all_files(path))
31 return render(request, "archive/list_new.html", locals())
34 @permission_required('archive.change_audiobook')
35 def file_new(request, filename):
39 root_filepath = os.path.join(settings.NEW_PATH, filename)
41 form = AudiobookForm(request.POST)
44 form.save(path=filepath)
47 return redirect(list_new)
50 tags = mutagen.File(root_filepath)
57 if isinstance(value, list):
63 d[tag] = models.Project.objects.get(name=d[tag]).pk
64 except models.Project.DoesNotExist:
68 form = AudiobookForm(initial=d)
69 return render(request, "archive/file_new.html", locals())
73 @permission_required('archive.change_audiobook')
74 def move_to_archive(request, filename):
75 """ move a new file to the unmanaged files dir """
77 filename_str = filename.encode('utf-8')
78 old_path = os.path.join(settings.NEW_PATH, filename_str)
79 new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
80 new_dir = os.path.split(new_path)[0]
81 if not os.path.isdir(new_dir):
84 if not os.path.isfile(old_path):
88 os.link(old_path, new_path)
91 # destination file exists, don't overwrite it
92 # TODO: this should probably be more informative
93 return redirect(file_new, filename)
95 return redirect(list_new)
99 @permission_required('archive.change_audiobook')
100 def remove_to_archive(request, aid):
101 """ move a managed file to the unmanaged files dir """
103 audiobook = get_object_or_404(models.Audiobook, id=aid)
104 old_path = audiobook.source_file.path
105 new_path = os.path.join(settings.UNMANAGED_PATH,
106 str(audiobook.source_file)[len(settings.FILES_SAVE_PATH):].lstrip('/'))
107 new_dir = os.path.split(new_path)[0]
108 if not os.path.isdir(new_dir):
111 if not os.path.isfile(old_path):
115 try_new_path = new_path
119 os.link(old_path, try_new_path)
121 # destination file exists, don't overwrite it
123 parts = new_path.rsplit('.', 1)
124 parts[0] += '_%d' % try_number
125 try_new_path = ".".join(parts)
131 return redirect(list_unmanaged)
134 @permission_required('archive.change_audiobook')
135 def move_to_new(request, filename):
136 """ move a unmanaged file to new files dir """
138 filename_str = filename.encode('utf-8')
139 old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
140 new_path = os.path.join(settings.NEW_PATH, filename_str)
141 new_dir = os.path.split(new_path)[0]
142 if not os.path.isdir(new_dir):
145 if not os.path.isfile(old_path):
149 os.link(old_path, new_path)
152 # destination file exists, don't overwrite it
153 # TODO: this should probably be more informative
154 return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
156 return redirect(list_unmanaged)
160 @permission_required('archive.change_audiobook')
161 def publish(request, aid, publish=True):
162 """ mark file for publishing """
163 audiobook = get_object_or_404(models.Audiobook, id=aid)
165 'name': audiobook.title,
166 'url': audiobook.url,
167 'tags': audiobook.new_publish_tags(),
169 audiobook.mp3_tags = tags
170 audiobook.ogg_tags = tags
171 audiobook.mp3_status = audiobook.ogg_status = status.WAITING
173 # isn't there a race here?
174 audiobook.mp3_task = tasks.Mp3Task.delay(request.user.id, aid, publish).task_id
175 audiobook.ogg_task = tasks.OggTask.delay(request.user.id, aid, publish).task_id
178 return redirect(file_managed, aid)
182 @permission_required('archive.change_audiobook')
183 def cancel_publishing(request, aid):
184 """ cancel scheduled publishing """
185 audiobook = get_object_or_404(models.Audiobook, id=aid)
187 audiobook.mp3_status = None
188 audiobook.ogg_status = None
190 return redirect(file_managed, aid)
193 def download(request, aid, which="source"):
194 if which not in ("source", "mp3", "ogg"):
196 audiobook = get_object_or_404(models.Audiobook, id=aid)
197 file_ = getattr(audiobook, "%s_file" % which)
200 ext = file_.path.rsplit('.', 1)[-1]
201 response = HttpResponse(mimetype='application/force-download')
203 response['Content-Disposition'] = "attachment; filename*=UTF-8''%s.%s" % (
204 quote(audiobook.title.encode('utf-8'), safe=''), ext)
205 response['X-Sendfile'] = file_.path.encode('utf-8')
209 def list_unpublished(request):
210 division = 'unpublished'
212 objects = models.Audiobook.objects.filter(Q(mp3_published=None) | Q(ogg_published=None))
213 return render(request, "archive/list_unpublished.html", locals())
216 def list_publishing(request):
217 division = 'publishing'
219 objects = models.Audiobook.objects.exclude(mp3_status=None, ogg_status=None)
220 objects_by_status = {}
223 k = o.mp3_status, o.get_mp3_status_display()
224 objects_by_status.setdefault(k, []).append(o)
225 if o.ogg_status and o.ogg_status != o.mp3_status:
226 k = o.ogg_status, o.get_ogg_status_display()
227 objects_by_status.setdefault(k, []).append(o)
228 status_objects = sorted(objects_by_status.items(), reverse=True)
230 return render(request, "archive/list_publishing.html", locals())
233 def list_published(request):
234 division = 'published'
236 objects = models.Audiobook.objects.exclude(Q(mp3_published=None) | Q(ogg_published=None))
237 return render(request, "archive/list_published.html", locals())
240 @permission_required('archive.change_audiobook')
241 def file_managed(request, id):
242 audiobook = get_object_or_404(models.Audiobook, id=id)
245 form = AudiobookForm(request.POST, instance=audiobook)
252 division = 'published' if audiobook.published() else 'unpublished'
253 path = audiobook.source_file.path[len(settings.FILES_PATH):].lstrip('/')
256 tags = mutagen.File(audiobook.source_file.path.encode('utf-8'))
259 form = AudiobookForm(instance=audiobook)
262 request.user.is_authenticated and
263 request.user.oauthconnection_set.filter(access=True).exists())
265 return render(request, "archive/file_managed.html", locals())
268 def list_unmanaged(request):
269 division = 'unmanaged'
271 objects = sorted(all_files(settings.UNMANAGED_PATH))
272 return render(request, "archive/list_unmanaged.html", locals())
275 def file_unmanaged(request, filename):
276 division = 'unmanaged'
278 tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
282 err_exists = request.GET.get('exists')
283 return render(request, "archive/file_unmanaged.html", locals())