Commit 2280108afce775e19ec37d91d2585628ec440338

Authored by fbormann
1 parent 94ff89ba

committing for saving important progress

reports/forms.py
... ... @@ -7,9 +7,16 @@ from django.forms.formsets import BaseFormSet
7 7 class ResourceAndTagForm(forms.Form):
8 8  
9 9 resource = forms.ChoiceField(label=_("Resources"), required=True)
10   - tag = forms.ChoiceField(label=_('Tag'))
11   -
  10 + tag = forms.ChoiceField(label=_('Tag'), required=True)
12 11  
  12 + def __init__(self, *args, **kwargs):
  13 + super(ResourceAndTagForm, self).__init__(*args, **kwargs)
  14 + if kwargs.get('initial'):
  15 + initial = kwargs['initial']
  16 + print(initial)
  17 + self.fields['resource'].choices = [(resource.id, resource.name) for resource in initial['resource']]
  18 + self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']]
  19 +
13 20  
14 21 class CreateInteractionReportForm(forms.Form):
15 22 topic = forms.ChoiceField( label= _("Topics to select data from"))
... ...
reports/templates/reports/_form.html
... ... @@ -51,12 +51,29 @@
51 51  
52 52 <div id="resources" class="panel-collapse collapse">
53 53 {{ resource_tag_formset.management_form }}
  54 + {{ resource_tag_formset.non_form_errors }}
54 55  
55 56 {% for resource_tag_form in resource_tag_formset %}
56 57 <div class="resource-tag-formset">
57 58 {% for field in resource_tag_form %}
58 59 <label>{{field.label}}</label>
59 60 {% render_field field class="form-control" %}
  61 +
  62 + {% if field.errors %}
  63 + <div class="row">
  64 + </br>
  65 + <div class="alert alert-danger alert-dismissible" role="alert">
  66 + <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  67 + <span aria-hidden="true">&times;</span>
  68 + </button>
  69 + <ul>
  70 + {% for error in field.errors %}
  71 + <li>{{ error }}</li>
  72 + {% endfor %}
  73 + </ul>
  74 + </div>
  75 + </div>
  76 + {% endif %}
60 77 {% endfor %}
61 78 </div>
62 79 {% endfor %}
... ...
reports/templates/reports/create.html
... ... @@ -49,9 +49,31 @@
49 49  
50 50 <script type="text/javascript" src="{% static "reports/js/report.js" %}"></script>
51 51 <script>
  52 +
  53 + var subject_id = "{{subject.id}}";
52 54 $('.resource-tag-formset').formset({
53 55 addText: 'add data source',
54   - deleteText: 'remove data source'
  56 + deleteText: 'remove data source',
  57 + added: function(object){
  58 + $.get("{% url 'subjects:reports:get_resource_and_tags' %}?subject_id={{subject.id}}", function(data){
  59 + fields = object.children("select");
  60 +
  61 + for(var j = 0; j < data.resources.length; j++){
  62 + fields[0].options[fields[0].options.length] = new Option(data.resources[j].name,data.resources[j].id);
  63 +
  64 + }
  65 + //Modify tags fields
  66 + fields[0].onchange = function(item){
  67 + //it request all tags associated with that resource
  68 + $.get("{% url 'subjects:reports:get_tags' %}?resource_id="+item.target.value, function(data){
  69 + fields[1].options.length = 0;
  70 + for(var j = 0; j < data.tags.length; j++){
  71 + fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id);
  72 + }
  73 + });
  74 + };
  75 + });
  76 + },
55 77 });
56 78 </script>
57 79 {% endblock content %}
58 80 \ No newline at end of file
... ...
reports/urls.py
... ... @@ -5,4 +5,6 @@ from . import views
5 5 urlpatterns = [
6 6 url(r'^create/interactions/$', views.ReportView.as_view(), name='create_interaction'),
7 7 url(r'^view/interactions/$', views.ViewReportView.as_view(), name='view_report'),
  8 + url(r'^get/resources/$', views.get_resources, name='get_resource_and_tags'),
  9 + url(r'^get/tags/$', views.get_tags, name='get_tags'),
8 10 ]
9 11 \ No newline at end of file
... ...
reports/views.py
... ... @@ -15,7 +15,7 @@ from datetime import datetime, date
15 15 from subjects.models import Subject
16 16 from .forms import CreateInteractionReportForm, ResourceAndTagForm
17 17 from log.models import Log
18   -from topics.models import Resource
  18 +from topics.models import Resource, Topic
