fix
[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
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_published(request):
163     division = 'published'
164
165     objects = models.Audiobook.objects.exclude(Q(mp3_published=None) | Q(ogg_published=None))
166     return render(request, "archive/list_published.html", locals())
167
168
169 @login_required
170 def file_managed(request, id):
171     audiobook = get_object_or_404(models.Audiobook, id=id)
172
173     if request.POST:
174         form = AudiobookForm(request.POST, instance=audiobook)
175         if form.is_valid():
176             try:
177                 form.save()
178             except IOError:
179                 raise Http404
180
181     division = 'published' if audiobook.published() else 'unpublished'
182
183     # for tags update
184     tags = mutagen.File(audiobook.source_file.path)
185     form = AudiobookForm(instance=audiobook)
186
187     return render(request, "archive/file_managed.html", locals())
188
189
190 @login_required
191 def list_unmanaged(request):
192     division = 'unmanaged'
193
194     objects = sorted(os.listdir(settings.UNMANAGED_PATH))
195     return render(request, "archive/list_unmanaged.html", locals())
196
197
198 @login_required
199 def file_unmanaged(request, filename):
200     division = 'unmanaged'
201
202     tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
203     err_exists = request.GET.get('exists')
204     return render(request, "archive/file_unmanaged.html", locals())
205
206
207 @login_required
208 def logout_view(request):
209     logout(request)
210     return redirect(list_new)