Commit 83e20f1d53026ddbc985e3b6ad5a94b10310f3f7
1 parent
4232560b
Exists in
master
and in
3 other branches
create file is working, removing and updateing as well but still not downloading pdf file
Showing
5 changed files
with
256 additions
and
4 deletions
Show diff stats
pdf_file/models.py
1 | 1 | from django.db import models |
2 | 2 | from django.utils.translation import ugettext_lazy as _ |
3 | +import os | |
4 | +from django.core.exceptions import ValidationError | |
3 | 5 | |
4 | 6 | from topics.models import Resource |
5 | 7 | # Create your models here. |
... | ... | @@ -13,7 +15,7 @@ def validate_file_extension(value): |
13 | 15 | raise ValidationError(_('File not supported, use PDF format instead.')) |
14 | 16 | |
15 | 17 | class PDFFile(Resource): |
16 | - file = models.FileField(_('File'), upload_to='/files', validators = [validate_file_extension]) | |
18 | + file = models.FileField(_('File'), upload_to='files/', validators = [validate_file_extension]) | |
17 | 19 | class Meta: |
18 | 20 | verbose_name = "PDFFile" |
19 | 21 | verbose_name_plural = "PDFFiles" | ... | ... |
pdf_file/templates/pdf_file/create.html
... | ... | @@ -17,8 +17,8 @@ |
17 | 17 | |
18 | 18 | {% breadcrumb topic 'subjects:topic_view' topic.subject.slug topic.slug %} |
19 | 19 | |
20 | - {% trans 'Create File Link' as bread %} | |
21 | - {% breadcrumb bread 'file_links:create' topic.slug %} | |
20 | + {% trans 'Create PDF file' as bread %} | |
21 | + {% breadcrumb bread 'pdf_file:create' topic.slug %} | |
22 | 22 | {% endblock %} |
23 | 23 | |
24 | 24 | {% block content %} | ... | ... |
... | ... | @@ -0,0 +1,36 @@ |
1 | +{% extends 'subjects/view.html' %} | |
2 | + | |
3 | +{% load static i18n django_bootstrap_breadcrumbs %} | |
4 | + | |
5 | +{% block style %} | |
6 | + {{block.super}} | |
7 | + <link rel="stylesheet" type="text/css" href="{% static "css/bootstrap-tagsinput.css" %}"> | |
8 | +{% endblock %} | |
9 | + | |
10 | +{% block javascript %} | |
11 | + {{block.super}} | |
12 | + <script type="text/javascript" src="{% static "js/bootstrap-tagsinput.js" %} "></script> | |
13 | +{% endblock %} | |
14 | + | |
15 | +{% block breadcrumbs %} | |
16 | + {{ block.super }} | |
17 | + | |
18 | + {% breadcrumb topic 'subjects:topic_view' topic.subject.slug topic.slug %} | |
19 | + | |
20 | + {% trans 'Edit: ' as bread %} | |
21 | + {% with bread|add:pdf_file.name as bread_slug %} | |
22 | + {% breadcrumb bread_slug 'pdf_file:update' topic.slug pdf_file.slug %} | |
23 | + {% endwith %} | |
24 | +{% endblock %} | |
25 | + | |
26 | +{% block content %} | |
27 | + <div class="card"> | |
28 | + <div class="card-content"> | |
29 | + <div class="card-body"> | |
30 | + {% include 'pdf_file/_form.html' %} | |
31 | + </div> | |
32 | + </div> | |
33 | + </div> | |
34 | + <br clear="all" /> | |
35 | + <br clear="all" /> | |
36 | +{% endblock %} | |
0 | 37 | \ No newline at end of file | ... | ... |
pdf_file/urls.py
... | ... | @@ -5,4 +5,7 @@ from . import views |
5 | 5 | |
6 | 6 | urlpatterns = [ |
7 | 7 | url(r'^create/(?P<slug>[\w_-]+)/$', views.PDFFileCreateView.as_view(), name='create'), |
8 | + url(r'^download/(?P<slug>[\w_-]+)/$', views.DownloadPDFFile.as_view(), name = 'download'), | |
9 | + url(r'^update/(?P<topic_slug>[\w_-]+)/(?P<slug>[\w_-]+)/$', views.UpdateView.as_view(), name = 'update'), | |
10 | + url(r'^delete/(?P<slug>[\w_-]+)/$', views.DeleteView.as_view(), name = 'delete'), | |
8 | 11 | ] |
9 | 12 | \ No newline at end of file | ... | ... |
pdf_file/views.py
... | ... | @@ -5,15 +5,69 @@ from django.contrib.auth.mixins import LoginRequiredMixin |
5 | 5 | from django.utils.translation import ugettext_lazy as _ |
6 | 6 | from django.core.urlresolvers import reverse, reverse_lazy |
7 | 7 | from django.shortcuts import get_object_or_404, redirect, render |
8 | +from django.contrib import messages | |
8 | 9 | |
9 | 10 | from amadeus.permissions import has_subject_permissions, has_resource_permissions |
10 | 11 | from .forms import PDFFileForm |
11 | 12 | |
12 | 13 | from log.mixins import LogMixin |
13 | 14 | from topics.models import Topic |
14 | - | |
15 | +from .models import PDFFile | |
15 | 16 | from pendencies.forms import PendenciesForm |
16 | 17 | |
18 | +class DownloadPDFFile(LoginRequiredMixin, LogMixin, generic.DetailView): | |
19 | + log_component = 'resources' | |
20 | + log_action = 'view' | |
21 | + log_resource = 'pdf_file' | |
22 | + log_context = {} | |
23 | + | |
24 | + login_url = reverse_lazy("users:login") | |
25 | + redirect_field_name = 'next' | |
26 | + | |
27 | + model = PDFFile | |
28 | + | |
29 | + def dispatch(self, request, *args, **kwargs): | |
30 | + slug = self.kwargs.get('slug', '') | |
31 | + pdf_file = get_object_or_404(PDFFile, slug = slug) | |
32 | + | |
33 | + if not has_resource_permissions(request.user, pdf_file): | |
34 | + return redirect(reverse_lazy('subjects:home')) | |
35 | + | |
36 | + return super(DownloadPDFFile, self).dispatch(request, *args, **kwargs) | |
37 | + | |
38 | + def render_to_response(self, context, **response_kwargs): | |
39 | + slug = self.kwargs.get('slug', '') | |
40 | + pdf_file = get_object_or_404(PDFFile, slug = slug) | |
41 | + | |
42 | + if not path.exists(pdf_file.file_content.path): | |
43 | + raise Http404() | |
44 | + | |
45 | + response = HttpResponse(open(pdf_file.file_content.path, 'rb').read()) | |
46 | + response['Content-Type'] = 'application/force-download' | |
47 | + response['Pragma'] = 'public' | |
48 | + response['Expires'] = '0' | |
49 | + response['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0' | |
50 | + response['Content-Disposition'] = 'attachment; filename=%s' % pdf_file.filename | |
51 | + response['Content-Transfer-Encoding'] = 'binary' | |
52 | + response['Content-Length'] = str(path.getsize(pdf_file.file_content.path)) | |
53 | + | |
54 | + self.log_context['category_id'] = pdf_file.topic.subject.category.id | |
55 | + self.log_context['category_name'] = pdf_file.topic.subject.category.name | |
56 | + self.log_context['category_slug'] = pdf_file.topic.subject.category.slug | |
57 | + self.log_context['subject_id'] = pdf_file.topic.subject.id | |
58 | + self.log_context['subject_name'] = pdf_file.topic.subject.name | |
59 | + self.log_context['subject_slug'] = pdf_file.topic.subject.slug | |
60 | + self.log_context['topic_id'] = pdf_file.topic.id | |
61 | + self.log_context['topic_name'] = pdf_file.topic.name | |
62 | + self.log_context['topic_slug'] = pdf_file.topic.slug | |
63 | + self.log_context['pdf_file_id'] = pdf_file.id | |
64 | + self.log_context['pdf_filek_name'] = pdf_file.name | |
65 | + self.log_context['pdf_file_slug'] = pdf_file.slug | |
66 | + | |
67 | + super(DownloadPDFFile, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
68 | + | |
69 | + return response | |
70 | + | |
17 | 71 | |
18 | 72 | class PDFFileCreateView(LoginRequiredMixin, LogMixin , generic.CreateView): |
19 | 73 | form_class = PDFFileForm |
... | ... | @@ -130,4 +184,161 @@ class PDFFileCreateView(LoginRequiredMixin, LogMixin , generic.CreateView): |
130 | 184 | return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) |
131 | 185 | |
132 | 186 | |
187 | +class UpdateView(LoginRequiredMixin, LogMixin, generic.UpdateView): | |
188 | + log_component = 'resources' | |
189 | + log_action = 'update' | |
190 | + log_resource = 'pdf_file' | |
191 | + log_context = {} | |
192 | + | |
193 | + login_url = reverse_lazy("users:login") | |
194 | + redirect_field_name = 'next' | |
195 | + | |
196 | + template_name = 'pdf_file/update.html' | |
197 | + model = PDFFile | |
198 | + form_class = PDFFileForm | |
199 | + context_object_name = 'pdf_file' | |
200 | + | |
201 | + def dispatch(self, request, *args, **kwargs): | |
202 | + slug = self.kwargs.get('topic_slug', '') | |
203 | + topic = get_object_or_404(Topic, slug = slug) | |
204 | + | |
205 | + if not has_subject_permissions(request.user, topic.subject): | |
206 | + return redirect(reverse_lazy('subjects:home')) | |
207 | + | |
208 | + return super(UpdateView, self).dispatch(request, *args, **kwargs) | |
209 | + | |
210 | + def get(self, request, *args, **kwargs): | |
211 | + self.object = self.get_object() | |
212 | + | |
213 | + form_class = self.get_form_class() | |
214 | + form = self.get_form(form_class) | |
215 | + | |
216 | + slug = self.kwargs.get('topic_slug', '') | |
217 | + topic = get_object_or_404(Topic, slug = slug) | |
218 | + | |
219 | + pend_form = self.object.pendencies_resource.all() | |
220 | + | |
221 | + if len(pend_form) > 0: | |
222 | + pendencies_form = PendenciesForm(instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
223 | + else: | |
224 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
225 | + | |
226 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
227 | + | |
228 | + def post(self, request, *args, **kwargs): | |
229 | + self.object = self.get_object() | |
230 | + | |
231 | + form_class = self.get_form_class() | |
232 | + form = self.get_form(form_class) | |
233 | + | |
234 | + slug = self.kwargs.get('topic_slug', '') | |
235 | + topic = get_object_or_404(Topic, slug = slug) | |
236 | + | |
237 | + pend_form = self.object.pendencies_resource.all() | |
238 | + | |
239 | + if len(pend_form) > 0: | |
240 | + pendencies_form = PendenciesForm(self.request.POST, instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
241 | + else: | |
242 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
243 | + | |
244 | + if (form.is_valid() and pendencies_form.is_valid()): | |
245 | + return self.form_valid(form, pendencies_form) | |
246 | + else: | |
247 | + return self.form_invalid(form, pendencies_form) | |
248 | + | |
249 | + def form_invalid(self, form, pendencies_form): | |
250 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
251 | + | |
252 | + def form_valid(self, form, pendencies_form): | |
253 | + self.object = form.save(commit = False) | |
254 | + | |
255 | + if not self.object.topic.visible and not self.object.topic.repository: | |
256 | + self.object.visible = False | |
257 | + | |
258 | + self.object.save() | |
259 | + | |
260 | + pend_form = pendencies_form.save(commit = False) | |
261 | + pend_form.resource = self.object | |
262 | + | |
263 | + if not pend_form.action == "": | |
264 | + pend_form.save() | |
265 | + | |
266 | + self.log_context['category_id'] = self.object.topic.subject.category.id | |
267 | + self.log_context['category_name'] = self.object.topic.subject.category.name | |
268 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | |
269 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
270 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
271 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
272 | + self.log_context['topic_id'] = self.object.topic.id | |
273 | + self.log_context['topic_name'] = self.object.topic.name | |
274 | + self.log_context['topic_slug'] = self.object.topic.slug | |
275 | + self.log_context['pdf_file_id'] = self.object.id | |
276 | + self.log_context['pdf_file_name'] = self.object.name | |
277 | + self.log_context['pdf_file_slug'] = self.object.slug | |
278 | + | |
279 | + super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
280 | + | |
281 | + return redirect(self.get_success_url()) | |
282 | + | |
283 | + def get_context_data(self, **kwargs): | |
284 | + context = super(UpdateView, self).get_context_data(**kwargs) | |
285 | + | |
286 | + context['title'] = _('Update PDF File') | |
287 | + | |
288 | + slug = self.kwargs.get('topic_slug', '') | |
289 | + topic = get_object_or_404(Topic, slug = slug) | |
290 | + | |
291 | + context['topic'] = topic | |
292 | + context['subject'] = topic.subject | |
293 | + | |
294 | + return context | |
295 | + | |
296 | + def get_success_url(self): | |
297 | + messages.success(self.request, _('The PDF File "%s" was updated successfully!')%(self.object.name)) | |
298 | + | |
299 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
300 | + | |
301 | +class DeleteView(LoginRequiredMixin, LogMixin, generic.DeleteView): | |
302 | + log_component = 'resources' | |
303 | + log_action = 'delete' | |
304 | + log_resource = 'pdf_file' | |
305 | + log_context = {} | |
306 | + | |
307 | + login_url = reverse_lazy("users:login") | |
308 | + redirect_field_name = 'next' | |
309 | + | |
310 | + template_name = 'resources/delete.html' | |
311 | + model = PDFFile | |
312 | + context_object_name = 'resource' | |
313 | + | |
314 | + def dispatch(self, request, *args, **kwargs): | |
315 | + slug = self.kwargs.get('slug', '') | |
316 | + pdf_file = get_object_or_404(PDFFile, slug = slug) | |
317 | + | |
318 | + if not has_subject_permissions(request.user, pdf_file.topic.subject): | |
319 | + return redirect(reverse_lazy('subjects:home')) | |
320 | + | |
321 | + return super(DeleteView, self).dispatch(request, *args, **kwargs) | |
322 | + | |
323 | + def get_success_url(self): | |
324 | + messages.success(self.request, _('The PDF File "%s" was removed successfully from virtual environment "%s"!')%(self.object.name, self.object.topic.subject.name)) | |
325 | + | |
326 | + self.log_context['category_id'] = self.object.topic.subject.category.id | |
327 | + self.log_context['category_name'] = self.object.topic.subject.category.name | |
328 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | |
329 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
330 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
331 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
332 | + self.log_context['topic_id'] = self.object.topic.id | |
333 | + self.log_context['topic_name'] = self.object.topic.name | |
334 | + self.log_context['topic_slug'] = self.object.topic.slug | |
335 | + self.log_context['pdf_file_id'] = self.object.id | |
336 | + self.log_context['pdf_file_name'] = self.object.name | |
337 | + self.log_context['pdf_file_slug'] = self.object.slug | |
338 | + | |
339 | + super(DeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
340 | + | |
341 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
342 | + | |
343 | + | |
133 | 344 | ... | ... |