1 # Create your views here.
3 from datetime import datetime
7 from archive import settings
8 from django.contrib.auth import logout
9 from django.contrib.auth.decorators import login_required
10 from django.core.urlresolvers import reverse
11 from django.db.models import Q, Max
12 from django.http import Http404
13 from django.shortcuts import render, redirect, get_object_or_404
14 from django.views.decorators.http import require_POST
18 from archive.constants import status
19 from archive import models
20 from archive.forms import AudiobookForm
21 from archive import tasks
25 def list_new(request):
28 path = settings.NEW_PATH
29 objects = sorted(os.listdir(path))
30 return render(request, "archive/list_new.html", locals())
34 def file_new(request, filename):
37 filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
39 form = AudiobookForm(request.POST)
42 form.save(path=filepath)
45 return redirect(list_new)
48 tags = mutagen.File(filepath)
54 if isinstance(value, list):
60 d[tag] = models.Project.objects.get(name=d[tag]).pk
61 except models.Project.DoesNotExist:
65 form = AudiobookForm(d)
66 return render(request, "archive/file_new.html", locals())
71 def move_to_archive(request, filename):
72 """ move a new file to the unmanaged files dir """
74 filename_str = filename.encode('utf-8')
75 old_path = os.path.join(settings.NEW_PATH, filename_str)
76 if not os.path.isdir(settings.UNMANAGED_PATH):
77 os.makedirs(settings.UNMANAGED_PATH)
78 new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
80 if not os.path.isfile(old_path):
84 os.link(old_path, new_path)
87 # destination file exists, don't overwrite it
88 # TODO: this should probably be more informative
89 return redirect(file_new, filename)
91 return redirect(list_new)
96 def move_to_new(request, filename):
97 """ move a unmanaged file to new files dir """
99 filename_str = filename.encode('utf-8')
100 old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
101 if not os.path.isdir(settings.NEW_PATH):
102 os.makedirs(settings.NEW_PATH)
103 new_path = os.path.join(settings.NEW_PATH, filename_str)
105 if not os.path.isfile(old_path):
109 os.link(old_path, new_path)
112 # destination file exists, don't overwrite it
113 # TODO: this should probably be more informative
114 return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
116 return redirect(list_unmanaged)
121 def publish(request, aid):
122 """ mark file for publishing """
123 audiobook = get_object_or_404(models.Audiobook, id=aid)
125 'name': audiobook.title,
126 'url': audiobook.url,
127 'tags': audiobook.new_publish_tags(),
129 audiobook.mp3_tags = tags
130 audiobook.ogg_tags = tags
131 audiobook.mp3_status = audiobook.ogg_status = status.WAITING
133 # isn't there a race here?
134 audiobook.mp3_task = tasks.Mp3Task.delay(aid).task_id
135 audiobook.ogg_task = tasks.OggTask.delay(aid).task_id
138 return redirect(file_managed, aid)
143 def cancel_publishing(request, aid):
144 """ cancel scheduled publishing """
145 audiobook = get_object_or_404(models.Audiobook, id=aid)
147 audiobook.mp3_status = None
148 audiobook.ogg_status = None
150 return redirect(file_managed, aid)
154 def list_unpublished(request):
155 division = 'unpublished'
157 objects = models.Audiobook.objects.filter(Q(mp3_published=None) | Q(ogg_published=None))
158 return render(request, "archive/list_unpublished.html", locals())
162 def list_publishing(request):
163 division = 'publishing'
165 objects = models.Audiobook.objects.exclude(mp3_status=None, ogg_status=None)
166 objects_by_status = {}
169 k = o.mp3_status, o.get_mp3_status_display()
170 objects_by_status.setdefault(k, []).append(o)
171 if o.ogg_status and o.ogg_status != o.mp3_status:
172 k = o.ogg_status, o.get_ogg_status_display()
173 objects_by_status.setdefault(k, []).append(o)
174 status_objects = sorted(objects_by_status.items(), reverse=True)
176 return render(request, "archive/list_publishing.html", locals())
180 def list_published(request):
181 division = 'published'
183 objects = models.Audiobook.objects.exclude(Q(mp3_published=None) | Q(ogg_published=None))
184 return render(request, "archive/list_published.html", locals())
188 def file_managed(request, id):
189 audiobook = get_object_or_404(models.Audiobook, id=id)
192 form = AudiobookForm(request.POST, instance=audiobook)
199 division = 'published' if audiobook.published() else 'unpublished'
202 tags = mutagen.File(audiobook.source_file.path)
203 form = AudiobookForm(instance=audiobook)
205 return render(request, "archive/file_managed.html", locals())
209 def list_unmanaged(request):
210 division = 'unmanaged'
212 objects = sorted(os.listdir(settings.UNMANAGED_PATH))
213 return render(request, "archive/list_unmanaged.html", locals())
217 def file_unmanaged(request, filename):
218 division = 'unmanaged'
220 tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
221 err_exists = request.GET.get('exists')
222 return render(request, "archive/file_unmanaged.html", locals())
226 def logout_view(request):
228 return redirect(list_new)