19 19  
20 20 from django.forms import formset_factory
21 21  
... ... @@ -55,10 +55,11 @@ class ReportView(LoginRequiredMixin, generic.FormView):
55 55 resources.append(resource)
56 56 context['resources'] = resources
57 57 context['tags'] = tags
58   -
  58 +
59 59  
60 60 #set formset
61   - resourceTagFormSet = formset_factory(ResourceAndTagForm, extra= 1)
  61 + resourceTagFormSet = formset_factory(ResourceAndTagForm, extra = 1)
  62 + resourceTagFormSet = resourceTagFormSet(initial=[{'resource':resources, 'tag':tags}])
62 63 context['resource_tag_formset'] = resourceTagFormSet
63 64 return context
64 65  
... ... @@ -72,6 +73,10 @@ class ReportView(LoginRequiredMixin, generic.FormView):
72 73 get_params += key + "=" + str(value) + "&"
73 74  
74 75  
  76 + for form_data in self.formset_data:
  77 + for key, value in form_data.items():
  78 + get_params += key + "=" + str(value) + "&"
  79 +
75 80 #retrieving subject id for data purposes
76 81 for key, value in self.request.GET.items():
77 82 get_params += key + "=" + str(value)
... ... @@ -84,10 +89,24 @@ class ReportView(LoginRequiredMixin, generic.FormView):
84 89 POST variables and then checked for validity.
85 90 """
86 91 form = self.get_form()
87   - print(form)
88   - if form.is_valid():
89   - print(form)
  92 +
  93 + subject = Subject.objects.get(id=self.request.GET['subject_id'])
  94 +
  95 + topics = subject.topic_subject.all()
  96 + #get all resources associated with topics
  97 + resources = []
  98 + tags = []
  99 + for topic in topics:
  100 + resources_set = topic.resource_topic.all()
  101 + for resource in resources_set:
  102 + for tag in resource.tags.all():
  103 + tags.append(tag)
  104 + resources.append(resource)
  105 + resourceTagFormSet = formset_factory(ResourceAndTagForm, extra= 1)
  106 + resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}])
  107 + if form.is_valid() and resources_formset.is_valid():
90 108 self.form_data = form.cleaned_data
  109 + self.formset_data = resources_formset.cleaned_data
91 110 return self.form_valid(form)
92 111 else:
93 112 return self.form_invalid(form)
... ... @@ -208,3 +227,33 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
208 227 return data, header
209 228  
210 229  
  230 +
  231 +def get_resources(request):
  232 + subject = Subject.objects.get(id=request.GET['subject_id'])
  233 +
  234 + topics = subject.topic_subject.all()
  235 + #get all resources associated with topics
  236 + resources = []
  237 + tags = []
  238 + for topic in topics:
  239 + resources_set = topic.resource_topic.all()
  240 + for resource in resources_set:
  241 + for tag in resource.tags.all():
  242 + tags.append(tag)
  243 + resources.append(resource)
  244 +
  245 + data = {}
  246 +
  247 + data['resources']= [ {'id':resource.id, 'name':resource.name} for resource in resources]
  248 + return JsonResponse(data)
  249 +
  250 +
  251 +def get_tags(request):
  252 + resource = Resource.objects.get(id=request.GET['resource_id'])
  253 + data = {}
  254 + tags = []
  255 +
  256 + for tag in resource.tags.all():
  257 + tags.append(tag)
  258 + data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags]
  259 + return JsonResponse(data)
... ...
subjects/forms.py
... ... @@ -175,7 +175,6 @@ class UpdateSubjectForm(forms.ModelForm):
175 175 def clean_subscribe_begin(self):
176 176 subscribe_begin = self.cleaned_data['subscribe_begin']
177 177  
178   - print (self.instance.subscribe_begin, "################################")
179 178 if subscribe_begin < datetime.datetime.today().date() and subscribe_begin < self.instance.subscribe_begin:
180 179 self._errors['subscribe_begin'] = [_('This date must be today or after')]
181 180 return ValueError
... ...
topics/views.py
... ... @@ -57,7 +57,6 @@ class CreateView(LoginRequiredMixin, LogMixin, generic.edit.CreateView):
57 57 subject = get_object_or_404(Subject, slug = slug)
58 58  
59 59 self.object.subject = subject
60   - print (subject.topic_subject.count())
61 60 self.object.order = subject.topic_subject.count() + 1
62 61  
63 62 self.object.save()
... ...