41c71eaf37ee03ae4b48194b6999c32a614664e9
[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.utils.datastructures import SortedDict
15 from django.views.decorators.http import require_POST
16
17 import mutagen
18
19 from archive.constants import status
20 from archive import models
21 from archive.forms import AudiobookForm
22 from archive import tasks
23
24
25 @login_required
26 def list_new(request):
27     division = 'new'
28
29     path = settings.NEW_PATH
30     objects = sorted(os.listdir(path))
31     return render(request, "archive/list_new.html", locals())
32
33
34 @login_required
35 def file_new(request, filename):
36     division = 'new'
37
38     filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
39     if request.POST:
40         form = AudiobookForm(request.POST)
41         if form.is_valid():
42             try:
43                 form.save(path=filepath)
44             except IOError:
45                 raise Http404
46             return redirect(list_new)
47
48     try:
49         tags = mutagen.File(filepath)
50     except IOError:
51         raise Http404
52     d = {}
53     for tag in tags:
54         value = tags[tag]
55         if isinstance(value, list):
56             d[tag] = value[0]
57         else:
58             d[tag] = value
59         if tag == 'project':
60             try:
61                 d[tag] = models.Project.objects.get(name=d[tag]).pk
62             except models.Project.DoesNotExist:
63                 d[tag] = None
64
65     if not request.POST:
66         form = AudiobookForm(d)
67     return render(request, "archive/file_new.html", locals())
68
69
70 @require_POST
71 @login_required
72 def move_to_archive(request, filename):
73     """ move a new file to the unmanaged files dir """
74
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)
80
81     if not os.path.isfile(old_path):
82         raise Http404
83
84     try:
85         os.link(old_path, new_path)
86         os.unlink(old_path)
87     except OSError:
88         # destination file exists, don't overwrite it
89         # TODO: this should probably be more informative
90         return redirect(file_new, filename)
91
92     return redirect(list_new)
93
94
95 @require_POST
96 @login_required
97 def move_to_new(request, filename):
98     """ move a unmanaged file to new files dir """
99
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)
105
106     if not os.path.isfile(old_path):
107         raise Http404
108
109     try:
110         os.link(old_path, new_path)
111         os.unlink(old_path)
112     except OSError:
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")
116
117     return redirect(list_unmanaged)
118
119
120 @require_POST
121 @login_required
122 def publish(request, aid):
123     """ mark file for publishing """
124     audiobook = get_object_or_404(models.Audiobook, id=aid)
125     tags = {
126         'name': audiobook.title,
127         'url': audiobook.url,
128         'tags': audiobook.new_publish_tags(),
129         }
130     audiobook.mp3_tags = tags
131     audiobook.ogg_tags = tags
132     audiobook.mp3_status = audiobook.ogg_status = status.WAITING
133     audiobook.save()
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
137     audiobook.save()
138
139     return redirect(file_managed, aid)
140
141
142 @require_POST
143 @login_required
144 def cancel_publishing(request, aid):
145     """ cancel scheduled publishing """
146     audiobook = get_object_or_404(models.Audiobook, id=aid)
147     # TODO: cancel tasks
148     audiobook.mp3_status = None
149     audiobook.ogg_status = None
150     audiobook.save()
151     return redirect(file_managed, aid)
152
153
154 @login_required
155 def list_unpublished(request):
156     division = 'unpublished'
157
158     objects = models.Audiobook.objects.filter(Q(mp3_published=None) | Q(ogg_published=None))
159     return render(request, "archive/list_unpublished.html", locals())
160
161
162 @login_required
163 def list_publishing(request):
164     division = 'publishing'
165
166     objects = models.Audiobook.objects.exclude(mp3_status=None, ogg_status=None)
167     objects_by_status = SortedDict()
168     for o in objects:
169         if o.mp3_status:
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)
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)