Switch to side-by-side view

--- a/OSSEval/analysis/views.py
+++ b/OSSEval/analysis/views.py
@@ -1,20 +1,32 @@
 from django.conf import settings
 from django.core.urlresolvers import reverse
-from django.http import HttpResponseRedirect
-from django.shortcuts import render, get_object_or_404, render_to_response
+from django.http import HttpResponseRedirect, HttpResponse
+from django.shortcuts import render, get_object_or_404, render_to_response, redirect
 from django.template import RequestContext
+from django.utils import simplejson, timezone
 from django.views.generic import ListView
-
 from xml.dom import minidom
 
 from forms import AnalysisForm, UploadFileForm, ImportChoice
-from analysis.models import Analysis, UploadedFile
-from methodology.models import Methodology, MethodologyVersion
+from analysis.models import Analysis, Configuration, Instance, UploadedFile, Answer
+from methodology.models import Methodology, MethodologyVersion, Question
+from OSSEval.utils import xmlMinidom, TrivialJSONEncoder
 
 def analysis_new(request, analysis_id=0):
-    if analysis_id>0:
-        analysis = get_object_or_404(Analysis, pk=analysis_id)
-        form = AnalysisForm(instance = analysis)
+    if request.method == "POST":
+        if analysis_id>0:
+            analysis = get_object_or_404(Analysis, pk=analysis_id)
+            form = AnalysisForm(instance = analysis)
+        else:
+            form = AnalysisForm(request.POST)
+            if form.is_valid():
+                cfg = Configuration.objects.get(pk=1)
+                model_instance = form.save()
+                model_instance.created = timezone.now()
+#                 model_instance.methodology_version = cfg.default_methodology_version
+#                 print "model_instance.methodology_version:  " + str(model_instance.methodology_version.id)
+                model_instance.save()
+                return HttpResponseRedirect(reverse('analysis_list'))
     else:
         form = AnalysisForm()
     return render(request, 'analysis/analysis_new.html', {'form': form})
