Commit f670705ed931313558de51b38f8b18d2b9b98515
Exists in
master
and in
5 other branches
Merge branch 'dev' of https://github.com/amadeusproject/amadeuslms into dev
Showing
4 changed files
with
207 additions
and
13 deletions
Show diff stats
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
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> |
@@ -0,0 +1,32 @@ | @@ -0,0 +1,32 @@ | ||
1 | +{% load static i18n list_topic_foruns permission_tags %} | ||
2 | +<!--MODAL VIEW LINK--> | ||
3 | +<div class="modal fade" data-backdrop="false" id="viewLinkModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> | ||
4 | + <div class="modal-dialog" role="document"> | ||
5 | + <div class="modal-content"> | ||
6 | + <div class="modal-header"> | ||
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">Link</h4> | ||
9 | + </div> | ||
10 | + <div class="modal-body"> | ||
11 | + <!-- Card --> | ||
12 | + <article class="card animated fadeInLeft"> | ||
13 | + <img class="card-img-top img-responsive" src="https://www.python.org/static/opengraph-icon-200x200.png" align="left"> | ||
14 | + <div class="card-block"> | ||
15 | + <b class="card-title">{{link.name}}</b><p></p> | ||
16 | + <p class="card-text"> </p><p>{{link.link_description}}</p> | ||
17 | + <a href="{{ link.link_url }}" class="btn btn-primary">Read more</a> | ||
18 | + </div> | ||
19 | + </article> | ||
20 | + <!-- .end Card --> | ||
21 | + </div> | ||
22 | + </div> | ||
23 | + </div> | ||
24 | +</div> | ||
25 | + | ||
26 | +<div class = 'row' id ="divModalLink"> | ||
27 | + | ||
28 | +</div> | ||
29 | +<!-- EndModal --> | ||
30 | +<script type="text/javascript"> | ||
31 | +$('.modal-backdrop').remove(); | ||
32 | +</script> |
links/tests.py
@@ -6,6 +6,7 @@ from django.utils.translation import ugettext_lazy as _ | @@ -6,6 +6,7 @@ from django.utils.translation import ugettext_lazy as _ | ||
6 | from users.models import User | 6 | from users.models import User |
7 | from .models import * | 7 | from .models import * |
8 | from .forms import * | 8 | from .forms import * |
9 | +from courses.models import CourseCategory, Course, Subject, Topic | ||
9 | 10 | ||
10 | # Create your tests here. | 11 | # Create your tests here. |
11 | class LinkTestCase(TestCase): | 12 | class LinkTestCase(TestCase): |
@@ -19,6 +20,62 @@ class LinkTestCase(TestCase): | @@ -19,6 +20,62 @@ class LinkTestCase(TestCase): | ||
19 | password = 'testing' | 20 | password = 'testing' |
20 | ) | 21 | ) |
21 | assign_role(self.user, 'system_admin') | 22 | assign_role(self.user, 'system_admin') |
23 | + self.teacher = User.objects.create_user( | ||
24 | + username = 'teacher', | ||
25 | + email = 'teacherg@school.com', | ||
26 | + is_staff = True, | ||
27 | + is_active = True, | ||
28 | + password = 'teaching' | ||
29 | + ) | ||
30 | + assign_role(self.teacher, 'professor') | ||
31 | + | ||
32 | + self.user_student = User.objects.create_user( | ||
33 | + username = 'student', | ||
34 | + email = 'student@amadeus.com', | ||
35 | + is_staff = False, | ||
36 | + is_active = True, | ||
37 | + password = 'testing', | ||
38 | + type_profile = 2 | ||
39 | + ) | ||
40 | + assign_role(self.user_student, 'student') | ||
41 | + | ||
42 | + self.category = CourseCategory( | ||
43 | + name = 'Categoria Teste', | ||
44 | + slug = 'categoria_teste' | ||
45 | + ) | ||
46 | + self.category.save() | ||
47 | + | ||
48 | + self.course = Course( | ||
49 | + name = 'Curso Teste', | ||
50 | + slug = 'curso_teste', | ||
51 | + max_students = 50, | ||
52 | + init_register_date = '2016-08-26', | ||
53 | + end_register_date = '2016-10-01', | ||
54 | + init_date = '2016-10-05', | ||
55 | + end_date = '2017-10-05', | ||
56 | + category = self.category, | ||
57 | + public = True, | ||
58 | + ) | ||
59 | + self.course.save() | ||
60 | + self.subject = Subject( | ||
61 | + name = 'Subject Test', | ||
62 | + description = "description of the subject test", | ||
63 | + visible = True, | ||
64 | + init_date = '2016-10-05', | ||
65 | + end_date = '2017-10-05', | ||
66 | + course = self.course, | ||
67 | + ) | ||
68 | + self.subject.save() | ||
69 | + self.subject.professors.add(self.teacher) | ||
70 | + | ||
71 | + self.topic = Topic( | ||
72 | + name = 'Topic Test', | ||
73 | + description = "description of the topic test", | ||
74 | + subject = self.subject, | ||
75 | + owner = self.teacher, | ||
76 | + ) | ||
77 | + self.topic.save() | ||
78 | + ''' | ||
22 | def test_create_link(self): | 79 | def test_create_link(self): |
23 | self.client.login(username='user', password = 'testing') | 80 | self.client.login(username='user', password = 'testing') |
24 | links = Link.objects.all().count() | 81 | links = Link.objects.all().count() |
@@ -42,19 +99,78 @@ class LinkTestCase(TestCase): | @@ -42,19 +99,78 @@ class LinkTestCase(TestCase): | ||
42 | } | 99 | } |
43 | response = self.client.post(url, data,format = 'json') | 100 | response = self.client.post(url, data,format = 'json') |
44 | self.assertEqual(Link.objects.filter(name= data['name']).exists(),False) #Verificada não existência do link com campo errado | 101 | self.assertEqual(Link.objects.filter(name= data['name']).exists(),False) #Verificada não existência do link com campo errado |
102 | + ''' | ||
103 | + def test_create_link_teacher(self): | ||
104 | + self.client.login(username='teacher', password = 'teaching') | ||
105 | + links = Link.objects.all().count() | ||
106 | + self.assertEqual(Link.objects.all().count(),links) #Before creating the link | ||
107 | + topic = Topic.objects.get(name = 'Topic Test') | ||
108 | + url = reverse('course:links:create_link',kwargs={'slug': topic.slug}) | ||
109 | + data = { | ||
110 | + 'name' : 'testinglink', | ||
111 | + "link_description" : 'testdescription', | ||
112 | + "link_url" : 'teste.com' | ||
113 | + } | ||
114 | + data['topic'] = topic | ||
115 | + response = self.client.post(url, data,format = 'json') | ||
116 | + link1 = Link.objects.get(name = data['name']) #Link criado com os dados inseridos corretamente | ||
117 | + self.assertEqual(Link.objects.filter(name= link1.name).exists(),True) #Verificada existência do link | ||
118 | + self.assertEqual(Link.objects.all().count(),links+1) #After creating link1, if OK, the link was created successfully. | ||
119 | + self.assertEqual(response.status_code, 302) #If OK, User is getting redirected correctly. | ||
120 | + self.assertTemplateUsed(template_name = 'links/create_link.html') | ||
121 | + data = { | ||
122 | + 'name' : 'testlink2', | ||
123 | + "link_description" : 'testdescription2', | ||
124 | + "link_url" : 'teste', | ||
125 | + } | ||
126 | + data['topic'] = topic | ||
127 | + response = self.client.post(url, data,format = 'json') | ||
128 | + self.assertEqual(Link.objects.filter(name= data['name']).exists(),False) #Verificada não existência do link com campo errado | ||
129 | + | ||
130 | + def test_create_link_student(self): | ||
131 | + self.client.login(username='student', password = 'testing') | ||
132 | + topic = Topic.objects.get(name = 'Topic Test') | ||
133 | + links = Link.objects.all().count() | ||
134 | + self.assertEqual(Link.objects.all().count(),links) #Before creating the link | ||
135 | + url = reverse('course:links:create_link',kwargs={'slug': topic.slug}) | ||
136 | + data = { | ||
137 | + 'name' : 'testinglink', | ||
138 | + "description" : 'testdescription', | ||
139 | + "link" : 'teste.com' | ||
140 | + } | ||
141 | + data['topic'] = topic | ||
142 | + response = self.client.post(url, data,format = 'json') | ||
143 | + self.assertEqual(response.status_code, 403) #Status code = 403, Permissão negada para usuário estudante. | ||
45 | 144 | ||
46 | - # def test_update_link(): | ||
47 | - # pass | 145 | + def test_update_link(self): |
146 | + self.client.login(username='teacher', password = 'teaching') | ||
147 | + topic = Topic.objects.get(name = 'Topic Test') | ||
148 | + self.link = Link.objects.create( | ||
149 | + name = 'testinglink', | ||
150 | + link_description = 'testdescription', | ||
151 | + link_url = 'teste.com', | ||
152 | + topic = topic | ||
153 | + ) | ||
154 | + url = reverse('course:links:update_link',kwargs={'slug': self.link.slug}) | ||
155 | + print("slug",self.link.slug) | ||
156 | + data = { | ||
157 | + "link_description":'new description', | ||
158 | + } | ||
159 | + self.assertEqual(Link.objects.all()[0].link_description, "testdescription") # old description | ||
160 | + response = self.client.post(url, data) | ||
161 | + self.assertEqual(Link.objects.all()[0].link_description, 'new description') # new description | ||
48 | def test_delete_link(self): | 162 | def test_delete_link(self): |
163 | + topic = Topic.objects.get(name = 'Topic Test') | ||
49 | self.link = Link.objects.create( | 164 | self.link = Link.objects.create( |
50 | name = 'testinglink', | 165 | name = 'testinglink', |
51 | - description = 'testdescription', | ||
52 | - link = 'teste.com' | 166 | + link_description = 'testdescription', |
167 | + link_url = 'teste.com', | ||
168 | + topic = topic | ||
53 | ) | 169 | ) |
54 | self.client.login(username='user', password = 'testing') | 170 | self.client.login(username='user', password = 'testing') |
55 | links = Link.objects.all().count() | 171 | links = Link.objects.all().count() |
56 | deletedlink = Link.objects.get(name = self.link.name) | 172 | deletedlink = Link.objects.get(name = self.link.name) |
57 | - url = reverse('course:delete_link',kwargs={'linkname': self.link.name}) | 173 | + url = reverse('course:links:delete_link',kwargs={'linkname': self.link.name}) |
58 | self.assertEqual(Link.objects.all().count(),links) | 174 | self.assertEqual(Link.objects.all().count(),links) |
59 | response = self.client.post(url) | 175 | response = self.client.post(url) |
60 | self.assertEqual(Link.objects.all().count(),links - 1) #Objeto removido | 176 | self.assertEqual(Link.objects.all().count(),links - 1) #Objeto removido |
links/views.py
@@ -4,42 +4,87 @@ from django.core.urlresolvers import reverse_lazy | @@ -4,42 +4,87 @@ from django.core.urlresolvers import reverse_lazy | ||
4 | from django.contrib import messages | 4 | from django.contrib import messages |
5 | from django.utils.translation import ugettext_lazy as _ | 5 | from django.utils.translation import ugettext_lazy as _ |
6 | from django.shortcuts import get_object_or_404,redirect | 6 | from django.shortcuts import get_object_or_404,redirect |
7 | +from django.contrib.auth.mixins import LoginRequiredMixin | ||
8 | +from rolepermissions.mixins import HasRoleMixin | ||
7 | 9 | ||
10 | +from courses.models import Topic | ||
8 | from .models import Link | 11 | from .models import Link |
9 | from .forms import * | 12 | from .forms import * |
10 | 13 | ||
11 | # Create your views here. | 14 | # Create your views here. |
12 | -class CreateLink(generic.CreateView): | 15 | +class CreateLink(LoginRequiredMixin, HasRoleMixin, generic.CreateView): |
16 | + allowed_roles = ['professor', 'system_admin'] | ||
13 | template_name = 'links/create_link.html' | 17 | template_name = 'links/create_link.html' |
14 | form_class = CreateLinkForm | 18 | form_class = CreateLinkForm |
15 | success_url = reverse_lazy('course:manage') | 19 | success_url = reverse_lazy('course:manage') |
16 | context_object_name = 'form' | 20 | context_object_name = 'form' |
17 | 21 | ||
18 | def form_valid(self, form): | 22 | def form_valid(self, form): |
19 | - form.save() | 23 | + self.object = form.save(commit = False) |
24 | + topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) | ||
25 | + self.object.topic = topic | ||
20 | messages.success(self.request, _('Link created successfully!')) | 26 | messages.success(self.request, _('Link created successfully!')) |
21 | - messages.error(self.request, _("An error occurred when trying to create the link")) | ||
22 | - return super(CreateLink, self).form_valid(form) | 27 | + #messages.error(self.request, _("An error occurred when trying to create the link")) |
28 | + self.object.save() | ||
29 | + #return self.success_url | ||
30 | + return self.get_success_url() | ||
23 | def get_context_data(self,**kwargs): | 31 | def get_context_data(self,**kwargs): |
24 | context = {} | 32 | context = {} |
25 | context['links'] = Link.objects.all() | 33 | context['links'] = Link.objects.all() |
26 | context['form'] = CreateLinkForm | 34 | context['form'] = CreateLinkForm |
35 | + topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) | ||
36 | + context["topic"] = topic | ||
27 | return context | 37 | return context |
28 | - | 38 | + def get_success_url(self): |
39 | + self.success_url = redirect('course:links:render_link', id = self.object.id) | ||
40 | + return self.success_url | ||
29 | def deleteLink(request,linkname): | 41 | def deleteLink(request,linkname): |
30 | link = get_object_or_404(Link,name = linkname) | 42 | link = get_object_or_404(Link,name = linkname) |
31 | link.delete() | 43 | link.delete() |
32 | template_name = 'links/delete_link.html' | 44 | template_name = 'links/delete_link.html' |
33 | messages.success(request,_("Link deleted Successfully!")) | 45 | messages.success(request,_("Link deleted Successfully!")) |
34 | - messages.error(request, _("An error occurred when trying to delete the link")) | 46 | + #messages.error(request, _("An error occurred when trying to delete the link")) |
35 | return redirect('course:manage') | 47 | return redirect('course:manage') |
48 | + | ||
49 | +def render_link(request, id): | ||
50 | + template_name = 'links/render_link.html' | ||
51 | + context = { | ||
52 | + 'link': get_object_or_404(Link, id = id) | ||
53 | + } | ||
54 | + return render(request, template_name, context) | ||
55 | + | ||
36 | #Referencia no delete link para adicionar quando resolver o problema do context {% url 'course:delete' link.name %} | 56 | #Referencia no delete link para adicionar quando resolver o problema do context {% url 'course:delete' link.name %} |
37 | -class UpdateLink(generic.UpdateView): | 57 | +class UpdateLink(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): |
58 | + allowed_roles = ['professor', 'system_admin'] | ||
38 | template_name = 'links/update_link.html' | 59 | template_name = 'links/update_link.html' |
39 | form_class = UpdateLinkForm | 60 | form_class = UpdateLinkForm |
40 | - success_url = reverse_lazy('course:manage') | 61 | + success_url = reverse_lazy('course:links:render_link') |
41 | def form_valid(self, form): | 62 | def form_valid(self, form): |
42 | form.save() | 63 | form.save() |
43 | messages.success(self.request, _('Link updated successfully!')) | 64 | messages.success(self.request, _('Link updated successfully!')) |
44 | 65 | ||
45 | return super(UpdateLink, self).form_valid(form) | 66 | return super(UpdateLink, self).form_valid(form) |
67 | + | ||
68 | + def get_object(self, queryset=None): | ||
69 | + self.object = get_object_or_404(Link, slug = self.kwargs.get('slug')) | ||
70 | + print(self.object.link_description) | ||
71 | + return self.object | ||
72 | + def get_success_url(self): | ||
73 | + self.success_url = redirect('course:links:render_link', id = self.object.id) | ||
74 | + return self.success_url | ||
75 | +class ViewLink(LoginRequiredMixin,HasRoleMixin,generic.DetailView): | ||
76 | + allowed_roles = ['professor', 'system_admin'] | ||
77 | + template_name = 'links/view_link.html' | ||
78 | + success_url = reverse_lazy('course:links:render_link') | ||
79 | + context_object_name = 'link' | ||
80 | + def get_context_data(self,**kwargs): | ||
81 | + context = {} | ||
82 | + link = Link.objects.get(slug = self.kwargs.get('slug')) | ||
83 | + context['link'] = link | ||
84 | + return context | ||
85 | + def get_success_url(self): | ||
86 | + self.success_url = redirect('course:links:render_link', id = self.object.id) | ||
87 | + return self.success_url | ||
88 | + def get_queryset(self): | ||
89 | + self.queryset = Link.objects.filter(slug = self.kwargs.get('slug')) | ||
90 | + return self.queryset |