Switch to side-by-side view

--- a/OSSEval/OpenSourceProject/models.py
+++ b/OSSEval/OpenSourceProject/models.py
@@ -6,10 +6,10 @@
 from ohloh import OhlohProxy
 from flossmole import FSF, FC, GC, GH
 from utils import Forges
+from OSSEval.utils import xmlMinidom
 
 class OSProject(models.Model):
     """
-    Must be the same name of the Django app                         ??????????????????????????????????????
     """
     name = models.CharField(max_length=200)
     instance = models.OneToOneField('analysis.Instance', related_name='actual_instance', primary_key=True)
@@ -17,6 +17,23 @@
     def __str__(self):
         return self.name + " - " + self.instance.name
     
+    def to_xml(self):
+        str_xml = "<OSProjectForges>"
+        for osprojectforge in self.osprojectforge_set.all():
+            str_xml += osprojectforge.to_xml()
+        str_xml += "</OSProjectForges>"
+        return '<OSProject Name="' + self.name + '">' + str_xml + "</OSProject>"
+    
+    def from_xml(self, xmldoc, instance, insert = True):
+        self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
+        self.instance = instance
+        self.save()
+        #OSProjectForge
+        xml_osproject_forges = xmldoc.getElementsByTagName('OSProjectForge')
+        for xml_osproject_forge in xml_osproject_forges:
+            ospf = OSProjectForge()
+            ospf.from_xml(xml_osproject_forge, self, insert)
+
     def getInstanceInfo(self):
         '''
         Part of the required interface
@@ -107,8 +124,14 @@
     name = models.CharField(max_length=50)
     url = models.CharField(max_length=200)
     os_projects = models.ManyToManyField(OSProject, through='OSProjectForge', blank=True)
+
     def __str__(self):
         return self.name + " - " + self.url
+
+    def to_xml(self):
+        return '<Forge Id="' + str(self.id) + '" Name="' + self.name + '" URL="' + self.url + '" />'
+    #forges do not get added using xml, neither updated; they're are just a reference for OSProjectForge
+    #hence there is no "from_xml"
 
 class OSProjectForge(models.Model):
     name = models.CharField(max_length=200)
@@ -116,8 +139,27 @@
     os_project = models.ForeignKey(OSProject)
     identifier_in_forge = models.CharField(max_length=200)
     datasource_id = models.IntegerField()
+    
+    def to_xml(self):
+        str_xml = self.forge.to_xml()
+        return '<OSProjectForge Id="' + str(self.id) + '" Name="' + self.name + '" IdentifierInForge="' + str(self.identifier_in_forge) + '" DatasourceId="' + str(self.datasource_id) + '">' + str_xml + "</OSProjectForge>"
+    
+    def from_xml(self, xmldoc, os_project, insert = True):
+        if not insert:
+            self.id = xmlMinidom.getNaturalAttribute(xmldoc, 'Id')
+        self.name = xmlMinidom.getStringAttribute(xmldoc, 'Name')
+        self.identifier_in_forge = xmlMinidom.getStringAttribute(xmldoc, 'IdentifierInForge')
+        self.datasource_id = xmlMinidom.getNaturalAttribute(xmldoc, 'DatasourceId')
+        self.os_project = os_project
+        #Forge
+        xml_forge = xmldoc.getElementsByTagName('Forge')[0]
+        f = Forge.objects.get(pk=xmlMinidom.getNaturalAttribute(xml_forge, 'Id'))
+        self.forge = f
+        self.save()
+    
     def __str__(self):
         return self.name + " - " + self.forge.name
+    
     def getProjectInfo(self):
         if self.forge.id == Forges.OHLOH:
             osprojectforge_info = OhlohProxy.getProjectInfo(self.identifier_in_forge)