@@ -34,13 +46,44 @@
     search_html_ui = str(search_html_form(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
     analysis_detail = str(instance_list_html(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
     
-#     exec("from " + entity.actual_entity_app + ".models import " + entity.actual_entity_class)
-#     actual_entity_class = locals()[entity.actual_entity_class]
-#     search_html_ui = actual_entity_class.search_html_ui(request)
-#     
-#     analysis_detail = actual_entity_class.instance_list_html(request, analysis_id)
+    return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui})
+
+def analysis_questions(request, analysis_id):
+    analysis = get_object_or_404(Analysis, pk=analysis_id)
+
+    entity = analysis.methodology_version.methodology.entity
     
-    return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui})
+    exec("from " + entity.actual_entity_app + ".views import search_html_form, instance_list_html")
+    #"Content-Type: text/html; charset=utf-8" has to be removed as these methods return just a partial
+    analysis_detail = str(instance_list_html(request, analysis_id))[len("Content-Type: text/html; charset=utf-8"):]
+    
+    return render(request, 'analysis/analysis_questions.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'methodology_version': analysis.methodology_version})
+
+def save_answer(request):
+    try:
+        question_id = request.POST.get("question_id", "")
+        id_selected_instance = request.POST.get("id_selected_instance", "")
+        value = request.POST.get("value", "")
+        notes = request.POST.get("notes", "")
+        # I look for an answer for the same question and instance
+        try:
+            a = Answer.objects.get(instance_id=id_selected_instance, question_id=question_id)
+        except:
+            # I didn't find one; let's create it 
+            a = Answer()
+            a.instance = Instance.objects.get(pk=id_selected_instance)
+            a.question = Question.objects.get(pk=question_id)
+        a.value_integer = value
+        a.notes = notes
+        a.save()
+    except Exception as ex:
+        return HttpResponse(simplejson.dumps({'response': ex.message})) 
+    return HttpResponse(simplejson.dumps({'response': 'OK', 'question_id': question_id}))
+
+def get_answers(request):
+    id_instance = request.GET.get("id_instance", "")
+    answers = Answer.objects.filter(instance_id=id_instance)
+    return HttpResponse(TrivialJSONEncoder().encode(list(answers)))
 
 def export(request, analysis_id):
     a = get_object_or_404(Analysis, pk=analysis_id)
@@ -65,10 +108,10 @@
 
                 analysis_on_file = Analysis()
                 analysis_xml = xmldoc.getElementsByTagName('Analysis')
-                analysis_on_file.id = int(analysis_xml[0].getElementsByTagName('Id')[0].firstChild.data)
-                analysis_on_file.name = analysis_xml[0].getElementsByTagName('Name')[0].firstChild.data
-                analysis_on_file.created = analysis_xml[0].getElementsByTagName('Created')[0].firstChild.data
-                analysis_on_file.user_login = analysis_xml[0].getElementsByTagName('UserLogin')[0].firstChild.data
+                analysis_on_file.id = int(analysis_xml[0].attributes["Id"].firstChild.data)
+                analysis_on_file.name = analysis_xml[0].attributes["Name"].firstChild.data
+                analysis_on_file.created = analysis_xml[0].attributes["Created"].firstChild.data
+                analysis_on_file.user_login = analysis_xml[0].attributes["UserLogin"].firstChild.data
                 analysis_on_db = Analysis()
                 try: 
                     analysis_on_db = Analysis.objects.get(pk=analysis_on_file.id)
@@ -79,11 +122,11 @@
                 methodology_version_on_file = MethodologyVersion()
                 methodology_version_on_file.methodology = methodology_on_file
                 methodology_version_xml = xmldoc.getElementsByTagName('MethodologyVersion')
-                methodology_version_on_file.id = methodology_version_xml[0].getElementsByTagName('Id')[0].firstChild.data
-                methodology_version_on_file.number = methodology_version_xml[0].getElementsByTagName('Number')[0].firstChild.data
+                methodology_version_on_file.id = xmlMinidom.getNaturalAttribute(methodology_version_xml, 'Id')
+                methodology_version_on_file.number = methodology_version_xml[0].attributes["Number"].firstChild.data
                 methodology_xml = methodology_version_xml[0].getElementsByTagName('Methodology')
-                methodology_on_file.id = methodology_xml[0].getElementsByTagName('Id')[0].firstChild.data
-                methodology_on_file.name = methodology_xml[0].getElementsByTagName('Name')[0].firstChild.data
+                methodology_on_file.id = xmlMinidom.getNaturalAttribute(methodology_xml, 'Id')
+                methodology_on_file.name = methodology_xml[0].attributes["Name"].firstChild.data
 
                 analysis_on_file.methodology_version = methodology_version_on_file
                 import_choice_form = ImportChoice(initial={'uploaded_file_id': new_uploaded_file.id, 'new_uploaded_file_relpath': new_uploaded_file.docfile.url}) # An unbound form
@@ -110,33 +153,12 @@
         mv.from_xml(methodology_version_xml, always_insert)
     else:
         #If I am not importing the methodology I still need to associate it to the analysis
-        mv.id = methodology_version_xml.getElementsByTagName('Id')[0].firstChild.data
+        mv.id = methodology_version_xml.attributes["Id"].firstChild.data
     a = Analysis()
     a.methodology_version = mv
     analysis_xml = xmldoc.getElementsByTagName('Analysis')[0]
     if import_analysis:
         a.from_xml(analysis_xml, always_insert)
     else:
-        a.id = analysis_xml.getElementsByTagName('Id')[0].firstChild.data
+        a.id = analysis_xml.attributes["Id"].firstChild.data
     return HttpResponseRedirect(reverse('analysis_detail', args=(a.id,)))
-
-#     what_to_import = request.POST.get("what_to_import", "")
-#     
-#     uf = UploadedFile.objects.get(pk=uploaded_file_id)
-#     response = urllib2.urlopen(uf.docfile.url)
-#     xml = response.read()
-#     from forms.ImportChoice
-#     HOW_TO_IMPORT = [['0','Update if ID exists, create if ID is empty or non existent'],['1','Always create new records']]
-#     WHAT_TO_IMPORT = [['0','Import methodology'],['1','Import analysis']]
-#     if how_to_import == 0:
-#         
-#     else:
-    
-#     xml_uploaded = request.FILES['file'].read()
-#     uploaded_file_id = UploadedFile(docfile = request.FILES['file'])
-#     HOW_TO_IMPORT = [['0','Update if ID exists, create if ID is empty or non existent'],['1','Always create new records']]
-#     WHAT_TO_IMPORT = [['0','Import methodology'],['1','Import analysis']]
-#     how_to_import = forms.ChoiceField( widget=RadioSelect(), choices=HOW_TO_IMPORT)
-#     what_to_import = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=WHAT_TO_IMPORT, label='What to import (select at least one):')
-#     uploaded_file_id = forms.CharField(widget=forms.HiddenInput())
-