8841dbbd76d006d05e8bbfc950aa8340795ed485
[audio.git] / apps / archive / views.py
1 # Create your views here.
2
3 from datetime import datetime
4 import os
5 import os.path
6
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
15
16 import mutagen
17
18 from archive.constants import status
19 from archive import models
20 from archive.forms import AudiobookForm
21 from archive import tasks
22
23
24 @login_required
25 def list_new(request):
26     division = 'new'
27
28     path = settings.NEW_PATH
29     objects = sorted(os.listdir(path))
30     return render(request, "archive/list_new.html", locals())
31
32
33 @login_required
34 def file_new(request, filename):
35     division = 'new'
36
37     filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
38     if request.POST:
39         form = AudiobookForm(request.POST)
40         if form.is_valid():
41             try:
42                 form.save(path=filepath)
43             except IOError:
44                 raise Http404
45             return redirect(list_new)
46
47     try:
48         tags = mutagen.File(filepath)
49     except IOError:
50         raise Http404
51     d = {}
52     for tag in tags:
53         value = tags[tag]
54         if isinstance(value, list):
55             d[tag] = value[0]
56         else:
57             d[tag] = value
58         if tag == 'project':
59             try:
60                 d[tag] = models.Project.objects.get(name=d[tag]).pk
61             except models.Project.DoesNotExist:
62                 d[tag] = None
63
64     if not request.POST:
65         form = AudiobookForm(d)
66     return render(request, "archive/file_new.html", locals())
67
68
69 @require_POST
70 @login_required
71 def move_to_archive(request, filename):
72     """ move a new file to the unmanaged files dir """
73
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)
79
80     if not os.path.isfile(old_path):
81         raise Http404
82
83     try:
84         os.link(old_path, new_path)
85         os.unlink(old_path)
86     except OSError:
87         # destination file exists, don't overwrite it
88         # TODO: this should probably be more informative
89         return redirect(file_new, filename)
90
91     return redirect(list_new)
92
93
94 @require_POST
95 @login_required
96 def move_to_new(request, filename):
97     """ move a unmanaged file to new files dir """
98
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)
104
105     if not os.path.isfile(old_path):
106         raise Http404
107
108     try:
109         os.link(old_path, new_path)
110         os.unlink(old_path)
111     except OSError:
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")
115
116     return redirect(list_unmanaged)
117
118
119 @require_POST
120 @login_required
121 def publish(request, aid):
122     """ mark file for publishing """
123     audiobook = get_object_or_404(models.Audiobook, id=aid)
124     tags = {
125         'name': audiobook.title,
126         'url': audiobook.url,
127         'tags': audiobook.new_publish_tags(),
128         }
129     audiobook.mp3_tags = tags
130     audiobook.ogg_tags = tags
131     audiobook.mp3_status = audiobook.ogg_status = status.WAITING
132     audiobook.save()
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
136     audiobook.save()
137
138     return redirect(file_managed, aid)
139
140
141 @require_POST
142 @login_required
143 def cancel_publishing(request, aid):
144     """ cancel scheduled publishing """
145     audiobook = get_object_or_404(models.Audiobook, id=aid)
146     # TODO: cancel tasks
147     audiobook.mp3_status = None
148     audiobook.ogg_status = None
149     audiobook.save()
150     return redirect(file_managed, aid)
151
152
153 @login_required
154 def list_unpublished(request):
155     division = 'unpublished'
156
157     objects = models.Audiobook.objects.filter(Q(mp3_published=None) | Q(ogg_published=None))
158     return render(request, "archive/list_unpublished.html", locals())
159
160
161 @login_required
162 def list_publishing(request):
163     division = 'publishing'
164
165     objects = models.Audiobook.objects.exclude(mp3_status=None, ogg_status=None)
166     objects_by_status = {}
167     for o in objects:
168         if o.mp3_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)
175
176     return render(request, "archive/list_publishing.html", locals())
177
178
179 @login_required
180 def list_published(request):
181     division = 'published'
182
183     objects = models.Audiobook.objects.exclude(Q(mp3_published=None) | Q(ogg_published=None))
184     return render(request, "archive/list_published.html", locals())
185
186
187 @login_required
188 def file_managed(request, id):
189     audiobook = get_object_or_404(models.Audiobook, id=id)
190
191     if request.POST:
192         form = AudiobookForm(request.POST, instance=audiobook)
193         if form.is_valid():
194             try:
195                 form.save()
196             except IOError:
197                 raise Http404
198
199     division = 'published' if audiobook.published() else 'unpublished'
200
201     # for tags update
202     tags = mutagen.File(audiobook.source_file.path)
203     form = AudiobookForm(instance=audiobook)
204
205     return render(request, "archive/file_managed.html", locals())
206
207
208 @login_required
209 def list_unmanaged(request):
210     division = 'unmanaged'
211
212     objects = sorted(os.listdir(settings.UNMANAGED_PATH))
213     return render(request, "archive/list_unmanaged.html", locals())
214
215
216 @login_required
217 def file_unmanaged(request, filename):
218     division = 'unmanaged'
219
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())
223
224
225 @login_required
226 def logout_view(request):
227     logout(request)
228     return redirect(list_new)