Commit 524efcb7016aa3dc0945a96cdcde76bd6d2c1ab0
Exists in
master
and in
5 other branches
dev merge
Showing
15 changed files
with
229 additions
and
80 deletions
Show diff stats
courses/templates/subject/form_view_teacher.html
... | ... | @@ -4,7 +4,7 @@ |
4 | 4 | <script type="text/javascript" src="{% static 'js/forum.js' %}"></script> |
5 | 5 | <script src="{% static 'js/file.js' %}"></script> |
6 | 6 | <script type="text/javascript" src="{% static 'js/material.js' %}"></script> |
7 | - <script type = "text/javascript" src="{% static 'links.js' %}"></script> | |
7 | + <script type = "text/javascript" src="{% static 'js/links.js' %}"></script> | |
8 | 8 | {% endblock %} |
9 | 9 | <div class="panel panel-default cards-detail"> |
10 | 10 | <div class="panel-heading topic"> |
... | ... | @@ -39,7 +39,7 @@ |
39 | 39 | </div><!--column--> |
40 | 40 | </div><!--row--> |
41 | 41 | </div> |
42 | - <div class="panel-collapse collapseTopic-{{topic.slug}} collapse in" role="tabpanel" aria-labelledby="heading_{{topic.id}}" aria-expanded="true" aria-hidden="false"> | |
42 | + <div class="panel-collapse collapseTopic-{{topic.slug}} topic_{{ topic.id }} collapse in" role="tabpanel" aria-labelledby="heading_{{topic.id}}" aria-expanded="true" aria-hidden="false"> | |
43 | 43 | <div class="panel-body"> |
44 | 44 | <div class="presentation" id="presentation_{{topic.slug}}"> |
45 | 45 | <p> |
... | ... | @@ -86,7 +86,7 @@ |
86 | 86 | </div> |
87 | 87 | </div> |
88 | 88 | </div><!--EndPresentation--> |
89 | - <div class="editation" id="editation_{{topic.slug}}"> | |
89 | + <div class="editation topic_{{ topic.id }}" id="editation_{{topic.slug}}"> | |
90 | 90 | <div class="form-group"> |
91 | 91 | <label class="control-label" for="focusedInput2">{% trans 'Name Topic' %}</label> |
92 | 92 | <input type="text" class="form-control" value="{{topic}}"> |
... | ... | @@ -140,11 +140,7 @@ |
140 | 140 | </div> |
141 | 141 | </div> |
142 | 142 | </div><!--EndEditation--> |
143 | - {% professor_subject topic.subject user as professor_links %} | |
144 | - {% if professor_links%} | |
145 | - {% include "links/create_link.html" %} | |
146 | - {% include "links/delete_link.html" %} | |
147 | - {% endif %} | |
143 | + | |
148 | 144 | </div> |
149 | 145 | </div> |
150 | 146 | </div> |
... | ... | @@ -287,10 +283,7 @@ |
287 | 283 | <!--EndModal--> |
288 | 284 | |
289 | 285 | <!-- MODAL LINK EDIT--> |
290 | -{% professor_subject topic.subject user as links_update %} | |
291 | -{% if links_update%} | |
292 | - {% include "links/update_link.html" %} | |
293 | -{% endif %} | |
286 | + | |
294 | 287 | |
295 | 288 | <!-- EndModal --> |
296 | 289 | |
... | ... | @@ -313,5 +306,3 @@ |
313 | 306 | </div> |
314 | 307 | </div> |
315 | 308 | </div> |
316 | - | |
317 | - | ... | ... |
courses/templates/topic/link_topic_list.html
1 | 1 | {% load static i18n list_topic_foruns permission_tags %} |
2 | 2 | <div id="list-topic{{ topic.id }}-links"> |
3 | 3 | {% for link in links%} |
4 | - <li><i class="fa fa-link" aria-hidden="true"></i> <a href="javascript:get_modal_link('{% url 'course:links:view_link' link.slug %}', '#viewLinkModal','#divModalLink')">{{link}}</a></li> | |
4 | + <li id = "link_{{ link.slug }}"><i class="fa fa-link" aria-hidden="true"></i> <a href="javascript:get_modal_link('{% url 'course:links:view_link' link.slug %}', '#viewLinkModal','#divModalLink')">{{link.name}}</a></li> | |
5 | 5 | {% endfor %} |
6 | 6 | </div> |
7 | 7 | <div class = 'row' id ="divModalLink"> | ... | ... |
courses/templates/topic/link_topic_list_edit.html
... | ... | @@ -2,8 +2,12 @@ |
2 | 2 | <div id="list-topic{{ topic.id }}-links-edit"> |
3 | 3 | {% for link in links%} |
4 | 4 | |
5 | - <li class="icon_edit_remove"> <a href="#" data-toggle="modal" data-target="#linksModalEdit"><i class="fa fa-pencil fa-lg" aria-hidden="true"></i></a> <a href="#" data-toggle="modal" data-target="#removeLink"><i class="fa fa-trash fa-lg" aria-hidden="true"></i></a></li> | |
6 | - <li><i class="fa fa-link" aria-hidden="true"></i> <a href="javascript:get_modal_link('{% url 'course:links:view_link' link.slug %}', '#viewLinkModal','#divModalLink')">{{link}}</a></li> | |
5 | + <li class="icon_edit_remove" id = "link_edit_icon_{{ link.slug }}"> <a href="javascript:get_modal_link('{% url 'course:links:update_link' link.slug %}', '#linksModalEdit', '#divModalLinkUpdate')"><i class="fa fa-pencil fa-lg" aria-hidden="true"></i></a> <a href="javascript:get_modal_link('{% url 'course:links:delete_link' link.slug %}', '#linkDeleteModal', '#divModalLinkUpdate')"><i class="fa fa-trash fa-lg" aria-hidden="true"></i></a></li> | |
6 | + <li id="link_edit_{{ link.slug }}"><i class="fa fa-link" aria-hidden="true"></i> <a href="javascript:get_modal_link('{% url 'course:links:view_link' link.slug %}', '#viewLinkModal','#divModalLink')">{{link.name}}</a></li> | |
7 | 7 | |
8 | 8 | {% endfor %} |
9 | 9 | </div> |
10 | + | |
11 | +<div class="row" id="divModalLinkUpdate"> | |
12 | + | |
13 | +</div> | ... | ... |
forum/static/js/forum.js
... | ... | @@ -47,7 +47,7 @@ function createForum(url, topic) { |
47 | 47 | $(".forum_form").html(data); |
48 | 48 | $("#id_topic").val(topic); |
49 | 49 | |
50 | - setForumCreateFormSubmit(); | |
50 | + setForumCreateFormSubmit(topic); | |
51 | 51 | } |
52 | 52 | }); |
53 | 53 | |
... | ... | @@ -59,7 +59,7 @@ function createForum(url, topic) { |
59 | 59 | * Function to set the forum's create form submit function |
60 | 60 | * |
61 | 61 | */ |
62 | -function setForumCreateFormSubmit() { | |
62 | +function setForumCreateFormSubmit(topic) { | |
63 | 63 | $('.date-picker').datepicker({ |
64 | 64 | language: locale, |
65 | 65 | }); |
... | ... | @@ -72,13 +72,13 @@ function setForumCreateFormSubmit() { |
72 | 72 | data: frm.serialize(), |
73 | 73 | dataType: "json", |
74 | 74 | success: function (data) { |
75 | - $('.foruns_list').append("<li><i class='fa fa-commenting' aria-hidden='true'></i> <a id='forum_"+data.forum_id+"' href='"+data.url+"'> "+data.name+"</a></li>"); | |
75 | + $(".topic_" + topic).find('.foruns_list').append("<li><i class='fa fa-commenting' aria-hidden='true'></i> <a id='forum_"+data.forum_id+"' href='"+data.url+"'> "+data.name+"</a></li>"); | |
76 | 76 | |
77 | 77 | $("#createForum").modal('hide'); |
78 | 78 | }, |
79 | 79 | error: function(data) { |
80 | 80 | $(".forum_form").html(data.responseText); |
81 | - setForumCreateFormSubmit(); | |
81 | + setForumCreateFormSubmit(topic); | |
82 | 82 | } |
83 | 83 | }); |
84 | 84 | return false; | ... | ... |
links/admin.py
... | ... | @@ -3,8 +3,8 @@ from django.contrib import admin |
3 | 3 | from .models import Link |
4 | 4 | |
5 | 5 | class LinkAdmin(admin.ModelAdmin): |
6 | - list_display = ['name', 'link_url','link_description'] | |
7 | - search_fields = ['name', 'link_url','link_description'] | |
6 | + list_display = ['name', 'slug','link_url','link_description'] | |
7 | + search_fields = ['name','slug' ,'link_url','link_description'] | |
8 | 8 | |
9 | 9 | |
10 | 10 | admin.site.register(Link, LinkAdmin) | ... | ... |
links/forms.py
... | ... | @@ -21,6 +21,15 @@ class CreateLinkForm(forms.ModelForm): |
21 | 21 | fields = ['name','link_url','link_description'] |
22 | 22 | |
23 | 23 | class UpdateLinkForm(forms.ModelForm): |
24 | + def clean_link_url(self): | |
25 | + link_url = self.cleaned_data['link_url'] | |
26 | + try: | |
27 | + resposta = requests.head(link_url) | |
28 | + if resposta.status_code >= 400: | |
29 | + raise forms.ValidationError(_('Invalid url!')) | |
30 | + except requests.ConnectionError: | |
31 | + raise forms.ValidationError(_('Invalid url!')) | |
32 | + return link_url | |
24 | 33 | class Meta: |
25 | 34 | model = Link |
26 | 35 | fields = ['name','link_url','link_description'] | ... | ... |
links/templates/links/create_link.html
... | ... | @@ -13,6 +13,7 @@ |
13 | 13 | <form method="post" action="" id="form-link" enctype="multipart/form-data"> |
14 | 14 | {% csrf_token %} |
15 | 15 | {% for field in form %} |
16 | + <div class ="form-group"> | |
16 | 17 | {% if field.field.required %} |
17 | 18 | <label for="{{ field.auto_id }}">{{ field.label }}<span>*</span></label> |
18 | 19 | {% endif %} |
... | ... | @@ -29,9 +30,10 @@ |
29 | 30 | </ul> |
30 | 31 | </div> |
31 | 32 | {% endif %} |
33 | + </div> | |
32 | 34 | {% endfor %} |
33 | 35 | <div class="form-group"> |
34 | - <a href="javascript:void(0)" class="btn btn-raised btn-default" data-dismiss="modal">{% trans 'Cancel' %}</a> | |
36 | + <button type="button" class="btn btn-danger btn-raised" data-dismiss="modal">{% trans "Cancel" %}</button> | |
35 | 37 | <button class="btn btn-raised btn-primary" type="submit">{% trans 'Submit' %}</button> |
36 | 38 | </div> |
37 | 39 | <!-- .end Card --> |
... | ... | @@ -41,7 +43,7 @@ |
41 | 43 | </div> |
42 | 44 | <!-- EndModal --> |
43 | 45 | {% block script_link %} |
44 | - {# // <script src="{% static '/links.js' %}"></script> #} | |
46 | + {# // <script src="{% static 'js/links.js' %}"></script> #} | |
45 | 47 | <script type="text/javascript"> |
46 | 48 | $("#form-link").submit(function(event) { |
47 | 49 | var data = new FormData($('#form-link').get(0)); | ... | ... |
links/templates/links/delete_link.html
1 | -{% load widget_tweaks i18n %} | |
2 | -<!-- MODAL REMOVE LINK --> | |
3 | -<div class="modal" id="removeLink"> | |
4 | - <div class="modal-dialog"> | |
5 | - <div class="modal-content"> | |
6 | - <div class="modal-header"> | |
7 | - <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button> | |
8 | - <h4 class="modal-title"></h4> | |
9 | - </div> | |
10 | - <div class="modal-body"> | |
11 | - <p>{% trans 'Do you really want to delete this link?' %}</p> | |
12 | - </div> | |
13 | - <div class="modal-footer"> | |
14 | - <!-- --> | |
15 | - <button type="button" class="btn btn-primary btn-default" data-dismiss="modal">{% trans 'Cancel' %}</button> | |
16 | - <a href="http://www.google.com" target="_self"><button type="button" class="btn btn-primary" data-dismiss="modal" aria-hidden="true">{% trans 'Confirm' %}</button></a> | |
1 | +{% load static widget_tweaks i18n %} | |
17 | 2 | |
18 | - </div> | |
3 | +<!-- MODAL DELETE LINK --> | |
4 | +<link rel="stylesheet" type="text/css" href="{% static 'css/link.css' %}"> | |
5 | + | |
6 | +<div class="erro-update"> | |
7 | + <div class="modal fade" id="linkDeleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteLinkLabel" style="z-index: 10"> | |
8 | + <div class="modal-dialog" role="document"> | |
9 | + <div class="modal-content"> | |
10 | + <div class="modal-header"> | |
11 | + <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> | |
12 | + <h4 class="modal-title" id="deleteLinkLabel">{% trans 'Delete Link' %}</h4> | |
13 | + </div> | |
14 | + <div class="modal-body"> | |
15 | + <!-- Card --> | |
16 | + <form class="form-horizontal" method="post" id="form-delete-link" enctype="multipart/form-data"> | |
17 | + {% csrf_token %} | |
18 | + <fieldset> | |
19 | + <div class="col-md-12"> | |
20 | + {% trans "Are you sure to delete the link " %} <strong>"{{ link.name }}"</strong> </a> of {{ link.topic.name }}? | |
21 | + </div> | |
22 | + <div class="form-group"> | |
23 | + <div class="col-md-12"> | |
24 | + <button type="button" class="btn btn-danger btn-raised" data-dismiss="modal">{% trans "Close" %}</button> | |
25 | + <button class="btn btn-raised btn-primary" type="submit">{% trans 'Delete' %}</button> | |
26 | + </div> | |
27 | + </div> | |
28 | + </fieldset> | |
29 | + </form> | |
30 | + <!-- .end Card --> | |
31 | + </div> | |
32 | + </div> | |
33 | + </div> | |
19 | 34 | </div> |
20 | - </div> | |
21 | 35 | </div> |
22 | -<!-- END --> | |
36 | + | |
37 | +{% block script_link %} | |
38 | + | |
39 | + {# // <script src="{% static 'js/link.js' %}"></script> #} | |
40 | + <script type="text/javascript"> | |
41 | + $("#form-delete-link").submit(function(event) { | |
42 | + var data = new FormData($('#form-delete-link').get(0)); | |
43 | + $.ajax({ | |
44 | + url: "{% url 'course:links:delete_link' link.slug %}", | |
45 | + type: $("#form-delete-link").attr('method'), | |
46 | + data: data, | |
47 | + cache: false, | |
48 | + processData: false, | |
49 | + contentType: false, | |
50 | + success: function(data) { | |
51 | + $('#linkDeleteModal').modal('hide'); | |
52 | + $('#link_{{ link.slug }}').remove(); | |
53 | + $('#link_edit_icon_{{ link.slug }}').remove(); | |
54 | + $('#link_edit_{{ link.slug }}').remove(); | |
55 | + alertify.alert('Link successfully deleted!') | |
56 | + }, | |
57 | + error: function(data){ | |
58 | + // $('.erro-update').html(data.responseText); | |
59 | + $('.modal-backdrop').remove(); | |
60 | + $('#linkDeteleModal').modal(); | |
61 | + alertify.alert('Error when trying to delete.'); | |
62 | + } | |
63 | + }); | |
64 | + event.preventDefault(); | |
65 | + }); | |
66 | + </script> | |
67 | +{% endblock script_link %} | |
68 | +<!-- EndModal --> | ... | ... |
links/templates/links/render_link.html
1 | -<li><i class="fa fa-link" aria-hidden="true"></i> <a href="javascript:get_modal_link('{% url 'course:links:view_link' link.slug %}', '#viewLinkModal','#divModalLink')">{{link}}</a></li> | |
1 | +<li id="link_{{ link.slug }}"><i class="fa fa-link" aria-hidden="true"></i> <a href="javascript:get_modal_link('{% url 'course:links:view_link' link.slug %}', '#viewLinkModal','#divModalLink')">{{link.name}}</a></li> | ... | ... |
links/templates/links/update_link.html
... | ... | @@ -5,29 +5,73 @@ |
5 | 5 | <div class="modal-content"> |
6 | 6 | <div class="modal-header"> |
7 | 7 | <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> |
8 | - <h4 class="modal-title" id="myModalLabel">{% trans 'Links' %}</h4> | |
8 | + <h4 class="modal-title" id="myModalLabel">{% trans 'Edit Link' %}</h4> | |
9 | 9 | </div> |
10 | 10 | <div class="modal-body"> |
11 | 11 | <!-- Card --> |
12 | + <form class="form-horizontal" method="post" id="form-update-link" enctype="multipart/form-data"> | |
13 | + {% csrf_token %} | |
14 | + {% for field in form %} | |
15 | + <div class="form-group is-empty"> | |
16 | + <div class="col-md-12"> | |
17 | + {% if field.field.required %} | |
18 | + <label for="{{ field.auto_id }}" class="control-label">{{ field.label }}<span>*</span></label> | |
19 | + {% else %} | |
20 | + <label for="{{ field.auto_id }}" class=" control-label">{{ field.label }}</label> | |
21 | + {% endif %} | |
22 | + {% render_field field class='form-control input-sm' %} | |
23 | + </div> | |
24 | + </div> | |
25 | + {% if field.errors %} | |
26 | + <div class="alert alert-danger alert-dismissible clearfix" role="alert"> | |
27 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
28 | + <span aria-hidden="true">×</span> | |
29 | + </button> | |
30 | + <ul> | |
31 | + {% for error in field.errors %} | |
32 | + <li>{{ error }}</li> | |
33 | + {% endfor %} | |
34 | + </ul> | |
35 | + </div> | |
36 | + {% endif %} | |
37 | + {% endfor %} | |
12 | 38 | <div class="form-group"> |
13 | - <label class="control-label" for="inputDefault">{% trans 'Name' %}</label> | |
14 | - <input value="Python" type="text" class="form-control" id="inputDefault"> | |
39 | + <button type="button" class="btn btn-danger btn-raised" data-dismiss="modal">{% trans "Cancel" %}</button> | |
40 | + <button class="btn btn-raised btn-primary" type="submit">{% trans 'Submit' %}</button> | |
15 | 41 | </div> |
16 | - <div class="form-group"> | |
17 | - <label class="control-label" for="inputDefault">{% trans 'URL' %}</label> | |
18 | - <input value="https://www.python.org/" type="text" class="form-control" id="inputDefault"> | |
19 | - </div> | |
20 | - <div class="form-group is-empty"> | |
21 | - <label class="control-label" for="inputDefault">{% trans 'Description' %}</label> | |
22 | - <textarea class="form-control" rows="3"></textarea> | |
23 | - </div> | |
24 | - <div class="form-group"> | |
25 | - <a href="javascript:void(0)" class="btn btn-raised btn-default" data-dismiss="modal">{% trans 'Cancel' %}</a> | |
26 | - <a href="javascript:void(0)" class="btn btn-raised btn-primary">{% trans 'Submit' %}</a> | |
27 | - </div> | |
28 | 42 | <!-- .end Card --> |
29 | 43 | </div> |
30 | 44 | </div> |
31 | 45 | </div> |
32 | 46 | </div> |
33 | 47 | <!-- EndModal --> |
48 | +{% block script_link %} | |
49 | + | |
50 | + {# // <script src="{% static 'js/links.js' %}"></script> #} | |
51 | + <script type="text/javascript"> | |
52 | + $("#form-update-link").submit(function(event) { | |
53 | + var data = new FormData($('#form-update-link').get(0)); | |
54 | + $.ajax({ | |
55 | + url: "{% url 'course:links:update_link' link.slug %}", | |
56 | + type: $("#form-update-link").attr('method'), | |
57 | + data: data, | |
58 | + cache: false, | |
59 | + processData: false, | |
60 | + contentType: false, | |
61 | + success: function(data) { | |
62 | + $('#linksModalEdit').modal('hide'); | |
63 | + $('#link_edit_{{ link.slug }}').replaceWith(data); | |
64 | + $('#link_{{ link.slug }}').replaceWith(data); | |
65 | + alertify.alert("Sucessfully Updated!") | |
66 | + }, | |
67 | + error: function(data){ | |
68 | + $('.erro-update').html(data.responseText); | |
69 | + $('.modal-backdrop').remove(); | |
70 | + $('#linksModalEdit').modal(); | |
71 | + alertify.alert('Invalid link, insert a valid one!'); | |
72 | + } | |
73 | + }); | |
74 | + event.preventDefault(); | |
75 | + }); | |
76 | + </script> | |
77 | +{% endblock script_link %} | ... | ... |
links/tests.py
... | ... | @@ -154,7 +154,9 @@ class LinkTestCase(TestCase): |
154 | 154 | url = reverse('course:links:update_link',kwargs={'slug': self.link.slug}) |
155 | 155 | print("slug",self.link.slug) |
156 | 156 | data = { |
157 | + "name" : 'testinglink', | |
157 | 158 | "link_description":'new description', |
159 | + "link_url" : 'teste.com', | |
158 | 160 | } |
159 | 161 | self.assertEqual(Link.objects.all()[0].link_description, "testdescription") # old description |
160 | 162 | response = self.client.post(url, data) | ... | ... |
links/urls.py
... | ... | @@ -3,8 +3,8 @@ from . import views |
3 | 3 | |
4 | 4 | urlpatterns = [ |
5 | 5 | url(r'^create_link/(?P<slug>[\w_-]+)/$', views.CreateLink.as_view(), name='create_link'), |
6 | - url(r'^deletelink/(?P<linkname>[\w_-]+)/$', views.deleteLink,name = 'delete_link'), | |
7 | - url(r'^updatelink/(?P<slug>[\w_-]+)/$', views.UpdateLink.as_view(),name = 'update_link'), | |
8 | - url(r'^render-link/(?P<id>[0-9]+)/$', views.render_link, name='render_link'), | |
6 | + url(r'^delete_link/(?P<slug>[\w_-]+)/$', views.DeleteLink.as_view(),name = 'delete_link'), | |
7 | + url(r'^update_link/(?P<slug>[\w_-]+)/$', views.UpdateLink.as_view(),name = 'update_link'), | |
8 | + url(r'^render-link/(?P<slug>[\w_-]+)/$', views.render_link, name='render_link'), | |
9 | 9 | url(r'^view_link/(?P<slug>[\w_-]+)/$',views.ViewLink.as_view(),name = 'view_link') |
10 | 10 | ] | ... | ... |
links/views.py
... | ... | @@ -9,7 +9,7 @@ from rolepermissions.mixins import HasRoleMixin |
9 | 9 | from core.mixins import NotificationMixin |
10 | 10 | from django.urls import reverse |
11 | 11 | from django.core.files.base import ContentFile |
12 | - | |
12 | +from rolepermissions.verifications import has_role | |
13 | 13 | |
14 | 14 | from .image_crawler import * |
15 | 15 | from courses.models import Topic |
... | ... | @@ -62,20 +62,37 @@ class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.Cr |
62 | 62 | context["topic"] = topic |
63 | 63 | return context |
64 | 64 | def get_success_url(self): |
65 | - self.success_url = redirect('course:links:render_link', id = self.object.id) | |
65 | + self.success_url = redirect('course:links:render_link', slug = self.object.slug) | |
66 | 66 | return self.success_url |
67 | -def deleteLink(request,linkname): | |
68 | - link = get_object_or_404(Link,name = linkname) | |
69 | - link.delete() | |
70 | - template_name = 'links/delete_link.html' | |
71 | - messages.success(request,_("Link deleted Successfully!")) | |
72 | 67 | |
73 | - return redirect('course:manage') | |
68 | +class DeleteLink(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): | |
69 | + allowed_roles = ['professor', 'system_admin'] | |
70 | + login_url = reverse_lazy("core:home") | |
71 | + redirect_field_name = 'next' | |
72 | + model = Link | |
73 | + template_name = 'links/delete_link.html' | |
74 | + | |
75 | + def dispatch(self, *args, **kwargs): | |
76 | + link = get_object_or_404(Link, slug = self.kwargs.get('slug')) | |
77 | + if(not (link.topic.owner == self.request.user) and not(has_role(self.request.user, 'system_admin')) ): | |
78 | + return self.handle_no_permission() | |
79 | + return super(DeleteLink, self).dispatch(*args, **kwargs) | |
80 | + | |
81 | + def get_context_data(self, **kwargs): | |
82 | + context = super(DeleteLink, self).get_context_data(**kwargs) | |
83 | + context['course'] = self.object.topic.subject.course | |
84 | + context['subject'] = self.object.topic.subject | |
85 | + context['link'] = self.object | |
86 | + context["topic"] = self.object.topic | |
87 | + return context | |
74 | 88 | |
75 | -def render_link(request, id): | |
89 | + def get_success_url(self): | |
90 | + return reverse_lazy('course:view_topic', kwargs={'slug' : self.object.topic.slug}) | |
91 | + | |
92 | +def render_link(request, slug): | |
76 | 93 | template_name = 'links/render_link.html' |
77 | 94 | context = { |
78 | - 'link': get_object_or_404(Link, id = id) | |
95 | + 'link': get_object_or_404(Link, slug = slug) | |
79 | 96 | } |
80 | 97 | return render(request, template_name, context) |
81 | 98 | |
... | ... | @@ -85,18 +102,41 @@ class UpdateLink(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): |
85 | 102 | template_name = 'links/update_link.html' |
86 | 103 | form_class = UpdateLinkForm |
87 | 104 | success_url = reverse_lazy('course:links:render_link') |
88 | - def form_valid(self, form): | |
89 | - form.save() | |
90 | - messages.success(self.request, _('Link updated successfully!')) | |
91 | 105 | |
92 | - return super(UpdateLink, self).form_valid(form) | |
106 | + def form_invalid(self,form): | |
107 | + context = super(UpdateLink, self).form_invalid(form) | |
108 | + context.status_code = 400 | |
93 | 109 | |
110 | + return context | |
111 | + def form_valid(self, form): | |
112 | + formulario = form | |
113 | + if formulario.has_changed(): | |
114 | + if 'link_url' in formulario.changed_data: | |
115 | + self.object = form.save() | |
116 | + self.link = Link.objects.get(slug = self.object.slug) | |
117 | + self.formato,self.baixado = get_images(self.link.link_url,self.link.slug) | |
118 | + self.caminho = 'links/static/images/%s'%(self.link.slug)+'%s'%(self.formato) | |
119 | + self.setImage() | |
120 | + else: | |
121 | + form.save() | |
122 | + else: | |
123 | + form.save() | |
124 | + return super(UpdateLink, self).form_valid(form) | |
125 | + def setImage(self): | |
126 | + if self.baixado: | |
127 | + with open(self.caminho,'rb') as f: | |
128 | + data = f.read() | |
129 | + nome = '%s'%(self.link.slug)+"%s"%(self.formato) | |
130 | + self.object.image.save(nome,ContentFile(data)) | |
131 | + else: | |
132 | + with open('links/static/images/default.jpg','rb') as f: | |
133 | + data = f.read() | |
134 | + self.object.image.save('default.jpg',ContentFile(data)) | |
94 | 135 | def get_object(self, queryset=None): |
95 | 136 | self.object = get_object_or_404(Link, slug = self.kwargs.get('slug')) |
96 | - print(self.object.link_description) | |
97 | 137 | return self.object |
98 | 138 | def get_success_url(self): |
99 | - self.success_url = redirect('course:links:render_link', id = self.object.id) | |
139 | + self.success_url = reverse_lazy('course:links:render_link', args = (self.object.slug, )) | |
100 | 140 | return self.success_url |
101 | 141 | class ViewLink(LoginRequiredMixin,HasRoleMixin,generic.DetailView): |
102 | 142 | allowed_roles = ['professor', 'system_admin'] |
... | ... | @@ -109,7 +149,7 @@ class ViewLink(LoginRequiredMixin,HasRoleMixin,generic.DetailView): |
109 | 149 | context['link'] = link |
110 | 150 | return context |
111 | 151 | def get_success_url(self): |
112 | - self.success_url = redirect('course:links:render_link', id = self.object.id) | |
152 | + self.success_url = redirect('course:links:render_link', slug = self.object.slug) | |
113 | 153 | return self.success_url |
114 | 154 | def get_queryset(self): |
115 | 155 | self.queryset = Link.objects.filter(slug = self.kwargs.get('slug')) | ... | ... |