From 8227d916f5d85f2a83c031e4e71be5c62d4f801a Mon Sep 17 00:00:00 2001 From: fbormann Date: Mon, 13 Mar 2017 21:46:09 -0300 Subject: [PATCH] posted data from more than one dynamic form --- reports/forms.py | 17 ++++++++++++++++- reports/templates/reports/_form.html | 24 ++++++++---------------- reports/views.py | 35 +++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/reports/forms.py b/reports/forms.py index d243b70..758bfc8 100644 --- a/reports/forms.py +++ b/reports/forms.py @@ -4,6 +4,21 @@ import datetime from django.forms.formsets import BaseFormSet + +class BaseResourceAndTagFormset(BaseFormSet): + def clean(self): + """ + Adds validation to check that no two links have the same anchor or URL + and that all links have both an anchor and URL. + """ + print("here 2") + print(self.errors) + if any(self.errors): + return + + for form in self.forms: + print(form) + class ResourceAndTagForm(forms.Form): resource = forms.ChoiceField(label=_("Kind Of Resource"), required=True) @@ -13,7 +28,7 @@ class ResourceAndTagForm(forms.Form): super(ResourceAndTagForm, self).__init__(*args, **kwargs) if kwargs.get('initial'): initial = kwargs['initial'] - self.fields['resource'].choices = [(resource.id, resource.name) for resource in initial['resource']] + self.fields['resource'].choices = [(classes.__name__, classes.__name__) for classes in initial['class_name']] self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']] diff --git a/reports/templates/reports/_form.html b/reports/templates/reports/_form.html index 8dbb330..f56d603 100644 --- a/reports/templates/reports/_form.html +++ b/reports/templates/reports/_form.html @@ -65,29 +65,21 @@
{{ resource_tag_formset.management_form }} - {{ resource_tag_formset.non_form_errors }} + {% for resource_tag_form in resource_tag_formset %} + + {% if resource_tag_form.anchor.errors %} + {% for error in resource_tag_form.anchor.errors %} + {{ error|escape }} + {% endfor %} + {% endif %}
{% for field in resource_tag_form %} {% render_field field class="form-control" %} - {% if field.errors %} -
-
- -
- {% endif %} + {% endfor %}
{% endfor %} diff --git a/reports/views.py b/reports/views.py index f5b0afd..07a9ebf 100644 --- a/reports/views.py +++ b/reports/views.py @@ -13,7 +13,7 @@ from django.db.models import Q from django.contrib.auth.mixins import LoginRequiredMixin from datetime import datetime, date from subjects.models import Subject -from .forms import CreateInteractionReportForm, ResourceAndTagForm +from .forms import CreateInteractionReportForm, ResourceAndTagForm, BaseResourceAndTagFormset from log.models import Log from topics.models import Resource, Topic @@ -45,21 +45,20 @@ class ReportView(LoginRequiredMixin, generic.FormView): topics = subject.topic_subject.all() #get all resources associated with topics - resources = [] tags = [] for topic in topics: resources_set = topic.resource_topic.all() for resource in resources_set: for tag in resource.tags.all(): tags.append(tag) - resources.append(resource) - context['resources'] = resources - context['tags'] = tags + classes = Resource.__subclasses__() + + #set formset - resourceTagFormSet = formset_factory(ResourceAndTagForm) - resourceTagFormSet = resourceTagFormSet() + resourceTagFormSet = formset_factory(ResourceAndTagForm, formset=BaseResourceAndTagFormset) + resourceTagFormSet = resourceTagFormSet(initial=[{'class_name': classes, 'tag':tags}]) context['resource_tag_formset'] = resourceTagFormSet return context @@ -94,16 +93,22 @@ class ReportView(LoginRequiredMixin, generic.FormView): topics = subject.topic_subject.all() #get all resources associated with topics - resources = [] tags = [] for topic in topics: resources_set = topic.resource_topic.all() for resource in resources_set: for tag in resource.tags.all(): tags.append(tag) - resources.append(resource) - resourceTagFormSet = formset_factory(ResourceAndTagForm) - resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}]) + + classes = Resource.__subclasses__() + amount_of_forms = self.request.POST['form-TOTAL_FORMS'] + initial_datum = {'class_name': classes , 'tag': tags} + initial_data = [] + for i in range(int(amount_of_forms)): + initial_data.append(initial_datum) + + resourceTagFormSet = formset_factory(ResourceAndTagForm, formset=BaseResourceAndTagFormset) + resources_formset = resourceTagFormSet(self.request.POST, initial = initial_data) if form.is_valid() and resources_formset.is_valid(): self.form_data = form.cleaned_data self.formset_data = resources_formset.cleaned_data @@ -125,7 +130,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): context['init_date'] = params_data['init_date'] context['end_date'] = params_data['end_date'] context['subject'] = subject - + print(params_data) if params_data['from_mural']: 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): user = student).count() + #VAR08 - + #VAR20 - number of access to mural between 6 a.m to 12a.m. interactions[' number of access to mural between 6 a.m to 12a.m.'] = Log.objects.filter(action="access", resource="subject", 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): interactions['number of access to mural between 0 p.m to 6p.m.'] = Log.objects.filter(action="access", resource="subject", user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count() #VAR22 - interactions[' number of access to mural between 6 p.m to 12p.m.'] = Log.objects.filter(action="access", resource="subject", + interactions['number of access to mural between 6 p.m to 12p.m.'] = Log.objects.filter(action="access", resource="subject", user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count() #VAR23 - interactions[' number of access to mural between 0 a.m to 6a.m.'] = Log.objects.filter(action="access", resource="subject", + interactions['number of access to mural between 0 a.m to 6a.m.'] = Log.objects.filter(action="access", resource="subject", user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count() #VAR24 through 30 -- libgit2 0.21.2