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)
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 old_path = os.path.join(settings.NEW_PATH, filename)
67 if not os.path.isdir(settings.UNMANAGED_PATH):
68 os.makedirs(settings.UNMANAGED_PATH)
69 new_path = os.path.join(settings.UNMANAGED_PATH, filename)
71 if not os.path.isfile(old_path):
75 os.link(old_path, new_path)
78 # destination file exists, don't overwrite it
79 # TODO: this should probably be more informative
80 return redirect(file_new, filename)
82 return redirect(list_new)
86 def move_to_new(request, filename):
87 """ move a unmanaged file to new files dir """
89 old_path = os.path.join(settings.UNMANAGED_PATH, filename)
90 if not os.path.isdir(settings.NEW_PATH):
91 os.makedirs(settings.NEW_PATH)
92 new_path = os.path.join(settings.NEW_PATH, filename)
94 if not os.path.isfile(old_path):
98 os.link(old_path, new_path)
101 # destination file exists, don't overwrite it
102 # TODO: this should probably be more informative
103 return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
105 return redirect(list_unmanaged)
108 def publish(request, id):
109 """ mark file for publishing """
110 audiobook = get_object_or_404(models.Audiobook, id=id)
111 audiobook.publish_wait = datetime.now()
112 audiobook.publishing_tags = audiobook.new_publish_tags()
114 return redirect(file_managed, id)
117 def cancel_publishing(request, id):
118 """ cancel scheduled publishing """
119 audiobook = get_object_or_404(models.Audiobook, id=id)
120 if not audiobook.publishing:
121 audiobook.publish_wait = None
122 audiobook.publishing_tags = None
124 return redirect(file_managed, id)
127 def list_unpublished(request):
128 division = 'unpublished'
130 objects = models.Audiobook.objects.filter(published=None)
131 return render(request, "archive/list_unpublished.html", locals())
135 def file_managed(request, id):
136 audiobook = get_object_or_404(models.Audiobook, id=id)
137 division = 'published' if audiobook.published else 'unpublished'
140 tags = mutagen.File(audiobook.source_file.path)
141 form = AudiobookForm(instance=audiobook)
143 return render(request, "archive/file_managed.html", locals())
147 def list_published(request):
148 division = 'published'
150 objects = models.Audiobook.objects.exclude(published=None)
151 return render(request, "archive/list_published.html", locals())
156 def list_unmanaged(request):
157 division = 'unmanaged'
159 objects = sorted(os.listdir(settings.UNMANAGED_PATH))
160 return render(request, "archive/list_unmanaged.html", locals())
163 def file_unmanaged(request, filename):
164 division = 'unmanaged'
166 tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename))
167 err_exists = request.GET.get('exists')
168 return render(request, "archive/file_unmanaged.html", locals())