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.utils.datastructures import SortedDict
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
26 def list_new(request):
29 path = settings.NEW_PATH
30 objects = sorted(os.listdir(path))
31 return render(request, "archive/list_new.html", locals())
35 def file_new(request, filename):
38 filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
40 form = AudiobookForm(request.POST)
43 form.save(path=filepath)
46 return redirect(list_new)
49 tags = mutagen.File(filepath)
55 if isinstance(value, list):
61 d[tag] = models.Project.objects.get(name=d[tag]).pk
62 except models.Project.DoesNotExist:
66 form = AudiobookForm(d)
67 return render(request, "archive/file_new.html", locals())
72 def move_to_archive(request, filename):
73 """ move a new file to the unmanaged files dir """
75 filename_str = filename.encode('utf-8')
76 old_path = os.path.join(settings.NEW_PATH, filename_str)
77 if not os.path.isdir(settings.UNMANAGED_PATH):
78 os.makedirs(settings.UNMANAGED_PATH)
79 new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
81 if not os.path.isfile(old_path):
85 os.link(old_path, new_path)
88 # destination file exists, don't overwrite it
89 # TODO: this should probably be more informative
90 return redirect(file_new, filename)
92 return redirect(list_new)
97 def move_to_new(request, filename):
98 """ move a unmanaged file to new files dir """
100 filename_str = filename.encode('utf-8')
101 old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
102 if not os.path.isdir(settings.NEW_PATH):
103 os.makedirs(settings.NEW_PATH)
104 new_path = os.path.join(settings.NEW_PATH, filename_str)
106 if not os.path.isfile(old_path):
110 os.link(old_path, new_path)
113 # destination file exists, don't overwrite it
114 # TODO: this should probably be more informative
115 return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
117 return redirect(list_unmanaged)
122 def publish(request, aid):
123 """ mark file for publishing """
124 audiobook = get_object_or_404(models.Audiobook, id=aid)
126 'name': audiobook.title,
127 'url': audiobook.url,
128 'tags': audiobook.new_publish_tags(),
130 audiobook.mp3_tags = tags
131 audiobook.ogg_tags = tags
132 audiobook.mp3_status = audiobook.ogg_status = status.WAITING
134 # isn't there a race here?
135 audiobook.mp3_task = tasks.Mp3Task.delay(aid).task_id
136 audiobook.ogg_task = tasks.OggTask.delay(aid).task_id
139 return redirect(file_managed, aid)
144 def cancel_publishing(request, aid):
145 """ cancel scheduled publishing """
146 audiobook = get_object_or_404(models.Audiobook, id=aid)
148 audiobook.mp3_status = None
149 audiobook.ogg_status = None
151 return redirect(file_managed, aid)
155 def list_unpublished(request):
156 division = 'unpublished'
158 objects = models.Audiobook.objects.filter(Q(mp3_published=None) | Q(ogg_published=None))
159 return render(request, "archive/list_unpublished.html", locals())
163 def list_publishing(request):
164 division = 'publishing'
166 objects = models.Audiobook.objects.exclude(mp3_status=None, ogg_status=None)
167 objects_by_status = SortedDict()
170 k = o.mp3_status, o.get_mp3_status_display()
171 objects_by_status.setdefault(k, []).append(o)
172 if o.ogg_status and o.ogg_status != o.mp3_status:
173 k = o.ogg_status, o.get_ogg_status_display()
174 objects_by_status.setdefault(k, []).append(o)
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)