4e883c370594f2fda743be7a67acb4142226fe35
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

1) #!/usr/bin/python
2) # -*- coding: utf-8 -*-
3) 
4) import logging
5) import urllib
6) log = logging.getLogger(__name__)
7) 
8) class GroupList(object):
9) 
10)   def __init__(self):
11)     self.groups={}
12)     self.lastChange=False   
13) 
14)   def load(self,data):
15)     if 'lastChange' in data:
16)       self.lastChange=data['lastChange']
17)     if 'groups' in data:
18)       for g in data['groups']:
19)         self.groups[g]=Group()
20)         self.groups[g].load(data['groups'][g])
21) 
22)   def export(self):
23)     groups={}
24)     for uuid in self.groups:
25)       groups[uuid]=self.groups[uuid].export()
26) 
27)     return {
28)       'lastChange': self.lastChange,
29)       'groups': groups
30)     }
31) 
32)   def toJSON(self,pretty=False):
33)     if pretty:
34)       return json.dumps(self.export(),indent=4, separators=(',', ': '))
35)     else:
36)       return json.dumps(self.export())
37) 
38)   def sync(self,groups):
39)     ret=GroupList()
40)     if groups.lastChange<self.lastChange:
41)       ret.lastChange=self.lastChange
42)     else:
43)       ret.lastChange=groups.lastChange
44)     for uuid in groups.groups:
45)       if uuid in self.groups:
46)         ret.groups[uuid]=self.groups[uuid].sync(groups.groups[uuid])
47)       else:
48)         ret.groups[uuid]=groups.groups[uuid]
Benjamin Renard Fixed forget step in group...

Benjamin Renard authored 10 years ago

49)     for uuid in self.groups:
50)       if uuid not in ret.groups:
51)         ret.groups[uuid]=self.groups[uuid]
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

52)     return ret
53) 
54) 
55) class Group(object):
56) 
57)   def __init__(self):
58)     self.uuid=None
59)     self.name=None
60)     self.contributors={}
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

61)     self.deletedContributors={}
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

62)     self.contributions={}
63)     self.deletedContributions={}
64) 
65)   def load(self,data):
66)     try:
67)       self.uuid=data['uuid']
68)       self.name=data['name']
69)       for email in data['contributors']:
70)         self.contributors[email]=Contributor()
71)         self.contributors[email].load(data['contributors'][email])
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

72)       if 'deletedContributors' in data:
73)         for email in data['deletedContributors']:
74)           self.deletedContributors[email]=Contributor()
75)           self.deletedContributors[email].load(data['deletedContributors'][email])
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

76)       for uuid in data['contributions']:
77)         self.contributions[uuid]=Contribution()
78)         self.contributions[uuid].load(data['contributions'][uuid])
79)       if 'deletedContributions' in data:
80)         for uuid in data['deletedContributions']:
81)           self.deletedContributions[uuid]=Contribution()
82)           self.deletedContributions[uuid].load(data['deletedContributions'][uuid])
83)       return True
84)     except Exception,e:
85)       logging.error('Error loading JSON data : %s',e)
86)       return False
87) 
88)   def export(self):
89)     contributors={}
90)     for email in self.contributors:
91)       contributors[email]=self.contributors[email].export()
92) 
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

93)     deletedContributors={}
94)     for email in self.deletedContributors:
95)       deletedContributors[email]=self.deletedContributors[email].export()
96) 
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

97)     contributions={}
98)     for uuid in self.contributions:
99)       contributions[uuid]=self.contributions[uuid].export()
100) 
101)     deletedContributions={}
102)     for uuid in self.deletedContributions:
103)       deletedContributions[uuid]=self.deletedContributions[uuid].export()
104) 
105)     return {
106)       'uuid': self.uuid,
107)       'name': self.name,
108)       'contributors': contributors,
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

109)       'deletedContributors': deletedContributors,
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

110)       'contributions': contributions,
111)       'deletedContributions': deletedContributions
112)     }
113) 
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

114)   def restoreContributor(self, email):
115)     contributor=Contributor()
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

116)     contributor.load(self.deletedContributors[email].export())
117)     for uuid in self.deletedContributions.keys():
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

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()
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

