Commit 604de34dcac460007964017742ded43b4009d052
1 parent
a3fa7886
Exists in
master
and in
5 other branches
Template de ver enquete #146
Showing
3 changed files
with
74 additions
and
162 deletions
Show diff stats
poll/templates/poll/view.html
1 | -{% extends "topic/index.html" %} | 1 | +{% extends "poll/create.html" %} |
2 | 2 | ||
3 | -{% load i18n widget_tweaks dict_access static%} | ||
4 | -{% block content %} | ||
5 | -<!-- Modal (remember to change the ids!!!) --> | ||
6 | -<div class="modal fade" id="poll" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> | ||
7 | - <div class="modal-dialog" role="document"> | ||
8 | - <div class="modal-content"> | 3 | +{% load i18n dict_access static%} |
9 | 4 | ||
10 | - <!-- Modal Header --> | ||
11 | - <div class="modal-header"> | 5 | +{% block style %} |
6 | +{% endblock %} | ||
12 | 7 | ||
13 | - <!-- Put your title here!!! --> | ||
14 | - <h4 class="modal-title" id="myModalLabel">{{form.question}}</h4> | 8 | +{% block title_poll %} |
9 | +<!-- Put your title here!!! --> | ||
10 | +<h4 class="modal-title" id="myModalLabel">{{poll}}</h4> | ||
11 | +{% endblock title_poll %} | ||
15 | 12 | ||
16 | - </div> | ||
17 | - <!-- Modal Body --> | ||
18 | - <div class="modal-body"> | ||
19 | - | ||
20 | - <!-- Put ONLY your content here!!! --> | ||
21 | - <div class="conteiner"> | ||
22 | - </div> | ||
23 | - <form id="form" method="post"> | ||
24 | - {% csrf_token %} | ||
25 | - {% for key in keys %} | ||
26 | - <div class="row form-group"> | ||
27 | - <div class="col-md-1"> | ||
28 | - </br> | ||
29 | - <label><span class="glyphicon glyphicon-move"></span></label> | ||
30 | - </div> | ||
31 | - <div class="col-md-10"> | ||
32 | - <div class="has-success is-empty"> | ||
33 | - <input type="text" name="{{key}}" class="form-control" placeholder='{% trans "Answer" %}' value="{{ answers|value:key }}"> | ||
34 | - <span class="help-block">{% trans "Possible answer for the question" %}</span> | ||
35 | - </div> | ||
36 | - </div> | ||
37 | - <div class="col-md-1"> | ||
38 | - </br> | ||
39 | - <label><span class="glyphicon glyphicon-remove" onclick="this.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode);"></span></label> | ||
40 | - </div> | ||
41 | - </div> | ||
42 | - {% empty %} | ||
43 | - <div class="row form-group"> | ||
44 | - <div class="col-md-1"> | ||
45 | - </br> | ||
46 | - <label><span class="glyphicon glyphicon-move"></span></label> | ||
47 | - </div> | ||
48 | - <div class="col-md-10"> | ||
49 | - <div class="has-success is-empty"> | ||
50 | - <input type="text" name="1" class="form-control" placeholder='{% trans "Answer" %}'> | ||
51 | - <span class="help-block">{% trans "Possible answer for the question" %}</span> | ||
52 | - </div> | ||
53 | - </div> | ||
54 | - <div class="col-md-1"> | ||
55 | - </br> | ||
56 | - <label><span class="glyphicon glyphicon-remove" onclick="this.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode);"></span></label> | ||
57 | - </div> | ||
58 | - </div> | ||
59 | - {% endfor %} | ||
60 | - </form> | ||
61 | - </br> | ||
62 | - </div> | ||
63 | - <button type="button" id="add" class="btn btn-primary btn-block btn-sm">add</button> | ||
64 | - <div class="row form-group"> | ||
65 | - <input form="form" class="form-control" type="date" name="{{form.limit_date.name}}" {% if form.limit_date.value != None %}value="{% if form.limit_date.value.year %}{{form.limit_date.value|date:'Y-m-d'}}{% else %}{{form.limit_date.value}}{% endif %}"{% endif %}> | ||
66 | - {% if form.limit_date.errors %} | ||
67 | - <div class="not_submited"> | ||
68 | - </br> | ||
69 | - <div class="alert alert-danger alert-dismissible" role="alert"> | ||
70 | - <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
71 | - <span aria-hidden="true">×</span> | ||
72 | - </button> | ||
73 | - <ul> | ||
74 | - {% for error in form.limit_date.errors %} | ||
75 | - <li>{{ error }}</li> | ||
76 | - {% endfor %} | ||
77 | - </ul> | ||
78 | - </div> | ||
79 | - </div> | ||
80 | - {% endif %} | ||
81 | - </div> | ||
82 | - | ||
83 | - </div> | ||
84 | - | ||
85 | - <!-- Modal Footer --> | ||
86 | - <div class="modal-footer"> | ||
87 | - | ||
88 | - <!-- Don't remove that!!! --> | ||
89 | - <button type="button" class="btn btn-danger btn-raised" data-dismiss="modal">{% trans "Close" %}</button> | ||
90 | - | ||
91 | - <!-- Put curtom buttons here!!! --> | ||
92 | - <button type="submite" id="button" form="form" class="btn btn-primary btn-raised">{% trans "Create" %}</button> | ||
93 | - </div> | ||
94 | - | ||
95 | - </div> | 13 | +{% block content_poll %} |
14 | +<!-- Put ONLY your content here!!! --> | ||
15 | +<div class="row"> | ||
16 | + <div class="col-md-10 col-md-offset-1"> | ||
17 | + <h3>{% trans "Limit date:" %} {{poll.limit_date|date:'d/m/y'}}</h3> | ||
96 | </div> | 18 | </div> |
97 | </div> | 19 | </div> |
98 | -<a href="" data-toggle="modal" data-target="#poll">modal</a> | ||
99 | -{% endblock content %} | 20 | +{% endblock content_poll %} |
21 | + | ||
22 | +{% block button_save %} | ||
23 | +<!-- Put curtom buttons here!!! --> | ||
24 | +<button type="submite" id="button" form="form" class="btn btn-primary btn-raised">{% trans "Answer" %}</button> | ||
25 | +{% endblock button_save %} |
poll/urls.py
@@ -3,10 +3,9 @@ from django.conf.urls import url | @@ -3,10 +3,9 @@ from django.conf.urls import url | ||
3 | from . import views | 3 | from . import views |
4 | 4 | ||
5 | urlpatterns = [ | 5 | urlpatterns = [ |
6 | + url(r'^view/(?P<slug>[\w\-_]+)/$', views.ViewPoll.as_view(), name='view_poll'), # poll slug | ||
6 | url(r'^create/(?P<slug>[\w\-_]+)/$', views.CreatePoll.as_view(), name='create_poll'), # topic slug | 7 | url(r'^create/(?P<slug>[\w\-_]+)/$', views.CreatePoll.as_view(), name='create_poll'), # topic slug |
7 | url(r'^update/(?P<slug>[\w\-_]+)/$', views.UpdatePoll.as_view(), name='update_poll'), # poll slug | 8 | url(r'^update/(?P<slug>[\w\-_]+)/$', views.UpdatePoll.as_view(), name='update_poll'), # poll slug |
8 | - url(r'^teste/(?P<slug>[\w\-_]+)/$', views.CreatePollModal.as_view(), name='teste'), # poll | ||
9 | - url(r'^mymodal/', views.MyModal.as_view(), name='mymodal'), | ||
10 | url(r'^delete/(?P<slug>[\w\-_]+)/$', views.DeletePoll.as_view(), name='delete_poll'), # poll | 9 | url(r'^delete/(?P<slug>[\w\-_]+)/$', views.DeletePoll.as_view(), name='delete_poll'), # poll |
11 | 10 | ||
12 | ] | 11 | ] |
poll/views.py
@@ -17,6 +17,60 @@ from core.mixins import NotificationMixin | @@ -17,6 +17,60 @@ from core.mixins import NotificationMixin | ||
17 | from users.models import User | 17 | from users.models import User |
18 | from courses.models import Course, Topic | 18 | from courses.models import Course, Topic |
19 | 19 | ||
20 | +class ViewPoll(LoginRequiredMixin,generic.DetailView): | ||
21 | + | ||
22 | + model = Poll | ||
23 | + context_object_name = 'poll' | ||
24 | + template_name = 'poll/view.html' | ||
25 | + | ||
26 | + def get_object(self, queryset=None): | ||
27 | + return get_object_or_404(Poll, slug = self.kwargs.get('slug')) | ||
28 | + | ||
29 | + def form_invalid(self, form,**kwargs): | ||
30 | + context = super(ViewPoll, self).form_invalid(form) | ||
31 | + answers = {} | ||
32 | + for key in self.request.POST: | ||
33 | + if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | ||
34 | + answers[key] = self.request.POST[key] | ||
35 | + | ||
36 | + keys = sorted(answers) | ||
37 | + context.context_data['answers'] = answers | ||
38 | + context.context_data['keys'] = keys | ||
39 | + return context | ||
40 | + | ||
41 | + def form_valid(self, form): | ||
42 | + poll = self.object | ||
43 | + poll = form.save(commit = False) | ||
44 | + poll.answers.all().delete() | ||
45 | + poll.save() | ||
46 | + | ||
47 | + | ||
48 | + for key in self.request.POST: | ||
49 | + if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | ||
50 | + answer = Answer(answer=self.request.POST[key],order=key,poll=poll) | ||
51 | + answer.save() | ||
52 | + | ||
53 | + return super(ViewPoll, self).form_valid(form) | ||
54 | + | ||
55 | + def get_context_data(self, **kwargs): | ||
56 | + context = super(ViewPoll, self).get_context_data(**kwargs) | ||
57 | + poll = self.object | ||
58 | + context['course'] = poll.topic.subject.course | ||
59 | + context['subject'] = poll.topic.subject | ||
60 | + context['subjects'] = poll.topic.subject.course.subjects.all() | ||
61 | + | ||
62 | + answers = {} | ||
63 | + for answer in poll.answers.all(): | ||
64 | + answers[answer.order] = answer.answer | ||
65 | + | ||
66 | + keys = sorted(answers) | ||
67 | + context['answers'] = answers | ||
68 | + context['keys'] = keys | ||
69 | + | ||
70 | + print (context) | ||
71 | + return context | ||
72 | + | ||
73 | + | ||
20 | class CreatePoll(LoginRequiredMixin,HasRoleMixin,generic.CreateView): | 74 | class CreatePoll(LoginRequiredMixin,HasRoleMixin,generic.CreateView): |
21 | 75 | ||
22 | allowed_roles = ['professor', 'system_admin'] | 76 | allowed_roles = ['professor', 'system_admin'] |
@@ -152,70 +206,3 @@ class DeletePoll(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): | @@ -152,70 +206,3 @@ class DeletePoll(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): | ||
152 | 206 | ||
153 | def get_success_url(self): | 207 | def get_success_url(self): |
154 | return reverse_lazy('course:view_topic', kwargs={'slug' : self.object.topic.slug}) | 208 | return reverse_lazy('course:view_topic', kwargs={'slug' : self.object.topic.slug}) |
155 | - | ||
156 | - | ||
157 | -from django_modalview.generic.edit import ModalCreateView | ||
158 | -from django_modalview.generic.component import ModalResponse | ||
159 | - | ||
160 | -class CreatePollModal(LoginRequiredMixin,ModalCreateView): | ||
161 | - | ||
162 | - login_url = reverse_lazy("core:home") | ||
163 | - redirect_field_name = 'next' | ||
164 | - model = Poll | ||
165 | - form_class = PollForm | ||
166 | - context_object_name = 'poll' | ||
167 | - template_name = 'poll/create.html' | ||
168 | - success_url = reverse_lazy('core:home') | ||
169 | - | ||
170 | - def form_invalid(self, form,**kwargs): | ||
171 | - context = super(CreatePollModal, self).form_invalid(form) | ||
172 | - answers = {} | ||
173 | - for key in self.request.POST: | ||
174 | - if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | ||
175 | - answers[key] = self.request.POST[key] | ||
176 | - | ||
177 | - keys = sorted(answers) | ||
178 | - context.context_data['answers'] = answers | ||
179 | - context.context_data['keys'] = keys | ||
180 | - return context | ||
181 | - | ||
182 | - def form_valid(self, form): | ||
183 | - self.object = form.save(commit = False) | ||
184 | - topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) | ||
185 | - self.object.topic = topic | ||
186 | - self.object.save() | ||
187 | - | ||
188 | - for key in self.request.POST: | ||
189 | - if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | ||
190 | - answer = Answer(answer=self.request.POST[key],order=key,poll=self.object) | ||
191 | - answer.save() | ||
192 | - | ||
193 | - return super(CreatePollModal, self).form_valid(form) | ||
194 | - | ||
195 | - def get_context_data(self, **kwargs): | ||
196 | - context = super(CreatePollModal, self).get_context_data(**kwargs) | ||
197 | - topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) | ||
198 | - context['course'] = topic.subject.course | ||
199 | - context['subject'] = topic.subject | ||
200 | - context['subjects'] = topic.subject.course.subjects.all() | ||
201 | - return context | ||
202 | - | ||
203 | - | ||
204 | -from django_modalview.generic.base import ModalTemplateView | ||
205 | - | ||
206 | -class MyModal(ModalTemplateView): | ||
207 | - ''' | ||
208 | - This modal inherit of ModalTemplateView, so it just display a text without logic. | ||
209 | - ''' | ||
210 | - def __init__(self, *args, **kwargs): | ||
211 | - ''' | ||
212 | - You have to call the init method of the parent, before to overide the values: | ||
213 | - - title: The title display in the modal-header | ||
214 | - - icon: The css class that define the modal's icon | ||
215 | - - description: The content of the modal. | ||
216 | - - close_button: A button object that has several attributes.(explain below) | ||
217 | - ''' | ||
218 | - super(MyModal, self).__init__(*args, **kwargs) | ||
219 | - self.title = "My modal" | ||
220 | - self.description = "This is my description" | ||
221 | - self.icon = "icon-mymodal" |