54bb28f63a0719ed20fc5c74feb3b74fb1c22cdc
[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)
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         print d
57
58     if not request.POST:
59         form = AudiobookForm(d)
60     return render(request, "archive/file_new.html", locals())
61
62
63 @require_POST
64 def move_to_archive(request, filename):
65     """ move a new file to the unmanaged files dir """
66
67     old_path = os.path.join(settings.NEW_PATH, filename)
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)
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     old_path = os.path.join(settings.UNMANAGED_PATH, filename)
91     if not os.path.isdir(settings.NEW_PATH):
92         os.makedirs(settings.NEW_PATH)
93     new_path = os.path.join(settings.NEW_PATH, filename)
94
95     if not os.path.isfile(old_path):
96         raise Http404
97
98     try:
99         os.link(old_path, new_path)
100         os.unlink(old_path)
101     except OSError:
102         # destination file exists, don't overwrite it
103         # TODO: this should probably be more informative
104         return redirect(reverse(file_unmanaged, args=[filename]) + "?exists=1")
105
106     return redirect(list_unmanaged)
107
108 @require_POST
109 def publish(request, id):
110     """ mark file for publishing """
111     audiobook = get_object_or_404(models.Audiobook, id=id)
112     audiobook.publish_wait = datetime.now()
113     audiobook.publishing_tags = audiobook.new_publish_tags()
114     audiobook.save()
115     return redirect(file_managed, id)
116
117 @require_POST
118 def cancel_publishing(request, id):
119     """ cancel scheduled publishing """
120     audiobook = get_object_or_404(models.Audiobook, id=id)
121     if not audiobook.publishing:
122         audiobook.publish_wait = None
123         audiobook.publishing_tags = None
124         audiobook.save()
125     return redirect(file_managed, id)
126
127
128 def list_unpublished(request):
129     division = 'unpublished'
130
131     objects = models.Audiobook.objects.filter(published=None)
132     return render(request, "archive/list_unpublished.html", locals())
133
134
135
136 def file_managed(request, id):
137     audiobook = get_object_or_404(models.Audiobook, id=id)
138     division = 'published' if audiobook.published else 'unpublished'
139
140     # for tags update
141     tags = mutagen.File(audiobook.source_file.path)
142     form = AudiobookForm(instance=audiobook)
143
144     return render(request, "archive/file_managed.html", locals())
145
146
147
148 def list_published(request):
149     division = 'published'
150
151     objects = models.Audiobook.objects.exclude(published=None)
152     return render(request, "archive/list_published.html", locals())
153
154
155
156
157 def list_unmanaged(request):
158     division = 'unmanaged'
159
160     objects = sorted(os.listdir(settings.UNMANAGED_PATH))
161     return render(request, "archive/list_unmanaged.html", locals())
162
163
164 def file_unmanaged(request, filename):
165     division = 'unmanaged'
166
167     tags = mutagen.File(os.path.join(settings.UNMANAGED_PATH, filename))
168     err_exists = request.GET.get('exists')
169     return render(request, "archive/file_unmanaged.html", locals())
170