130)     contributor.load(self.contributors[email].export())
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

131)     contributor.deletionTime=time
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

132)     for uuid in self.contributions.keys():
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

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) 
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

141)   def sync(self, group):
142)     ret=Group()
143)     ret.uuid=self.uuid
144) 
145)     # FIXME : Add lastChange on group to permit name choice between to object
146)     ret.name=group.name
147) 
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

148)     ## Deleted Contributors
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

149)     for email in self.deletedContributors.keys():
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

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) 
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

167)     for email in group.deletedContributors.keys():
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

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) 
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

185)     ## Contributors
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

186)     for email in self.contributors:
187)       if email not in ret.deletedContributors:
188)         ret.contributors[email]=self.contributors[email]
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

189)     for email in group.contributors:
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

190)       if email not in ret.contributors and email not in ret.deletedContributors:
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

191)         ret.contributors[email]=group.contributors[email]
192) 
193)     ## Deleted Contributions
194)     for uuid in self.deletedContributions:
195)       if uuid in group.deletedContributions:
196)         ret.deletedContributions[uuid]=self.deletedContributions[uuid].sync(group.deletedContributions[uuid])
197)       else:
198)         ret.deletedContributions[uuid]=self.deletedContributions[uuid]
199)     for uuid in group.deletedContributions:
200)       if uuid not in ret.deletedContributions:
201)         ret.deletedContributions[uuid]=group.deletedContributions[uuid]
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

202)       elif ret.deletedContributions[uuid].lastChange<group.deletedContributions[uuid].lastChange:
203)         ret.deletedContributions[uuid]=group.deletedContributions[uuid]
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

204) 
205)     ## Contributions
206)     for uuid in self.contributions:
207)       if uuid in group.contributions:
208)         ret.contributions[uuid]=self.contributions[uuid].sync(group.contributions[uuid])
209)       elif uuid not in ret.deletedContributions:
210)         ret.contributions[uuid]=self.contributions[uuid]
211)       elif self.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange:
212)         ret.contributions[uuid]=self.contributions[uuid]
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

213)         del ret.deletedContributions[uuid]
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

214)     for uuid in group.contributions:
215)       if uuid not in ret.contributions:
216)         if uuid not in ret.deletedContributions:
217)           ret.contributions[uuid]=group.contributions[uuid]
218)         elif group.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange:
219)           ret.contributions[uuid]=group.contributions[uuid]
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

220)           del ret.deletedContributions[uuid]
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

221) 
222)     return ret
223) 
224) 
225) class Contribution(object):
226) 
227)   def __init__(self):
228)     self.uuid=None
229)     self.contributor=None
230)     self.title=None
231)     self.cost=None
232)     self.date=None
233)     self.lastChange=None
234) 
235)   def load(self,data):
236)     self.uuid=data['uuid']
237)     self.contributor=data['contributor']
238)     self.title=data['title']
239)     self.cost=data['cost']
240)     self.date=data['date']
241)     self.lastChange=data['lastChange']
242) 
243)   def export(self):
244)     return {
245)       'uuid': self.uuid,
246)       'contributor': self.contributor,
247)       'cost': self.cost,
248)       'title': self.title,
249)       'date': self.date,
250)       'lastChange': self.lastChange
251)     }
252) 
253)   def sync(self, c):
254)     if c.lastChange>self.lastChange:
255)       return c
256)     else:
257)       return self
258) 
259) class Contributor(object):
260) 
261)   def __init__(self):
262)     self.name=None
263)     self.email=None
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

264)     self.deletionTime=None
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

265) 
266)   def load(self,data):
267)     self.name=data['name']
268)     self.email=data['email']
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

269)     if 'deletionTime' in data:
270)       self.deletionTime=data['deletionTime']
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

271) 
272)   def export(self):
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

273)     ret={
Benjamin Renard Implementation of groups ob...

Benjamin Renard authored 10 years ago

274)       'name': self.name,
275)       'email': self.email
276)     }
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

277)     if self.deletionTime is not None:
278)       ret['deletionTime']=self.deletionTime
279)     return ret