1 # Create your views here.
3 from datetime import datetime
7 from archive import settings
8 from django.core.urlresolvers import reverse
9 from django.http import Http404
10 from django.shortcuts import render, redirect, get_object_or_404
11 from django.views.decorators.http import require_POST
15 from archive import models
16 from archive.forms import AudiobookForm
19 def list_new(request):
22 path = settings.NEW_PATH
23 objects = sorted(os.listdir(path))
24 return render(request, "archive/list_new.html", locals())
27 def file_new(request, filename):
30 filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
32 form = AudiobookForm(request.POST)
35 form.save(path=filepath)
38 return redirect(list_new)
41 tags = mutagen.File(filepath)
47 if isinstance(value, list):
53 d[tag] = models.Project.objects.get(name=d[tag]).pk
54 except models.Project.DoesNotExist:
58 form = AudiobookForm(d)
59 return render(request, "archive/file_new.html", locals())
63 def move_to_archive(request, filename):
64 """ move a new file to the unmanaged files dir """
66 filename_str = filename.encode('utf-8')
67 old_path = os.path.join(settings.NEW_PATH, filename_str)
68 if not os.path.isdir(settings.UNMANAGED_PATH):
69 os.makedirs(settings.UNMANAGED_PATH)
70 new_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
72 if not os.path.isfile(old_path):
76 os.link(old_path, new_path)
79 # destination file exists, don't overwrite it
80 # TODO: this should probably be more informative
81 return redirect(file_new, filename)
83 return redirect(list_new)
87 def move_to_new(request, filename):
88 """ move a unmanaged file to new files dir """
90 filename_str = filename.encode('utf-8')
91 old_path = os.path.join(settings.UNMANAGED_PATH, filename_str)
92 if not os.path.isdir(settings.NEW_PATH):
93 os.makedirs(settings.NEW_PATH)
94 new_path = os.path.join(settings.NEW_PATH, filename_str)
96 if not os.path.isfile(old_path):
100 os.link(old_path, new_path)
103 # destination file exists, don't overwrite it
104 # TODO: this should probably be more informative
105 return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
107 return redirect(list_unmanaged)
110 def publish(request, id):
111 """ mark file for publishing """
112 audiobook = get_object_or_404(models.Audiobook, id=id)
113 audiobook.publish_wait = datetime.now()
114 audiobook.publishing_tags = audiobook.new_publish_tags()
116 return redirect(file_managed, id)
119 def cancel_publishing(request, id):
120 """ cancel scheduled publishing """
121 audiobook = get_object_or_404(models.Audiobook, id=id)
122 if not audiobook.publishing:
123 audiobook.publish_wait = None
124 audiobook.publishing_tags = None
126 return redirect(file_managed, id)
129 def list_unpublished(request):
130 division = 'unpublished'
132 objects = models.Audiobook.objects.filter(published=None)
133 return render(request, "archive/list_unpublished.html", locals())
137 def file_managed(request, id):
138 audiobook = get_object_or_404(models.Audiobook, id=id)
139 division = 'published' if audiobook.published else 'unpublished'
142 tags = mutagen.File(audiobook.source_file.path)
143 form = AudiobookForm(instance=audiobook)
145 return render(request, "archive/file_managed.html", locals())
149 def list_published(request):
150 division = 'published'
152 objects = models.Audiobook.objects.exclude(published=None)
153 return render(request, "archive/list_published.html", locals())
158 def list_unmanaged(request):
159 division = 'unmanaged'
161 objects = sorted(os.listdir(settings.UNMANAGED_PATH))
162 return render(request, "archive/list_unmanaged.html", locals())
165 def file_unmanaged(request, filename):
166 division = 'unmanaged'
168 tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename.encode('utf-8')))
169 err_exists = request.GET.get('exists')
170 return render(request, "archive/file_unmanaged.html", locals())