Commit 8227d916f5d85f2a83c031e4e71be5c62d4f801a

Authored by fbormann
1 parent 2846f023

posted data from more than one dynamic form

reports/forms.py
@@ -4,6 +4,21 @@ import datetime @@ -4,6 +4,21 @@ import datetime
4 4
5 from django.forms.formsets import BaseFormSet 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 class ResourceAndTagForm(forms.Form): 22 class ResourceAndTagForm(forms.Form):
8 23
9 resource = forms.ChoiceField(label=_("Kind Of Resource"), required=True) 24 resource = forms.ChoiceField(label=_("Kind Of Resource"), required=True)
@@ -13,7 +28,7 @@ class ResourceAndTagForm(forms.Form): @@ -13,7 +28,7 @@ class ResourceAndTagForm(forms.Form):
13 super(ResourceAndTagForm, self).__init__(*args, **kwargs) 28 super(ResourceAndTagForm, self).__init__(*args, **kwargs)
14 if kwargs.get('initial'): 29 if kwargs.get('initial'):
15 initial = kwargs['initial'] 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 self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']] 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,29 +65,21 @@
65 65
66 <div id="resources" class="panel-collapse collapse"> 66 <div id="resources" class="panel-collapse collapse">
67 {{ resource_tag_formset.management_form }} 67 {{ resource_tag_formset.management_form }}
68 - {{ resource_tag_formset.non_form_errors }} 68 +
69 69
70 {% for resource_tag_form in resource_tag_formset %} 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 <div class="resource-tag-formset"> 77 <div class="resource-tag-formset">
72 {% for field in resource_tag_form %} 78 {% for field in resource_tag_form %}
73 <label>{{field.label}}</label> 79 <label>{{field.label}}</label>
74 {% render_field field class="form-control" %} 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">&times;</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 {% endfor %} 83 {% endfor %}
92 </div> 84 </div>
93 {% endfor %} 85 {% endfor %}
reports/views.py
@@ -13,7 +13,7 @@ from django.db.models import Q @@ -13,7 +13,7 @@ from django.db.models import Q
13 from django.contrib.auth.mixins import LoginRequiredMixin 13 from django.contrib.auth.mixins import LoginRequiredMixin
14 from datetime import datetime, date 14 from datetime import datetime, date
15 from subjects.models import Subject 15 from subjects.models import Subject
16 -from .forms import CreateInteractionReportForm, ResourceAndTagForm 16 +from .forms import CreateInteractionReportForm, ResourceAndTagForm, BaseResourceAndTagFormset
17 from log.models import Log 17 from log.models import Log
18 from topics.models import Resource, Topic 18 from topics.models import Resource, Topic
19 19
@@ -45,21 +45,20 @@ class ReportView(LoginRequiredMixin, generic.FormView): @@ -45,21 +45,20 @@ class ReportView(LoginRequiredMixin, generic.FormView):
45 45
46 topics = subject.topic_subject.all() 46 topics = subject.topic_subject.all()
47 #get all resources associated with topics 47 #get all resources associated with topics
48 - resources = []  
49 tags = [] 48 tags = []
50 for topic in topics: 49 for topic in topics:
51 resources_set = topic.resource_topic.all() 50 resources_set = topic.resource_topic.all()
52 for resource in resources_set: 51 for resource in resources_set:
53 for tag in resource.tags.all(): 52 for tag in resource.tags.all():
54 tags.append(tag) 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 #set formset 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 context['resource_tag_formset'] = resourceTagFormSet 62 context['resource_tag_formset'] = resourceTagFormSet
64 return context 63 return context
65 64
@@ -94,16 +93,22 @@ class ReportView(LoginRequiredMixin, generic.FormView): @@ -94,16 +93,22 @@ class ReportView(LoginRequiredMixin, generic.FormView):
94 93
95 topics = subject.topic_subject.all() 94 topics = subject.topic_subject.all()
96 #get all resources associated with topics 95 #get all resources associated with topics
97 - resources = []  
98 tags = [] 96 tags = []
99 for topic in topics: 97 for topic in topics:
100 resources_set = topic.resource_topic.all() 98 resources_set = topic.resource_topic.all()
101 for resource in resources_set: 99 for resource in resources_set:
102 for tag in resource.tags.all(): 100 for tag in resource.tags.all():
103 tags.append(tag) 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 if form.is_valid() and resources_formset.is_valid(): 112 if form.is_valid() and resources_formset.is_valid():
108 self.form_data = form.cleaned_data 113 self.form_data = form.cleaned_data
109 self.formset_data = resources_formset.cleaned_data 114 self.formset_data = resources_formset.cleaned_data
@@ -125,7 +130,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -125,7 +130,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
125 context['init_date'] = params_data['init_date'] 130 context['init_date'] = params_data['init_date']
126 context['end_date'] = params_data['end_date'] 131 context['end_date'] = params_data['end_date']
127 context['subject'] = subject 132 context['subject'] = subject
128 - 133 + print(params_data)
129 134
130 if params_data['from_mural']: 135 if params_data['from_mural']:
131 context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date']) 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,6 +202,8 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
197 user = student).count() 202 user = student).count()
198 203
199 204
  205 + #VAR08 -
  206 +
200 #VAR20 - number of access to mural between 6 a.m to 12a.m. 207 #VAR20 - number of access to mural between 6 a.m to 12a.m.
201 interactions[' number of access to mural between 6 a.m to 12a.m.'] = Log.objects.filter(action="access", resource="subject", 208 interactions[' number of access to mural between 6 a.m to 12a.m.'] = Log.objects.filter(action="access", resource="subject",
202 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count() 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,11 +212,11 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
205 interactions['number of access to mural between 0 p.m to 6p.m.'] = Log.objects.filter(action="access", resource="subject", 212 interactions['number of access to mural between 0 p.m to 6p.m.'] = Log.objects.filter(action="access", resource="subject",
206 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count() 213 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count()
207 #VAR22 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 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count() 216 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count()
210 217
211 #VAR23 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 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count() 220 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count()
214 221
215 #VAR24 through 30 222 #VAR24 through 30