Switch to side-by-side view

--- a/OSSEval/analysis/views.py
+++ b/OSSEval/analysis/views.py
@@ -7,7 +7,7 @@
 from django.views.generic import ListView
 from xml.dom import minidom
 
-from forms import AnalysisForm, UploadFileForm, ImportChoice
+from forms import AnalysisForm, UploadFileForm, ImportChoice, MyModelForm
 from analysis.models import Analysis, Configuration, Instance, UploadedFile, Answer, Methodology, MethodologyVersion, Question, Page
 #from methodology.models import Methodology, MethodologyVersion, Question, Page
 from OSSEval.utils import xmlMinidom, TrivialJSONEncoder, SearchEngine
@@ -17,6 +17,13 @@
         form = AnalysisForm(request.POST)
         if form.is_valid():
             model_instance = form.save()
+            model_instance.visible = True
+            weight_scenarios = model_instance.methodology_version.weight_scenario_set.filter(active=True) 
+            if len(weight_scenarios) == 1:
+                model_instance.weight_scenario = weight_scenarios[0]
+            else:
+                raise Exception("There should be exactly one active WeightScenario for MethodologyVersion " + str(model_instance.methodology_version.id))
+            model_instance.protected = False
             model_instance.created = timezone.now()
             model_instance.save()
             return HttpResponseRedirect(reverse('analysis_list'))
@@ -24,7 +31,7 @@
     else:
         if analysis_id>0:
             analysis = get_object_or_404(Analysis, pk=analysis_id)
-            form = AnalysisForm(instance = analysis)
+            form = AnalysisForm(request.POST or None, instance = analysis)
             return render(request, 'analysis/analysis_new.html', {'form': form, 'analysis': analysis})
         else:
             form = AnalysisForm()
@@ -38,13 +45,13 @@
 def detail(request, analysis_id):
     analysis = get_object_or_404(Analysis, pk=analysis_id)
     entity = analysis.methodology_version.methodology.entity
-    
+    weight_scenarios = analysis.methodology_version.weightscenario_set.all()
     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
     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"):]
     
-    return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui})
+    return render(request, 'analysis/analysis_detail.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'search_html_ui': search_html_ui, 'weight_scenarios': weight_scenarios})
 
 def analysis_questions(request, analysis_id):
     analysis = get_object_or_404(Analysis, pk=analysis_id)
@@ -57,14 +64,18 @@
     
     return render(request, 'analysis/analysis_questions.html', {'analysis': analysis, 'analysis_detail': analysis_detail, 'methodology_version': analysis.methodology_version})
 
-def analysis_report(request, analysis_id):
+def analysis_report(request, analysis_id, weight_scenario_id = 0):
     analysis = get_object_or_404(Analysis, pk=analysis_id)
-    analysis.calculate_scores()
-#     print "I am in view 'analysis_report'; page: " + analysis.methodology_version.page_set.all()[0].name
-#     print "has radar_chart: " + analysis.methodology_version.page_set.all()[0].radar_chart[:100]
-#     print "has pippo: " + analysis.methodology_version.page_set.all()[0].pippo
-    td_width = 100 / (1 + len(analysis.instance_set.all())) 
-    return render(request, 'analysis/analysis_report.html', {'analysis': analysis, 'td_width': td_width})
+    weight_scenario = analysis.calculate_scores(weight_scenario_id)
+    td_width = 100 / (1 + len(analysis.instance_set.all()))
+    trend_script = "<script type=\"text/javascript\" src=\"//www.google.com/trends/embed.js?hl=it&q="
+    comma = ""
+    for instance in analysis.instance_set.all():
+        trend_script += comma + instance.name
+        comma = ","
+#    trend_script += "eucalyptus,+OpenNebula,+OpenStack,+Mozilla+Firefox" 
+    trend_script += "&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=330\"></script>" 
+    return render(request, 'analysis/analysis_report.html', {'analysis': analysis, 'td_width': td_width, 'trend_script': trend_script, 'weight_scenario': weight_scenario})
 
 def save_answer(request):
     try:
@@ -215,3 +226,19 @@
     else:
         a.id = analysis_xml.attributes["Id"].firstChild.data
         return HttpResponseRedirect(reverse('analysis_list'))
+
+
+def create_a_my_model(request):
+    if request.method == 'POST':
+        form = MyModelForm(request.POST)
+        if form.is_valid():
+            # save the model to database, directly from the form:
+            my_model = form.save()  # reference to my_model is often not needed at all, a simple form.save() is ok
+            # alternatively:
+            # my_model = form.save(commit=False)  # create model, but don't save to database
+            # my.model.something = whatever  # if I need to do something before saving it
+            # my.model.save()
+    else:        
+        form = MyModelForm()
+    c = { 'form' : form }
+    return render(request, 'analysis/template.html', c)