Add management of deletedContributors
Benjamin Renard

Benjamin Renard commited on 2014-01-31 00:58:00
Showing 1 changed files, with 81 additions and 2 deletions.

... ...
@@ -58,6 +58,7 @@ class Group(object):
58 58
     self.uuid=None
59 59
     self.name=None
60 60
     self.contributors={}
61
+    self.deletedContributors={}
61 62
     self.contributions={}
62 63
     self.deletedContributions={}
63 64
 
... ...
@@ -68,6 +69,10 @@ class Group(object):
68 69
       for email in data['contributors']:
69 70
         self.contributors[email]=Contributor()
70 71
         self.contributors[email].load(data['contributors'][email])
72
+      if 'deletedContributors' in data:
73
+        for email in data['deletedContributors']:
74
+          self.deletedContributors[email]=Contributor()
75
+          self.deletedContributors[email].load(data['deletedContributors'][email])
71 76
       for uuid in data['contributions']:
72 77
         self.contributions[uuid]=Contribution()
73 78
         self.contributions[uuid].load(data['contributions'][uuid])
... ...
@@ -85,6 +90,10 @@ class Group(object):
85 90
     for email in self.contributors:
86 91
       contributors[email]=self.contributors[email].export()
87 92
 
93
+    deletedContributors={}
94
+    for email in self.deletedContributors:
95
+      deletedContributors[email]=self.deletedContributors[email].export()
96
+
88 97
     contributions={}
89 98
     for uuid in self.contributions:
90 99
       contributions[uuid]=self.contributions[uuid].export()
... ...
@@ -97,10 +106,38 @@ class Group(object):
97 106
       'uuid': self.uuid,
98 107
       'name': self.name,
99 108
       'contributors': contributors,
109
+      'deletedContributors': deletedContributors,
100 110
       'contributions': contributions,
101 111
       'deletedContributions': deletedContributions
102 112
     }
103 113
 
114
+  def restoreContributor(self, email):
115
+    contributor=Contributor()
116
+    contributor.load(self.deletedContributors[email])
117
+    for uuid in self.deletedContributions:
118
+      if self.deletedContributions[uuid].contributor==contributor.email and self.deletedContributions[uuid].lastChange==contributor.deletionTime:
119
+        self.contributions[uuid]=Contribution()
120
+        self.contributions[uuid].load(self.deletedContributions[uuid].export())
121
+        # Restored contribution must not be more up to date than other
122
+        self.contributions[uuid].lastChange=0
123
+        del self.deletedContributions[uuid]
124
+    contributor.deletionTime=None
125
+    self.contributors[email]=contributor
126
+    del self.deletedContributors[email]
127
+
128
+  def deleteContributor(self, email, time):
129
+    contributor=Contributor()
130
+    contributor.load(self.contributors[email])
131
+    contributor.deletionTime=time
132
+    for uuid in self.contributions:
133
+      if self.contributions[uuid].contributor==email:
134
+        self.deletedContributions[uuid]=Contribution()
135
+        self.deletedContributions[uuid].load(self.contributions[uuid].export())
136
+        self.deletedContributions[uuid].lastChange=time
137
+        del self.contributions[uuid]
138
+    self.deletedContributors[email]=contributor
139
+    del self.contributors[email]
140
+
104 141
   def sync(self, group):
105 142
     ret=Group()
106 143
     ret.uuid=self.uuid
... ...
@@ -108,6 +145,43 @@ class Group(object):
108 145
     # FIXME : Add lastChange on group to permit name choice between to object
109 146
     ret.name=group.name
110 147
 
148
+    ## Deleted Contributors
149
+    for email in self.deletedContributors:
150
+      if email not in group.deletedContributors:
151
+        logging.debug('Contributor %s not deleted on the other' % email)
152
+        lastChange=0
153
+        for uuid in group.contributions:
154
+          if group.contributions[uuid].contributor==email and group.contributions[uuid].lastChange>lastChange:
155
+            lastChange=group.contributions[uuid].lastChange
156
+        if self.deletedContributors[email].deletionTime<lastChange:
157
+          logging.debug('Some modifications are more recent than my deletion, retoring contributors and his contributions')
158
+          # Restore contributor and his contributions
159
+          self.restoreContributor(email)
160
+          continue
161
+        elif email in group.contributors:
162
+          logging.debug('My deletion are more recent than other modification, deleting contributors and his contributions in the other group')
163
+          # Delete contributor and his contributions
164
+          group.deleteContributor(email,self.deletedContributors[email].deletionTime)
165
+      ret.deletedContributors[email]=self.deletedContributors[email]
166
+
167
+    for email in group.deletedContributors:
168
+      if email not in ret.deletedContributors:
169
+        logging.debug('Contributor %s not deleted on me' % email)
170
+        lastChange=0
171
+        for uuid in ret.contributions:
172
+          if ret.contributions[uuid].contributor==email and ret.contributions[uuid].lastChange>lastChange:
173
+            lastChange=ret.contributions[uuid].lastChange
174
+        if group.deletedContributors[email].deletionTime<lastChange:
175
+          logging.debug('Some of my modifications are more recent than the other deletion, retoring contributors and his contributions in the other group')
176
+          # Restore contributor and his contributions
177
+          group.restoreContributor(email)
178
+          continue
179
+        elif email in group.contributors:
180
+          # Delete contributor and his contributions
181
+          logging.debug('The other group deletion are more recent than my modifications, deleting my contributor and his contributions')
182
+          ret.deleteContributor(email,group.deletedContributors[email].deletionTime)
183
+      ret.deletedContributors[email]=group.deletedContributors[email]
184
+
111 185
     ## Contributors
112 186
     ret.contributors=self.contributors
113 187
     for email in group.contributors:
... ...
@@ -185,17 +259,22 @@ class Contributor(object):
185 259
   def __init__(self):
186 260
     self.name=None
187 261
     self.email=None
262
+    self.deletionTime=None
188 263
 
189 264
   def load(self,data):
190 265
     self.name=data['name']
191 266
     self.email=data['email']
267
+    if 'deletionTime' in data:
268
+      self.deletionTime=data['deletionTime']
192 269
 
193 270
   def export(self):
194
-    return {
271
+    ret={
195 272
       'name': self.name,
196 273
       'email': self.email
197 274
     }
198
-
275
+    if self.deletionTime is not None:
276
+      ret['deletionTime']=self.deletionTime
277
+    return ret
199 278
 
200 279
 if __name__ == '__main__':
201 280
   import testdata
202 281