Switch to side-by-side view

--- a/OSSEval/analysis/models.py
+++ b/OSSEval/analysis/models.py
@@ -53,21 +53,48 @@
         str_xml += "<Documentation><![CDATA[" + self.documentation + "]]></Documentation>"
         str_xml += self.entity.to_xml()
         return '<Methodology Id="' + str(self.id) + '" Name="' + self.name + '" Active="' + str(self.active) + '">' + str_xml + "</Methodology>"
+    
 
 class HasPages(models.Model):
-    bar_chart = models.TextField(blank=True)
-    radar_chart = models.TextField(blank=True)
-    def create_graphs(self, instances, include_max = True):
+    def bar_chart(self, weight_profile_id):
+        weight_scenario = WeightScenario.objects.get(pk=weight_profile_id)
+        if self.__class__.__name__ == "Page":
+            g = Graphs.objects.get(page = self, weight_scenario=weight_scenario)
+        else:
+            g = Graphs.objects.get(methodology_version=self, weight_scenario=weight_scenario)
+        return g.bar_chart
+    
+    def radar_chart(self, weight_profile_id):
+        weight_scenario = WeightScenario.objects.get(pk=weight_profile_id)
+        if self.__class__.__name__ == "Page":
+            g = Graphs.objects.get(page = self, weight_scenario=weight_scenario)
+        else:
+            g = Graphs.objects.get(methodology_version=self, weight_scenario=weight_scenario)
+        return g.radar_chart
+    
+#     radar_chart = models.TextField(blank=True)
+    def create_graphs(self, instances, weight_scenario, include_max = True):
         # I draw the graphs for the report
         #styles = [NeonStyle, DarkSolarizedStyle, LightSolarizedStyle, LightStyle, CleanStyle, RedBlueStyle, DarkColorizedStyle, LightColorizedStyle, TurquoiseStyle, LightGreenStyle, DarkGreenStyle, DarkGreenBlueStyle, BlueStyle]
         #style=styles[randrange(len(styles))]
         style=LightColorizedStyle
         bar_chart = Bar(width=300, height=400, explicit_size=True, rounded_bars=5, disable_xml_declaration=True, style=style)
         radar_chart = Radar(width=400, height=400, explicit_size=True, legend_at_bottom=True, disable_xml_declaration=True, style=style)
+
         if self.__class__.__name__ == "Page":
             radar_chart.title = self.name
+            try:
+                g = Graphs.objects.get(page = self, weight_scenario=weight_scenario)
+                g.delete()
+            except:
+                pass
         else:
             radar_chart.title = 'Summary graph'
+            try:
+                g = Graphs.objects.get(methodology_version=self, weight_scenario=weight_scenario)
+                g.delete()
+            except:
+                pass
         radar_chart.x_labels = []
         instance_scores = {}
         for instance in instances:
@@ -92,9 +119,19 @@
         if include_max:
             radar_chart.add("MAX", instance_scores[0])
             bar_chart.add("MAX", sum(instance_scores[0]))
-        self.radar_chart = radar_chart.render()
-        self.bar_chart = bar_chart.render()
-        self.save()
+        g = Graphs()
+        g.radar_chart = radar_chart.render()
+        g.bar_chart = bar_chart.render()
+        if self.__class__.__name__ == 'Page':
+            g.page = self
+        else:
+            g.methodology_version = self
+        g.weight_scenario = weight_scenario
+        g.save()
+        
+#         self.radar_chart = radar_chart.render()
+#         self.bar_chart = bar_chart.render()
+#         self.save()
     class Meta:
         abstract = True
 
@@ -232,7 +269,7 @@
                 pass
         self.save()
         ps.save()
-        self.create_graphs(instances)
+        self.create_graphs(instances, weight_scenario)
         return ps.score
         
     class Meta:
@@ -401,6 +438,14 @@
 
     def __str__(self):
         return self.name
+
+
+class Graphs(models.Model):
+    bar_chart = models.TextField(blank=True)
+    radar_chart = models.TextField(blank=True)
+    page = models.ForeignKey(Page,null=True,blank=True)
+    methodology_version = models.ForeignKey(MethodologyVersion,null=True,blank=True)
+    weight_scenario = models.ForeignKey(WeightScenario)
 
 class Weight(models.Model):
     question = models.ForeignKey(Question)
@@ -465,14 +510,8 @@
         for page in self.methodology_version.page_set.all():
             for instance in self.instance_set.all():
                 page.calculate_scores(instance, weight_scenario, self.instance_set.all())
-        self.methodology_version.create_graphs(self.instance_set.all())
+        self.methodology_version.create_graphs(self.instance_set.all(), weight_scenario)
         return weight_scenario
-        
-#  @staticmethod
-#     def create_graphs(object, pages, instances):
-        '''
-        a static method because I need to create the same 
-        '''
         
     def from_xml(self, xmldoc, insert = True):
         '''
@@ -607,3 +646,5 @@
     Used to save uploaded xml file so that it can be later retrieved and imported
     '''
     docfile = models.FileField(upload_to='documents/%Y/%m/%d')
+
+