Commit ed9a78ced7265c1e5bb7e32599ca5ed56b459657
1 parent
dbb6ae08
Exists in
master
and in
5 other branches
Criando o procedimento de criar e editar uma enquete pelo professor #78
Showing
10 changed files
with
268 additions
and
33 deletions
Show diff stats
... | ... | @@ -0,0 +1,21 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | +# Generated by Django 1.10 on 2016-09-30 04:24 | |
3 | +from __future__ import unicode_literals | |
4 | + | |
5 | +import autoslug.fields | |
6 | +from django.db import migrations | |
7 | + | |
8 | + | |
9 | +class Migration(migrations.Migration): | |
10 | + | |
11 | + dependencies = [ | |
12 | + ('core', '0001_initial'), | |
13 | + ] | |
14 | + | |
15 | + operations = [ | |
16 | + migrations.AlterField( | |
17 | + model_name='resource', | |
18 | + name='slug', | |
19 | + field=autoslug.fields.AutoSlugField(editable=False, populate_from='name', unique=True, verbose_name='Slug'), | |
20 | + ), | |
21 | + ] | ... | ... |
... | ... | @@ -0,0 +1,56 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | +# Generated by Django 1.10 on 2016-09-30 04:24 | |
3 | +from __future__ import unicode_literals | |
4 | + | |
5 | +from django.conf import settings | |
6 | +from django.db import migrations, models | |
7 | +import django.db.models.deletion | |
8 | + | |
9 | + | |
10 | +class Migration(migrations.Migration): | |
11 | + | |
12 | + dependencies = [ | |
13 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), | |
14 | + ('courses', '0001_initial'), | |
15 | + ] | |
16 | + | |
17 | + operations = [ | |
18 | + migrations.RemoveField( | |
19 | + model_name='activity', | |
20 | + name='student', | |
21 | + ), | |
22 | + migrations.RemoveField( | |
23 | + model_name='material', | |
24 | + name='student', | |
25 | + ), | |
26 | + migrations.AddField( | |
27 | + model_name='activity', | |
28 | + name='all_students', | |
29 | + field=models.BooleanField(default=False, verbose_name='All Students'), | |
30 | + ), | |
31 | + migrations.AddField( | |
32 | + model_name='activity', | |
33 | + name='students', | |
34 | + field=models.ManyToManyField(related_name='activities', to=settings.AUTH_USER_MODEL, verbose_name='Students'), | |
35 | + ), | |
36 | + migrations.AddField( | |
37 | + model_name='material', | |
38 | + name='all_students', | |
39 | + field=models.BooleanField(default=False, verbose_name='All Students'), | |
40 | + ), | |
41 | + migrations.AddField( | |
42 | + model_name='material', | |
43 | + name='students', | |
44 | + field=models.ManyToManyField(related_name='materials', to=settings.AUTH_USER_MODEL, verbose_name='Students'), | |
45 | + ), | |
46 | + migrations.AlterField( | |
47 | + model_name='activity', | |
48 | + name='topic', | |
49 | + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='activities', to='courses.Topic', verbose_name='Topic'), | |
50 | + ), | |
51 | + migrations.AlterField( | |
52 | + model_name='material', | |
53 | + name='topic', | |
54 | + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='materials', to='courses.Topic', verbose_name='Topic'), | |
55 | + ), | |
56 | + ] | ... | ... |
courses/models.py
... | ... | @@ -88,17 +88,19 @@ It is one kind of possible resources available inside a Topic. |
88 | 88 | Activity is something that has a deadline and has to be delivered by the student |
89 | 89 | """ |
90 | 90 | class Activity(Resource): |
91 | - topic = models.ForeignKey(Topic, verbose_name = _('Topic')) | |
91 | + topic = models.ForeignKey(Topic, verbose_name = _('Topic'), related_name='activities') | |
92 | 92 | limit_date = models.DateTimeField(_('Deliver Date')) |
93 | - student = models.ForeignKey(User, verbose_name = _('student')) | |
93 | + students = models.ManyToManyField(User, verbose_name = _('Students'), related_name='activities') | |
94 | + all_students = models.BooleanField(_('All Students'), default=False) | |
94 | 95 | |
95 | 96 | |
96 | 97 | """ |
97 | 98 | It represents any Material inside a topic, be it a file, a link, etc. |
98 | 99 | """ |
99 | 100 | class Material(Resource): |
100 | - topic = models.ForeignKey(Topic, verbose_name = _('Topic')) | |
101 | - student = models.ForeignKey(User, verbose_name = _('student')) | |
101 | + topic = models.ForeignKey(Topic, verbose_name = _('Topic'), related_name='materials') | |
102 | + students = models.ManyToManyField(User, verbose_name = _('Students'), related_name='materials') | |
103 | + all_students = models.BooleanField(_('All Students'), default=False) | |
102 | 104 | |
103 | 105 | """ |
104 | 106 | It is a category for each subject. |
... | ... | @@ -112,4 +114,3 @@ class SubjectCategory(models.Model): |
112 | 114 | class Meta: |
113 | 115 | verbose_name = _('subject category') |
114 | 116 | verbose_name_plural = _('subject categories') |
115 | - | ... | ... |
poll/forms.py
... | ... | @@ -5,18 +5,31 @@ from .models import Poll |
5 | 5 | |
6 | 6 | class PollForm(forms.ModelForm): |
7 | 7 | |
8 | - # password = forms.CharField(label=_('Password'), widget=forms.PasswordInput) | |
9 | - # password2 = forms.CharField(label = _('Password confirmation'), widget = forms.PasswordInput) | |
10 | - # birth_date = forms.DateField(widget=forms.SelectDateWidget()) | |
11 | - # MIN_LENGTH = 8 | |
8 | + def __init__(self, *args, **kwargs): | |
9 | + super(PollForm, self).__init__(*args, **kwargs) | |
10 | + self.fields["all_students"].required = False | |
11 | + self.fields["all_students"].initial = False | |
12 | + self.fields["students"].required = False | |
13 | + | |
14 | + def clean_all_students(self): | |
15 | + if('all_students' not in self.data): | |
16 | + if('students' in self.data): | |
17 | + return False | |
18 | + raise forms.ValidationError(_('It is required one these fields.')) | |
19 | + else: | |
20 | + all_students = self.data['all_students'] | |
21 | + if(not all_students): | |
22 | + raise forms.ValidationError(_('It is required one these fields.')) | |
23 | + return True | |
24 | + | |
12 | 25 | |
13 | 26 | class Meta: |
14 | 27 | model = Poll |
15 | - # exclude = ['is_staff', 'is_active'] | |
16 | - fields = ['name','limit_date'] | |
28 | + fields = ['name','limit_date','students','all_students'] | |
17 | 29 | |
18 | 30 | widgets = { |
19 | 31 | 'name': forms.TextInput(attrs={'placeholder': 'Question?'}), |
20 | - 'description': forms.DateTimeInput( | |
32 | + 'limit_date': forms.DateTimeInput( | |
21 | 33 | attrs={'placeholder': 'Maximum date permited to resolve the poll'}), |
34 | + 'student': forms.Select(), | |
22 | 35 | } | ... | ... |
... | ... | @@ -0,0 +1,19 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | +# Generated by Django 1.10 on 2016-09-30 04:24 | |
3 | +from __future__ import unicode_literals | |
4 | + | |
5 | +from django.db import migrations | |
6 | + | |
7 | + | |
8 | +class Migration(migrations.Migration): | |
9 | + | |
10 | + dependencies = [ | |
11 | + ('poll', '0001_initial'), | |
12 | + ] | |
13 | + | |
14 | + operations = [ | |
15 | + migrations.RemoveField( | |
16 | + model_name='poll', | |
17 | + name='question', | |
18 | + ), | |
19 | + ] | ... | ... |
poll/models.py
poll/templates/poll/create_update.html
... | ... | @@ -93,7 +93,9 @@ |
93 | 93 | </div> |
94 | 94 | <button type="button" id="add" class="btn btn-primary btn-block btn-sm">add</button> |
95 | 95 | <div class="row form-group"> |
96 | - <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 %}> | |
96 | + <label for="{{ form.limit_date.auto_id }}">{{ form.limit_date.label }}</label> | |
97 | + {% render_field form.limit_date class="form-control" form="form"%} | |
98 | + {# <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 %}>#} | |
97 | 99 | {% if form.limit_date.errors %} |
98 | 100 | <div class="not_submited"> |
99 | 101 | </br> |
... | ... | @@ -111,6 +113,37 @@ |
111 | 113 | {% endif %} |
112 | 114 | </div> |
113 | 115 | |
116 | + <div class="row form-group"> | |
117 | + <label for="{{ form.students.auto_id }}">{{ form.students.label }}</label> | |
118 | + {% render_field form.students class="form-control" form="form"%} | |
119 | + </div> | |
120 | + <div class="row form-group"> | |
121 | + <div class="checkbox"> | |
122 | + <label> | |
123 | + {% render_field form.all_students class="form-control" form="form" %}<span class="checkbox-material"><span class="check"></span></span> {{form.all_students.label }} | |
124 | + {# <input form="form" type="checkbox" name="{{form.all_students.name}}"><span class="checkbox-material"><span class="check"></span></span> {{ form.all_students.label }}#} | |
125 | + </label> | |
126 | + </div> | |
127 | + {% if form.all_students.errors %} | |
128 | + <div class="not_submited"> | |
129 | + </br> | |
130 | + <div class="alert alert-danger alert-dismissible" role="alert"> | |
131 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
132 | + <span aria-hidden="true">×</span> | |
133 | + </button> | |
134 | + <ul> | |
135 | + {% for error in form.all_students.errors %} | |
136 | + <li>{{ error }}</li> | |
137 | + {% endfor %} | |
138 | + </ul> | |
139 | + </div> | |
140 | + </div> | |
141 | + {% endif %} | |
142 | + | |
143 | + {# <label for="{{ form.all_students.auto_id }}">{{ form.all_students.label }}</label>#} | |
144 | + {# {% render_field form.all_students class="form-control" form="form"%}#} | |
145 | + </div> | |
146 | + | |
114 | 147 | </div> |
115 | 148 | |
116 | 149 | <!-- Modal Footer --> | ... | ... |
... | ... | @@ -0,0 +1,99 @@ |
1 | +{% extends "topic/index.html" %} | |
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"> | |
9 | + | |
10 | + <!-- Modal Header --> | |
11 | + <div class="modal-header"> | |
12 | + | |
13 | + <!-- Put your title here!!! --> | |
14 | + <h4 class="modal-title" id="myModalLabel">{{form.question}}</h4> | |
15 | + | |
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> | |
96 | + </div> | |
97 | +</div> | |
98 | +<a href="" data-toggle="modal" data-target="#poll">modal</a> | |
99 | +{% endblock content %} | ... | ... |
poll/urls.py
... | ... | @@ -3,7 +3,7 @@ from django.conf.urls import url |
3 | 3 | from . import views |
4 | 4 | |
5 | 5 | urlpatterns = [ |
6 | - url(r'^create/$', views.CreatePoll.as_view(), name='create_poll'), | |
7 | - url(r'^update/(?P<slug>[\w\-_]+)/$', views.UpdatePoll.as_view(), name='update_poll'), | |
6 | + 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 | 8 | |
9 | 9 | ] | ... | ... |
poll/views.py
... | ... | @@ -18,22 +18,19 @@ from courses.models import Course, Topic |
18 | 18 | |
19 | 19 | class CreatePoll(LoginRequiredMixin,generic.CreateView): |
20 | 20 | |
21 | - # login_url = reverse_lazy("core:home") | |
22 | - # redirect_field_name = 'next' | |
21 | + login_url = reverse_lazy("core:home") | |
22 | + redirect_field_name = 'next' | |
23 | 23 | model = Poll |
24 | 24 | form_class = PollForm |
25 | 25 | context_object_name = 'poll' |
26 | 26 | template_name = 'poll/create_update.html' |
27 | - # queryset = Course.objects.all() | |
28 | 27 | success_url = reverse_lazy('core:home') |
29 | - # def get_queryset(self): | |
30 | - # return Course.objects.all()[0] | |
31 | 28 | |
32 | 29 | def form_invalid(self, form,**kwargs): |
33 | 30 | context = super(CreatePoll, self).form_invalid(form) |
34 | 31 | answers = {} |
35 | 32 | for key in self.request.POST: |
36 | - if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date'): | |
33 | + if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | |
37 | 34 | answers[key] = self.request.POST[key] |
38 | 35 | |
39 | 36 | keys = sorted(answers) |
... | ... | @@ -43,14 +40,12 @@ class CreatePoll(LoginRequiredMixin,generic.CreateView): |
43 | 40 | |
44 | 41 | def form_valid(self, form): |
45 | 42 | self.object = form.save(commit = False) |
46 | - topic = Topic.objects.all()[0] | |
47 | - self.object.student = self.request.user | |
48 | - self.object.question = "question" | |
43 | + topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) | |
49 | 44 | self.object.topic = topic |
50 | 45 | self.object.save() |
51 | 46 | |
52 | 47 | for key in self.request.POST: |
53 | - if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date'): | |
48 | + if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | |
54 | 49 | answer = Answer(answer=self.request.POST[key],order=key,poll=self.object) |
55 | 50 | answer.save() |
56 | 51 | |
... | ... | @@ -58,11 +53,10 @@ class CreatePoll(LoginRequiredMixin,generic.CreateView): |
58 | 53 | |
59 | 54 | def get_context_data(self, **kwargs): |
60 | 55 | context = super(CreatePoll, self).get_context_data(**kwargs) |
61 | - course = Course.objects.all()[0] | |
62 | - # print (self.object) | |
63 | - context['course'] = course | |
64 | - context['subject'] = course.subjects.all()[0] | |
65 | - context['subjects'] = course.subjects.all() | |
56 | + topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) | |
57 | + context['course'] = topic.subject.course | |
58 | + context['subject'] = topic.subject | |
59 | + context['subjects'] = topic.subject.course.subjects.all() | |
66 | 60 | return context |
67 | 61 | |
68 | 62 | class UpdatePoll(LoginRequiredMixin,generic.UpdateView): |
... | ... | @@ -88,7 +82,7 @@ class UpdatePoll(LoginRequiredMixin,generic.UpdateView): |
88 | 82 | context = super(UpdatePoll, self).form_invalid(form) |
89 | 83 | answers = {} |
90 | 84 | for key in self.request.POST: |
91 | - if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date'): | |
85 | + if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | |
92 | 86 | answers[key] = self.request.POST[key] |
93 | 87 | |
94 | 88 | keys = sorted(answers) |
... | ... | @@ -104,7 +98,7 @@ class UpdatePoll(LoginRequiredMixin,generic.UpdateView): |
104 | 98 | |
105 | 99 | |
106 | 100 | for key in self.request.POST: |
107 | - if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date'): | |
101 | + if(key != 'csrfmiddlewaretoken' and key != 'name' and key != 'limit_date' and key != 'all_students' and key != 'students'): | |
108 | 102 | answer = Answer(answer=self.request.POST[key],order=key,poll=poll) |
109 | 103 | answer.save() |
110 | 104 | ... | ... |