04db6c03db10b059644d565e0d932e4d4482981b
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={}
61)     self.contributions={}
62)     self.deletedContributions={}
63) 
64)   def load(self,data):
65)     try:
66)       self.uuid=data['uuid']
67)       self.name=data['name']
68)       for email in data['contributors']:
69)         self.contributors[email]=Contributor()
70)         self.contributors[email].load(data['contributors'][email])
71)       for uuid in data['contributions']:
72)         self.contributions[uuid]=Contribution()
73)         self.contributions[uuid].load(data['contributions'][uuid])
74)       if 'deletedContributions' in data:
75)         for uuid in data['deletedContributions']:
76)           self.deletedContributions[uuid]=Contribution()
77)           self.deletedContributions[uuid].load(data['deletedContributions'][uuid])
78)       return True
79)     except Exception,e:
80)       logging.error('Error loading JSON data : %s',e)
81)       return False
82) 
83)   def export(self):
84)     contributors={}
85)     for email in self.contributors:
86)       contributors[email]=self.contributors[email].export()
87) 
88)     contributions={}
89)     for uuid in self.contributions:
90)       contributions[uuid]=self.contributions[uuid].export()
91) 
92)     deletedContributions={}
93)     for uuid in self.deletedContributions:
94)       deletedContributions[uuid]=self.deletedContributions[uuid].export()
95) 
96)     return {
97)       'uuid': self.uuid,
98)       'name': self.name,
99)       'contributors': contributors,
100)       'contributions': contributions,
101)       'deletedContributions': deletedContributions
102)     }
103) 
104)   def sync(self, group):
105)     ret=Group()
106)     ret.uuid=self.uuid
107) 
108)     # FIXME : Add lastChange on group to permit name choice between to object
109)     ret.name=group.name
110) 
111)     ## Contributors
112)     ret.contributors=self.contributors
113)     for email in group.contributors:
114)       if email not in ret.contributors:
115)         ret.contributors[email]=group.contributors[email]
116) 
117)     ## Deleted Contributions
118)     for uuid in self.deletedContributions:
119)       if uuid in group.deletedContributions:
120)         ret.deletedContributions[uuid]=self.deletedContributions[uuid].sync(group.deletedContributions[uuid])
121)       else:
122)         ret.deletedContributions[uuid]=self.deletedContributions[uuid]
123)     for uuid in group.deletedContributions:
124)       if uuid not in ret.deletedContributions:
125)         ret.deletedContributions[uuid]=group.deletedContributions[uuid]
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

128) 
129)     ## Contributions
130)     for uuid in self.contributions:
131)       if uuid in group.contributions:
132)         ret.contributions[uuid]=self.contributions[uuid].sync(group.contributions[uuid])
133)       elif uuid not in ret.deletedContributions:
134)         ret.contributions[uuid]=self.contributions[uuid]
135)       elif self.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange:
136)         ret.contributions[uuid]=self.contributions[uuid]
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

138)     for uuid in group.contributions:
139)       if uuid not in ret.contributions:
140)         if uuid not in ret.deletedContributions:
141)           ret.contributions[uuid]=group.contributions[uuid]
142)         elif group.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange:
143)           ret.contributions[uuid]=group.contributions[uuid]
Benjamin Renard Fix management of deletedCo...

Benjamin Renard authored 10 years ago

144)           del ret.deletedContributions[uuid]