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
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_published(request):
163 division = 'published'
165 objects = models.Audiobook.objects.exclude(Q(mp3_published=None) | Q(ogg_published=None))
166 return render(request, "archive/list_published.html", locals())
170 def file_managed(request, id):
171 audiobook = get_object_or_404(models.Audiobook, id=id)
174 form = AudiobookForm(request.POST, instance=audiobook)
181 division = 'published' if audiobook.published() else 'unpublished'
184 tags = mutagen.File(audiobook.source_file.path)
185 form = AudiobookForm(instance=audiobook)
187 return render(request, "archive/file_managed.html", locals())
191 def list_unmanaged(request):
192 division = 'unmanaged'
194 objects = sorted(os.listdir(settings.UNMANAGED_PATH))
195 return render(request, "archive/list_unmanaged.html", locals())
199 def file_unmanaged(request, filename):
200 division = 'unmanaged'
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())
208 def logout_view(request):
210 return redirect(list_new)