+

Add management of deletedContributors

Benjamin Renard [2014-01-31 00:58:00]
Add management of deletedContributors
Filename
mycoserver/group.py
diff --git a/mycoserver/group.py b/mycoserver/group.py
index ff3c7d6..38593ac 100644
--- a/mycoserver/group.py
+++ b/mycoserver/group.py
@@ -58,6 +58,7 @@ class Group(object):
     self.uuid=None
     self.name=None
     self.contributors={}
+    self.deletedContributors={}
     self.contributions={}
     self.deletedContributions={}

@@ -68,6 +69,10 @@ class Group(object):
       for email in data['contributors']:
         self.contributors[email]=Contributor()
         self.contributors[email].load(data['contributors'][email])
+      if 'deletedContributors' in data:
+        for email in data['deletedContributors']:
+          self.deletedContributors[email]=Contributor()
+          self.deletedContributors[email].load(data['deletedContributors'][email])
       for uuid in data['contributions']:
         self.contributions[uuid]=Contribution()
         self.contributions[uuid].load(data['contributions'][uuid])
@@ -85,6 +90,10 @@ class Group(object):
     for email in self.contributors:
       contributors[email]=self.contributors[email].export()

+    deletedContributors={}
+    for email in self.deletedContributors:
+      deletedContributors[email]=self.deletedContributors[email].export()
+
     contributions={}
     for uuid in self.contributions:
       contributions[uuid]=self.contributions[uuid].export()
@@ -97,10 +106,38 @@ class Group(object):
       'uuid': self.uuid,
       'name': self.name,
       'contributors': contributors,
+      'deletedContributors': deletedContributors,
       'contributions': contributions,
       'deletedContributions': deletedContributions
     }

+  def restoreContributor(self, email):
+    contributor=Contributor()
+    contributor.load(self.deletedContributors[email])
+    for uuid in self.deletedContributions:
+      if self.deletedContributions[uuid].contributor==contributor.email and self.deletedContributions[uuid].lastChange==contributor.deletionTime:
+        self.contributions[uuid]=Contribution()
+        self.contributions[uuid].load(self.deletedContributions[uuid].export())
+        # Restored contribution must not be more up to date than other
+        self.contributions[uuid].lastChange=0
+        del self.deletedContributions[uuid]
+    contributor.deletionTime=None
+    self.contributors[email]=contributor
+    del self.deletedContributors[email]
+
+  def deleteContributor(self, email, time):
+    contributor=Contributor()
+    contributor.load(self.contributors[email])
+    contributor.deletionTime=time
+    for uuid in self.contributions:
+      if self.contributions[uuid].contributor==email:
+        self.deletedContributions[uuid]=Contribution()
+        self.deletedContributions[uuid].load(self.contributions[uuid].export())
+        self.deletedContributions[uuid].lastChange=time
+        del self.contributions[uuid]
+    self.deletedContributors[email]=contributor
+    del self.contributors[email]
+
   def sync(self, group):
     ret=Group()
     ret.uuid=self.uuid
@@ -108,6 +145,43 @@ class Group(object):
     # FIXME : Add lastChange on group to permit name choice between to object
     ret.name=group.name

+    ## Deleted Contributors
+    for email in self.deletedContributors:
+      if email not in group.deletedContributors:
+        logging.debug('Contributor %s not deleted on the other' % email)
+        lastChange=0
+        for uuid in group.contributions:
+          if group.contributions[uuid].contributor==email and group.contributions[uuid].lastChange>lastChange:
+            lastChange=group.contributions[uuid].lastChange
+        if self.deletedContributors[email].deletionTime<lastChange:
+          logging.debug('Some modifications are more recent than my deletion, retoring contributors and his contributions')
+          # Restore contributor and his contributions
+          self.restoreContributor(email)
+          continue
+        elif email in group.contributors:
+          logging.debug('My deletion are more recent than other modification, deleting contributors and his contributions in the other group')
+          # Delete contributor and his contributions
+          group.deleteContributor(email,self.deletedContributors[email].deletionTime)
+      ret.deletedContributors[email]=self.deletedContributors[email]
+
+    for email in group.deletedContributors:
+      if email not in ret.deletedContributors:
+        logging.debug('Contributor %s not deleted on me' % email)
+        lastChange=0
+        for uuid in ret.contributions:
+          if ret.contributions[uuid].contributor==email and ret.contributions[uuid].lastChange>lastChange:
+            lastChange=ret.contributions[uuid].lastChange
+        if group.deletedContributors[email].deletionTime<lastChange:
+          logging.debug('Some of my modifications are more recent than the other deletion, retoring contributors and his contributions in the other group')
+          # Restore contributor and his contributions
+          group.restoreContributor(email)
+          continue
+        elif email in group.contributors:
+          # Delete contributor and his contributions
+          logging.debug('The other group deletion are more recent than my modifications, deleting my contributor and his contributions')
+          ret.deleteContributor(email,group.deletedContributors[email].deletionTime)
+      ret.deletedContributors[email]=group.deletedContributors[email]
+
     ## Contributors
     ret.contributors=self.contributors
     for email in group.contributors:
@@ -185,17 +259,22 @@ class Contributor(object):
   def __init__(self):
     self.name=None
     self.email=None
+    self.deletionTime=None

   def load(self,data):
     self.name=data['name']
     self.email=data['email']
+    if 'deletionTime' in data:
+      self.deletionTime=data['deletionTime']

   def export(self):
-    return {
+    ret={
       'name': self.name,
       'email': self.email
     }
-
+    if self.deletionTime is not None:
+      ret['deletionTime']=self.deletionTime
+    return ret

 if __name__ == '__main__':
   import testdata
ViewGit