df8fb77c551f177edbc6d87ad2165960997a1c32
[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.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
12
13 import mutagen
14
15 from archive import models
16 from archive.forms import AudiobookForm
17
18
19 def list_new(request):
20     division = 'new'
21
22     path = settings.NEW_PATH
23     objects = sorted(os.listdir(path))
24     return render(request, "archive/list_new.html", locals())
25
26
27 def file_new(request, filename):
28     division = 'new'
29
30     filepath = os.path.join(settings.NEW_PATH, filename.encode('utf-8'))
31     if request.POST:
32         form = AudiobookForm(request.POST)
33         if form.is_valid():
34             try:
35                 form.save(path=filepath)
36             except IOError:
37                 raise Http404
38             return redirect(list_new)
39
40     try:
41         tags = mutagen.File(filepath)
42     except IOError:
43         raise Http404
44     d = {}
45     for tag in tags:
46         value = tags[tag]
47         if isinstance(value, list):
48             d[tag] = value[0]
49         else:
50             d[tag] = value
51         if tag == 'project':
52             try:
53                 d[tag] = models.Project.objects.get(name=d[tag]).pk
54             except models.Project.DoesNotExist:
55                 d[tag] = None
56
57     if not request.POST:
58         form = AudiobookForm(d)
59     return render(request, "archive/file_new.html", locals())
60
61
62 @require_POST
63 def move_to_archive(request, filename):
64     """ move a new file to the unmanaged files dir """
65
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)
70
71     if not os.path.isfile(old_path):
72         raise Http404
73
74     try:
75         os.link(old_path, new_path)
76         os.unlink(old_path)
77     except OSError:
78         # destination file exists, don't overwrite it
79         # TODO: this should probably be more informative
80         return redirect(file_new, filename)
81
82     return redirect(list_new)
83
84
85 @require_POST
86 def move_to_new(request, filename):
87     """ move a unmanaged file to new files dir """
88
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)
93
94     if not os.path.isfile(old_path):
95         raise Http404
96
97     try:
98         os.link(old_path, new_path)
99         os.unlink(old_path)
100     except OSError:
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")
104
105     return redirect(list_unmanaged)
106
107 @require_POST
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()
113     audiobook.save()
114     return redirect(file_managed, id)
115
116 @require_POST
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
123         audiobook.save()
124     return redirect(file_managed, id)
125
126
127 def list_unpublished(request):
128     division = 'unpublished'
129
130     objects = models.Audiobook.objects.filter(published=None)
131     return render(request, "archive/list_unpublished.html", locals())
132
133
134
135 def file_managed(request, id):
136     audiobook = get_object_or_404(models.Audiobook, id=id)
137     division = 'published' if audiobook.published else 'unpublished'
138
139     # for tags update
140     tags = mutagen.File(audiobook.source_file.path)
141     form = AudiobookForm(instance=audiobook)
142
143     return render(request, "archive/file_managed.html", locals())
144
145
146
147 def list_published(request):
148     division = 'published'
149
150     objects = models.Audiobook.objects.exclude(published=None)
151     return render(request, "archive/list_published.html", locals())
152
153
154
155
156 def list_unmanaged(request):
157     division = 'unmanaged'
158
159     objects = sorted(os.listdir(settings.UNMANAGED_PATH))
160     return render(request, "archive/list_unmanaged.html", locals())
161
162
163 def file_unmanaged(request, filename):
164     division = 'unmanaged'
165
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())
169