Commit 8227d916f5d85f2a83c031e4e71be5c62d4f801a
1 parent
2846f023
Exists in
master
and in
3 other branches
posted data from more than one dynamic form
Showing
3 changed files
with
45 additions
and
31 deletions
Show diff stats
reports/forms.py
... | ... | @@ -4,6 +4,21 @@ import datetime |
4 | 4 | |
5 | 5 | from django.forms.formsets import BaseFormSet |
6 | 6 | |
7 | + | |
8 | +class BaseResourceAndTagFormset(BaseFormSet): | |
9 | + def clean(self): | |
10 | + """ | |
11 | + Adds validation to check that no two links have the same anchor or URL | |
12 | + and that all links have both an anchor and URL. | |
13 | + """ | |
14 | + print("here 2") | |
15 | + print(self.errors) | |
16 | + if any(self.errors): | |
17 | + return | |
18 | + | |
19 | + for form in self.forms: | |
20 | + print(form) | |
21 | + | |
7 | 22 | class ResourceAndTagForm(forms.Form): |
8 | 23 | |
9 | 24 | resource = forms.ChoiceField(label=_("Kind Of Resource"), required=True) |
... | ... | @@ -13,7 +28,7 @@ class ResourceAndTagForm(forms.Form): |
13 | 28 | super(ResourceAndTagForm, self).__init__(*args, **kwargs) |
14 | 29 | if kwargs.get('initial'): |
15 | 30 | initial = kwargs['initial'] |
16 | - self.fields['resource'].choices = [(resource.id, resource.name) for resource in initial['resource']] | |
31 | + self.fields['resource'].choices = [(classes.__name__, classes.__name__) for classes in initial['class_name']] | |
17 | 32 | self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']] |
18 | 33 | |
19 | 34 | ... | ... |
reports/templates/reports/_form.html
... | ... | @@ -65,29 +65,21 @@ |
65 | 65 | |
66 | 66 | <div id="resources" class="panel-collapse collapse"> |
67 | 67 | {{ resource_tag_formset.management_form }} |
68 | - {{ resource_tag_formset.non_form_errors }} | |
68 | + | |
69 | 69 | |
70 | 70 | {% for resource_tag_form in resource_tag_formset %} |
71 | + | |
72 | + {% if resource_tag_form.anchor.errors %} | |
73 | + {% for error in resource_tag_form.anchor.errors %} | |
74 | + {{ error|escape }} | |
75 | + {% endfor %} | |
76 | + {% endif %} | |
71 | 77 | <div class="resource-tag-formset"> |
72 | 78 | {% for field in resource_tag_form %} |
73 | 79 | <label>{{field.label}}</label> |
74 | 80 | {% render_field field class="form-control" %} |
75 | 81 | |
76 | - {% if field.errors %} | |
77 | - <div class="row"> | |
78 | - </br> | |
79 | - <div class="alert alert-danger alert-dismissible" role="alert"> | |
80 | - <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
81 | - <span aria-hidden="true">×</span> | |
82 | - </button> | |
83 | - <ul> | |
84 | - {% for error in field.errors %} | |
85 | - <li>{{ error }}</li> | |
86 | - {% endfor %} | |
87 | - </ul> | |
88 | - </div> | |
89 | - </div> | |
90 | - {% endif %} | |
82 | + | |
91 | 83 | {% endfor %} |
92 | 84 | </div> |
93 | 85 | {% endfor %} | ... | ... |
reports/views.py
... | ... | @@ -13,7 +13,7 @@ from django.db.models import Q |
13 | 13 | from django.contrib.auth.mixins import LoginRequiredMixin |
14 | 14 | from datetime import datetime, date |
15 | 15 | from subjects.models import Subject |
16 | -from .forms import CreateInteractionReportForm, ResourceAndTagForm | |
16 | +from .forms import CreateInteractionReportForm, ResourceAndTagForm, BaseResourceAndTagFormset | |
17 | 17 | from log.models import Log |
18 | 18 | from topics.models import Resource, Topic |
19 | 19 | |
... | ... | @@ -45,21 +45,20 @@ class ReportView(LoginRequiredMixin, generic.FormView): |
45 | 45 | |
46 | 46 | topics = subject.topic_subject.all() |
47 | 47 | #get all resources associated with topics |
48 | - resources = [] | |
49 | 48 | tags = [] |
50 | 49 | for topic in topics: |
51 | 50 | resources_set = topic.resource_topic.all() |
52 | 51 | for resource in resources_set: |
53 | 52 | for tag in resource.tags.all(): |
54 | 53 | tags.append(tag) |
55 | - resources.append(resource) | |
56 | - context['resources'] = resources | |
57 | - context['tags'] = tags | |
58 | 54 | |
59 | 55 | |
56 | + classes = Resource.__subclasses__() | |
57 | + | |
58 | + | |
60 | 59 | #set formset |
61 | - resourceTagFormSet = formset_factory(ResourceAndTagForm) | |
62 | - resourceTagFormSet = resourceTagFormSet() | |
60 | + resourceTagFormSet = formset_factory(ResourceAndTagForm, formset=BaseResourceAndTagFormset) | |
61 | + resourceTagFormSet = resourceTagFormSet(initial=[{'class_name': classes, 'tag':tags}]) | |
63 | 62 | context['resource_tag_formset'] = resourceTagFormSet |
64 | 63 | return context |
65 | 64 | |
... | ... | @@ -94,16 +93,22 @@ class ReportView(LoginRequiredMixin, generic.FormView): |
94 | 93 | |
95 | 94 | topics = subject.topic_subject.all() |
96 | 95 | #get all resources associated with topics |
97 | - resources = [] | |
98 | 96 | tags = [] |
99 | 97 | for topic in topics: |
100 | 98 | resources_set = topic.resource_topic.all() |
101 | 99 | for resource in resources_set: |
102 | 100 | for tag in resource.tags.all(): |
103 | 101 | tags.append(tag) |
104 | - resources.append(resource) | |
105 | - resourceTagFormSet = formset_factory(ResourceAndTagForm) | |
106 | - resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}]) | |
102 | + | |
103 | + classes = Resource.__subclasses__() | |
104 | + amount_of_forms = self.request.POST['form-TOTAL_FORMS'] | |
105 | + initial_datum = {'class_name': classes , 'tag': tags} | |
106 | + initial_data = [] | |
107 | + for i in range(int(amount_of_forms)): | |
108 | + initial_data.append(initial_datum) | |
109 | + | |
110 | + resourceTagFormSet = formset_factory(ResourceAndTagForm, formset=BaseResourceAndTagFormset) | |
111 | + resources_formset = resourceTagFormSet(self.request.POST, initial = initial_data) | |
107 | 112 | if form.is_valid() and resources_formset.is_valid(): |
108 | 113 | self.form_data = form.cleaned_data |
109 | 114 | self.formset_data = resources_formset.cleaned_data |
... | ... | @@ -125,7 +130,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): |
125 | 130 | context['init_date'] = params_data['init_date'] |
126 | 131 | context['end_date'] = params_data['end_date'] |
127 | 132 | context['subject'] = subject |
128 | - | |
133 | + print(params_data) | |
129 | 134 | |
130 | 135 | if params_data['from_mural']: |
131 | 136 | context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date']) |
... | ... | @@ -197,6 +202,8 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): |
197 | 202 | user = student).count() |
198 | 203 | |
199 | 204 | |
205 | + #VAR08 - | |
206 | + | |
200 | 207 | #VAR20 - number of access to mural between 6 a.m to 12a.m. |
201 | 208 | interactions[' number of access to mural between 6 a.m to 12a.m.'] = Log.objects.filter(action="access", resource="subject", |
202 | 209 | user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count() |
... | ... | @@ -205,11 +212,11 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): |
205 | 212 | interactions['number of access to mural between 0 p.m to 6p.m.'] = Log.objects.filter(action="access", resource="subject", |
206 | 213 | user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count() |
207 | 214 | #VAR22 |
208 | - interactions[' number of access to mural between 6 p.m to 12p.m.'] = Log.objects.filter(action="access", resource="subject", | |
215 | + interactions['number of access to mural between 6 p.m to 12p.m.'] = Log.objects.filter(action="access", resource="subject", | |
209 | 216 | user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count() |
210 | 217 | |
211 | 218 | #VAR23 |
212 | - interactions[' number of access to mural between 0 a.m to 6a.m.'] = Log.objects.filter(action="access", resource="subject", | |
219 | + interactions['number of access to mural between 0 a.m to 6a.m.'] = Log.objects.filter(action="access", resource="subject", | |
213 | 220 | user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count() |
214 | 221 | |
215 | 222 | #VAR24 through 30 | ... | ... |