Commit 813c728d9d17e1a383cbb22b45314a8195993bd3
1 parent
473745b3
Exists in
master
and in
3 other branches
now viewing pdf file is working 100%
Showing
5 changed files
with
276 additions
and
269 deletions
Show diff stats
amadeus/settings.py
pdf_file/models.py
| ... | ... | @@ -0,0 +1,29 @@ |
| 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 | + | |
| 27 | +{% block content %} | |
| 28 | + {{context\pdf_file}} | |
| 29 | +{% endblock content %} | |
| 0 | 30 | \ No newline at end of file | ... | ... |
pdf_file/urls.py
| ... | ... | @@ -5,7 +5,8 @@ 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 | 8 | url(r'^update/(?P<topic_slug>[\w_-]+)/(?P<slug>[\w_-]+)/$', views.UpdateView.as_view(), name = 'update'), |
| 10 | 9 | url(r'^delete/(?P<slug>[\w_-]+)/$', views.DeleteView.as_view(), name = 'delete'), |
| 10 | + url(r'^view/(?P<slug>[\w_-]+)/$', views.ViewPDFFile.as_view(), name = 'view'), | |
| 11 | + | |
| 11 | 12 | ] |
| 12 | 13 | \ No newline at end of file | ... | ... |
pdf_file/views.py
| ... | ... | @@ -9,336 +9,312 @@ from django.contrib import messages |
| 9 | 9 | |
| 10 | 10 | from amadeus.permissions import has_subject_permissions, has_resource_permissions |
| 11 | 11 | from .forms import PDFFileForm |
| 12 | +from os import path | |
| 13 | +from django.http import HttpResponse, Http404 | |
| 12 | 14 | |
| 13 | 15 | from log.mixins import LogMixin |
| 14 | -from topics.models import Topic | |
| 16 | +from topics.models import Topic, Resource | |
| 15 | 17 | from .models import PDFFile |
| 16 | 18 | from pendencies.forms import PendenciesForm |
| 17 | 19 | |
| 18 | -class DownloadPDFFile(LoginRequiredMixin, LogMixin, generic.DetailView): | |
| 19 | - log_component = 'resources' | |
| 20 | - log_action = 'view' | |
| 21 | - log_resource = 'pdf_file' | |
| 22 | - log_context = {} | |
| 23 | 20 | |
| 24 | - login_url = reverse_lazy("users:login") | |
| 25 | - redirect_field_name = 'next' | |
| 26 | 21 | |
| 27 | - model = PDFFile | |
| 22 | +class ViewPDFFile(generic.TemplateView): | |
| 23 | + template_name='pdf_file/view.html' | |
| 28 | 24 | |
| 29 | - def dispatch(self, request, *args, **kwargs): | |
| 30 | - slug = self.kwargs.get('slug', '') | |
| 31 | - pdf_file = get_object_or_404(PDFFile, slug = slug) | |
| 25 | + def dispatch(self, request, *args, **kwargs): | |
| 26 | + slug = self.kwargs.get('slug', '') | |
| 27 | + resource = get_object_or_404(Resource, slug = slug) | |
| 28 | + topic = resource.topic | |
| 32 | 29 | |
| 33 | - if not has_resource_permissions(request.user, pdf_file): | |
| 34 | - return redirect(reverse_lazy('subjects:home')) | |
| 30 | + if not has_subject_permissions(request.user, topic.subject): | |
| 31 | + return redirect(reverse_lazy('subjects:home')) | |
| 35 | 32 | |
| 36 | - return super(DownloadPDFFile, self).dispatch(request, *args, **kwargs) | |
| 33 | + return super(ViewPDFFile, self).dispatch(request, *args, **kwargs) | |
| 37 | 34 | |
| 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 | 35 | |
| 42 | - if not path.exists(pdf_file.file_content.path): | |
| 43 | - raise Http404() | |
| 44 | 36 | |
| 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)) | |
| 37 | + def render_to_response(self, context, **response_kwargs): | |
| 38 | + slug = self.kwargs.get('slug', '') | |
| 39 | + pdf_file = PDFFile.objects.get(slug=slug) | |
| 53 | 40 | |
| 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 | |
| 41 | + if not path.exists(pdf_file.file.path): | |
| 42 | + raise Http404() | |
| 70 | 43 | |
| 44 | + response = HttpResponse(open(pdf_file.file.path, 'rb').read(),content_type = 'application/pdf') | |
| 45 | + | |
| 46 | + return response | |
| 71 | 47 | |
| 72 | 48 | class PDFFileCreateView(LoginRequiredMixin, LogMixin , generic.CreateView): |
| 73 | - form_class = PDFFileForm | |
| 74 | - template_name = 'pdf_file/create.html' | |
| 49 | + form_class = PDFFileForm | |
| 50 | + template_name = 'pdf_file/create.html' | |
| 75 | 51 | |
| 76 | - login_url = reverse_lazy("users:login") | |
| 77 | - redirect_field_name = 'next' | |
| 52 | + login_url = reverse_lazy("users:login") | |
| 53 | + redirect_field_name = 'next' | |
| 78 | 54 | |
| 79 | 55 | |
| 80 | - def dispatch(self, request, *args, **kwargs): | |
| 81 | - slug = self.kwargs.get('slug', '') | |
| 82 | - topic = get_object_or_404(Topic, slug = slug) | |
| 56 | + def dispatch(self, request, *args, **kwargs): | |
| 57 | + slug = self.kwargs.get('slug', '') | |
| 58 | + topic = get_object_or_404(Topic, slug = slug) | |
| 83 | 59 | |
| 84 | - if not has_subject_permissions(request.user, topic.subject): | |
| 85 | - return redirect(reverse_lazy('subjects:home')) | |
| 60 | + if not has_subject_permissions(request.user, topic.subject): | |
| 61 | + return redirect(reverse_lazy('subjects:home')) | |
| 86 | 62 | |
| 87 | - return super(PDFFileCreateView, self).dispatch(request, *args, **kwargs) | |
| 63 | + return super(PDFFileCreateView, self).dispatch(request, *args, **kwargs) | |
| 88 | 64 | |
| 89 | - def get(self, request, *args, **kwargs): | |
| 90 | - self.object = None | |
| 91 | - | |
| 92 | - form_class = self.get_form_class() | |
| 93 | - form = self.get_form(form_class) | |
| 65 | + def get(self, request, *args, **kwargs): | |
| 66 | + self.object = None | |
| 67 | + | |
| 68 | + form_class = self.get_form_class() | |
| 69 | + form = self.get_form(form_class) | |
| 94 | 70 | |
| 95 | - slug = self.kwargs.get('slug', '') | |
| 96 | - topic = get_object_or_404(Topic, slug = slug) | |
| 71 | + slug = self.kwargs.get('slug', '') | |
| 72 | + topic = get_object_or_404(Topic, slug = slug) | |
| 97 | 73 | |
| 98 | - pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 74 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 99 | 75 | |
| 100 | - return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 76 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 101 | 77 | |
| 102 | - def post(self, request, *args, **kwargs): | |
| 103 | - self.object = None | |
| 104 | - | |
| 105 | - form_class = self.get_form_class() | |
| 106 | - form = self.get_form(form_class) | |
| 78 | + def post(self, request, *args, **kwargs): | |
| 79 | + self.object = None | |
| 80 | + | |
| 81 | + form_class = self.get_form_class() | |
| 82 | + form = self.get_form(form_class) | |
| 107 | 83 | |
| 108 | - slug = self.kwargs.get('slug', '') | |
| 109 | - topic = get_object_or_404(Topic, slug = slug) | |
| 84 | + slug = self.kwargs.get('slug', '') | |
| 85 | + topic = get_object_or_404(Topic, slug = slug) | |
| 110 | 86 | |
| 111 | - pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 112 | - | |
| 113 | - if (form.is_valid() and pendencies_form.is_valid()): | |
| 114 | - return self.form_valid(form, pendencies_form) | |
| 115 | - else: | |
| 116 | - return self.form_invalid(form, pendencies_form) | |
| 87 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 88 | + | |
| 89 | + if (form.is_valid() and pendencies_form.is_valid()): | |
| 90 | + return self.form_valid(form, pendencies_form) | |
| 91 | + else: | |
| 92 | + return self.form_invalid(form, pendencies_form) | |
| 117 | 93 | |
| 118 | - def get_initial(self): | |
| 119 | - initial = super(PDFFileCreateView, self).get_initial() | |
| 94 | + def get_initial(self): | |
| 95 | + initial = super(PDFFileCreateView, self).get_initial() | |
| 120 | 96 | |
| 121 | - slug = self.kwargs.get('slug', '') | |
| 97 | + slug = self.kwargs.get('slug', '') | |
| 122 | 98 | |
| 123 | - topic = get_object_or_404(Topic, slug = slug) | |
| 124 | - initial['subject'] = topic.subject | |
| 125 | - | |
| 126 | - return initial | |
| 99 | + topic = get_object_or_404(Topic, slug = slug) | |
| 100 | + initial['subject'] = topic.subject | |
| 101 | + | |
| 102 | + return initial | |
| 127 | 103 | |
| 128 | - def form_invalid(self, form, pendencies_form): | |
| 129 | - return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 104 | + def form_invalid(self, form, pendencies_form): | |
| 105 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 130 | 106 | |
| 131 | - def form_valid(self, form, pendencies_form): | |
| 132 | - self.object = form.save(commit = False) | |
| 107 | + def form_valid(self, form, pendencies_form): | |
| 108 | + self.object = form.save(commit = False) | |
| 133 | 109 | |
| 134 | - slug = self.kwargs.get('slug', '') | |
| 135 | - topic = get_object_or_404(Topic, slug = slug) | |
| 110 | + slug = self.kwargs.get('slug', '') | |
| 111 | + topic = get_object_or_404(Topic, slug = slug) | |
| 136 | 112 | |
| 137 | - self.object.topic = topic | |
| 138 | - self.object.order = topic.resource_topic.count() + 1 | |
| 113 | + self.object.topic = topic | |
| 114 | + self.object.order = topic.resource_topic.count() + 1 | |
| 139 | 115 | |
| 140 | - if not self.object.topic.visible and not self.object.topic.repository: | |
| 141 | - self.object.visible = False | |
| 116 | + if not self.object.topic.visible and not self.object.topic.repository: | |
| 117 | + self.object.visible = False | |
| 142 | 118 | |
| 143 | - self.object.save() | |
| 119 | + self.object.save() | |
| 144 | 120 | |
| 145 | - pend_form = pendencies_form.save(commit = False) | |
| 146 | - pend_form.resource = self.object | |
| 147 | - | |
| 148 | - if not pend_form.action == "": | |
| 149 | - pend_form.save() | |
| 150 | - | |
| 151 | - self.log_context['category_id'] = self.object.topic.subject.category.id | |
| 152 | - self.log_context['category_name'] = self.object.topic.subject.category.name | |
| 153 | - self.log_context['category_slug'] = self.object.topic.subject.category.slug | |
| 154 | - self.log_context['subject_id'] = self.object.topic.subject.id | |
| 155 | - self.log_context['subject_name'] = self.object.topic.subject.name | |
| 156 | - self.log_context['subject_slug'] = self.object.topic.subject.slug | |
| 157 | - self.log_context['topic_id'] = self.object.topic.id | |
| 158 | - self.log_context['topic_name'] = self.object.topic.name | |
| 159 | - self.log_context['topic_slug'] = self.object.topic.slug | |
| 160 | - self.log_context['pdf_file_id'] = self.object.id | |
| 161 | - self.log_context['pdf_file_name'] = self.object.name | |
| 162 | - self.log_context['pdf_file_slug'] = self.object.slug | |
| 121 | + pend_form = pendencies_form.save(commit = False) | |
| 122 | + pend_form.resource = self.object | |
| 123 | + | |
| 124 | + if not pend_form.action == "": | |
| 125 | + pend_form.save() | |
| 126 | + | |
| 127 | + self.log_context['category_id'] = self.object.topic.subject.category.id | |
| 128 | + self.log_context['category_name'] = self.object.topic.subject.category.name | |
| 129 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | |
| 130 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
| 131 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
| 132 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
| 133 | + self.log_context['topic_id'] = self.object.topic.id | |
| 134 | + self.log_context['topic_name'] = self.object.topic.name | |
| 135 | + self.log_context['topic_slug'] = self.object.topic.slug | |
| 136 | + self.log_context['pdf_file_id'] = self.object.id | |
| 137 | + self.log_context['pdf_file_name'] = self.object.name | |
| 138 | + self.log_context['pdf_file_slug'] = self.object.slug | |
| 163 | 139 | |
| 164 | - super(PDFFileCreateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
| 140 | + super(PDFFileCreateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
| 165 | 141 | |
| 166 | - return redirect(self.get_success_url()) | |
| 142 | + return redirect(self.get_success_url()) | |
| 167 | 143 | |
| 168 | - def get_context_data(self, **kwargs): | |
| 169 | - context = super(PDFFileCreateView, self).get_context_data(**kwargs) | |
| 144 | + def get_context_data(self, **kwargs): | |
| 145 | + context = super(PDFFileCreateView, self).get_context_data(**kwargs) | |
| 170 | 146 | |
| 171 | - context['title'] = _('Create PDF File') | |
| 147 | + context['title'] = _('Create PDF File') | |
| 172 | 148 | |
| 173 | - slug = self.kwargs.get('slug', '') | |
| 174 | - topic = get_object_or_404(Topic, slug = slug) | |
| 149 | + slug = self.kwargs.get('slug', '') | |
| 150 | + topic = get_object_or_404(Topic, slug = slug) | |
| 175 | 151 | |
| 176 | - context['topic'] = topic | |
| 177 | - context['subject'] = topic.subject | |
| 152 | + context['topic'] = topic | |
| 153 | + context['subject'] = topic.subject | |
| 178 | 154 | |
| 179 | - return context | |
| 155 | + return context | |
| 180 | 156 | |
| 181 | - def get_success_url(self): | |
| 182 | - messages.success(self.request, _('The PDF File "%s" was added to the Topic "%s" of the virtual environment "%s" successfully!')%(self.object.name, self.object.topic.name, self.object.topic.subject.name)) | |
| 157 | + def get_success_url(self): | |
| 158 | + messages.success(self.request, _('The PDF File "%s" was added to the Topic "%s" of the virtual environment "%s" successfully!')%(self.object.name, self.object.topic.name, self.object.topic.subject.name)) | |
| 183 | 159 | |
| 184 | - return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
| 160 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
| 185 | 161 | |
| 186 | 162 | |
| 187 | 163 | 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() | |
| 164 | + log_component = 'resources' | |
| 165 | + log_action = 'update' | |
| 166 | + log_resource = 'pdf_file' | |
| 167 | + log_context = {} | |
| 168 | + | |
| 169 | + login_url = reverse_lazy("users:login") | |
| 170 | + redirect_field_name = 'next' | |
| 171 | + | |
| 172 | + template_name = 'pdf_file/update.html' | |
| 173 | + model = PDFFile | |
| 174 | + form_class = PDFFileForm | |
| 175 | + context_object_name = 'pdf_file' | |
| 176 | + | |
| 177 | + def dispatch(self, request, *args, **kwargs): | |
| 178 | + slug = self.kwargs.get('topic_slug', '') | |
| 179 | + topic = get_object_or_404(Topic, slug = slug) | |
| 180 | + | |
| 181 | + if not has_subject_permissions(request.user, topic.subject): | |
| 182 | + return redirect(reverse_lazy('subjects:home')) | |
| 183 | + | |
| 184 | + return super(UpdateView, self).dispatch(request, *args, **kwargs) | |
| 185 | + | |
| 186 | + def get(self, request, *args, **kwargs): | |
| 187 | + self.object = self.get_object() | |
| 188 | + | |
| 189 | + form_class = self.get_form_class() | |
| 190 | + form = self.get_form(form_class) | |
| 191 | + | |
| 192 | + slug = self.kwargs.get('topic_slug', '') | |
| 193 | + topic = get_object_or_404(Topic, slug = slug) | |
| 194 | + | |
| 195 | + pend_form = self.object.pendencies_resource.all() | |
| 196 | + | |
| 197 | + if len(pend_form) > 0: | |
| 198 | + pendencies_form = PendenciesForm(instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 199 | + else: | |
| 200 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 201 | + | |
| 202 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 203 | + | |
| 204 | + def post(self, request, *args, **kwargs): | |
| 205 | + self.object = self.get_object() | |
| 206 | + | |
| 207 | + form_class = self.get_form_class() | |
| 208 | + form = self.get_form(form_class) | |
| 209 | + | |
| 210 | + slug = self.kwargs.get('topic_slug', '') | |
| 211 | + topic = get_object_or_404(Topic, slug = slug) | |
| 212 | + | |
| 213 | + pend_form = self.object.pendencies_resource.all() | |
| 214 | + | |
| 215 | + if len(pend_form) > 0: | |
| 216 | + pendencies_form = PendenciesForm(self.request.POST, instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 217 | + else: | |
| 218 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | |
| 219 | + | |
| 220 | + if (form.is_valid() and pendencies_form.is_valid()): | |
| 221 | + return self.form_valid(form, pendencies_form) | |
| 222 | + else: | |
| 223 | + return self.form_invalid(form, pendencies_form) | |
| 224 | + | |
| 225 | + def form_invalid(self, form, pendencies_form): | |
| 226 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | |
| 227 | + | |
| 228 | + def form_valid(self, form, pendencies_form): | |
| 229 | + self.object = form.save(commit = False) | |
| 230 | + | |
| 231 | + if not self.object.topic.visible and not self.object.topic.repository: | |
| 232 | + self.object.visible = False | |
| 233 | + | |
| 234 | + self.object.save() | |
| 235 | + | |
| 236 | + pend_form = pendencies_form.save(commit = False) | |
| 237 | + pend_form.resource = self.object | |
| 238 | + | |
| 239 | + if not pend_form.action == "": | |
| 240 | + pend_form.save() | |
| 265 | 241 | |
| 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 | |
| 242 | + self.log_context['category_id'] = self.object.topic.subject.category.id | |
| 243 | + self.log_context['category_name'] = self.object.topic.subject.category.name | |
| 244 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | |
| 245 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
| 246 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
| 247 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
| 248 | + self.log_context['topic_id'] = self.object.topic.id | |
| 249 | + self.log_context['topic_name'] = self.object.topic.name | |
| 250 | + self.log_context['topic_slug'] = self.object.topic.slug | |
| 251 | + self.log_context['pdf_file_id'] = self.object.id | |
| 252 | + self.log_context['pdf_file_name'] = self.object.name | |
| 253 | + self.log_context['pdf_file_slug'] = self.object.slug | |
| 278 | 254 | |
| 279 | - super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
| 255 | + super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
| 280 | 256 | |
| 281 | - return redirect(self.get_success_url()) | |
| 257 | + return redirect(self.get_success_url()) | |
| 282 | 258 | |
| 283 | - def get_context_data(self, **kwargs): | |
| 284 | - context = super(UpdateView, self).get_context_data(**kwargs) | |
| 259 | + def get_context_data(self, **kwargs): | |
| 260 | + context = super(UpdateView, self).get_context_data(**kwargs) | |
| 285 | 261 | |
| 286 | - context['title'] = _('Update PDF File') | |
| 262 | + context['title'] = _('Update PDF File') | |
| 287 | 263 | |
| 288 | - slug = self.kwargs.get('topic_slug', '') | |
| 289 | - topic = get_object_or_404(Topic, slug = slug) | |
| 264 | + slug = self.kwargs.get('topic_slug', '') | |
| 265 | + topic = get_object_or_404(Topic, slug = slug) | |
| 290 | 266 | |
| 291 | - context['topic'] = topic | |
| 292 | - context['subject'] = topic.subject | |
| 267 | + context['topic'] = topic | |
| 268 | + context['subject'] = topic.subject | |
| 293 | 269 | |
| 294 | - return context | |
| 270 | + return context | |
| 295 | 271 | |
| 296 | - def get_success_url(self): | |
| 297 | - messages.success(self.request, _('The PDF File "%s" was updated successfully!')%(self.object.name)) | |
| 272 | + def get_success_url(self): | |
| 273 | + messages.success(self.request, _('The PDF File "%s" was updated successfully!')%(self.object.name)) | |
| 298 | 274 | |
| 299 | - return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
| 275 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
| 300 | 276 | |
| 301 | 277 | 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}) | |
| 278 | + log_component = 'resources' | |
| 279 | + log_action = 'delete' | |
| 280 | + log_resource = 'pdf_file' | |
| 281 | + log_context = {} | |
| 282 | + | |
| 283 | + login_url = reverse_lazy("users:login") | |
| 284 | + redirect_field_name = 'next' | |
| 285 | + | |
| 286 | + template_name = 'resources/delete.html' | |
| 287 | + model = PDFFile | |
| 288 | + context_object_name = 'resource' | |
| 289 | + | |
| 290 | + def dispatch(self, request, *args, **kwargs): | |
| 291 | + slug = self.kwargs.get('slug', '') | |
| 292 | + pdf_file = get_object_or_404(PDFFile, slug = slug) | |
| 293 | + | |
| 294 | + if not has_subject_permissions(request.user, pdf_file.topic.subject): | |
| 295 | + return redirect(reverse_lazy('subjects:home')) | |
| 296 | + | |
| 297 | + return super(DeleteView, self).dispatch(request, *args, **kwargs) | |
| 298 | + | |
| 299 | + def get_success_url(self): | |
| 300 | + messages.success(self.request, _('The PDF File "%s" was removed successfully from virtual environment "%s"!')%(self.object.name, self.object.topic.subject.name)) | |
| 301 | + | |
| 302 | + self.log_context['category_id'] = self.object.topic.subject.category.id | |
| 303 | + self.log_context['category_name'] = self.object.topic.subject.category.name | |
| 304 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | |
| 305 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
| 306 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
| 307 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
| 308 | + self.log_context['topic_id'] = self.object.topic.id | |
| 309 | + self.log_context['topic_name'] = self.object.topic.name | |
| 310 | + self.log_context['topic_slug'] = self.object.topic.slug | |
| 311 | + self.log_context['pdf_file_id'] = self.object.id | |
| 312 | + self.log_context['pdf_file_name'] = self.object.name | |
| 313 | + self.log_context['pdf_file_slug'] = self.object.slug | |
| 314 | + | |
| 315 | + super(DeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
| 316 | + | |
| 317 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | |
| 342 | 318 | |
| 343 | 319 | |
| 344 | 320 | ... | ... |