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.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     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)
71
72     if not os.path.isfile(old_path):
73         raise Http404
74
75     try:
76         os.link(old_path, new_path)
77         os.unlink(old_path)
78     except OSError:
79         # destination file exists, don't overwrite it
80         # TODO: this should probably be more informative
81         return redirect(file_new, filename)
82
83     return redirect(list_new)
84
85
86 @require_POST
87 def move_to_new(request, filename):
88     """ move a unmanaged file to new files dir """
89
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)
95
96     if not os.path.isfile(old_path):
97         raise Http404
98
99     try:
100         os.link(old_path, new_path)
101         os.unlink(old_path)
102     except OSError:
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")
106
107     return redirect(list_unmanaged)
108
109 @require_POST
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()
115     audiobook.save()
116     return redirect(file_managed, id)
117
118 @require_POST
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
125         audiobook.save()
126     return redirect(file_managed, id)
127
128
129 def list_unpublished(request):
130     division = 'unpublished'
131
132     objects = models.Audiobook.objects.filter(published=None)
133     return render(request, "archive/list_unpublished.html", locals())
134
135
136
137 def file_managed(request, id):
138     audiobook = get_object_or_404(models.Audiobook, id=id)
139     division = 'published' if audiobook.published else 'unpublished'
140
141     # for tags update
142     tags = mutagen.File(audiobook.source_file.path)
143     form = AudiobookForm(instance=audiobook)
144
145     return render(request, "archive/file_managed.html", locals())
146
147
148
149 def list_published(request):
150     division = 'published'
151
152     objects = models.Audiobook.objects.exclude(published=None)
153     return render(request, "archive/list_published.html", locals())
154
155
156
157
158 def list_unmanaged(request):
159     division = 'unmanaged'
160
161     objects = sorted(os.listdir(settings.UNMANAGED_PATH))
162     return render(request, "archive/list_unmanaged.html", locals())
163
164
165 def file_unmanaged(request, filename):
166     division = 'unmanaged'
167
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())
171