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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

269) 
270)   def export(self):
Benjamin Renard Add management of deletedCo...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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