+

:4736 Function create_function() is deprecated [8192]

Last commit for mycoserver/group.py: 87622d7cd34753585e5c4e76c9c8c411feb891c2

Add uuid on category

Benjamin Renard [2014-08-17 18:54:21]
Add uuid on category
  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]
  49. for uuid in self.groups:
  50. if uuid not in ret.groups:
  51. ret.groups[uuid]=self.groups[uuid]
  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.deletedContributors={}
  62. self.contributions={}
  63. self.deletedContributions={}
  64. self.categories={}
  65. self.deletedCategories={}
  66.  
  67. def load(self,data):
  68. self.uuid=data.get('uuid',None)
  69. self.name=data.get('name',None)
  70. for email in data.get('contributors'):
  71. self.contributors[email]=Contributor()
  72. self.contributors[email].load(data['contributors'][email])
  73. if 'deletedContributors' in data:
  74. for email in data.get('deletedContributors',{}):
  75. self.deletedContributors[email]=Contributor()
  76. self.deletedContributors[email].load(data['deletedContributors'][email])
  77. for uuid in data.get('contributions',{}):
  78. self.contributions[uuid]=Contribution()
  79. self.contributions[uuid].load(data['contributions'][uuid])
  80. if 'deletedContributions' in data:
  81. for uuid in data.get('deletedContributions',{}):
  82. self.deletedContributions[uuid]=Contribution()
  83. self.deletedContributions[uuid].load(data['deletedContributions'][uuid])
  84. if 'categories' in data:
  85. for uuid in data.get('categories',{}):
  86. self.categories[uuid]=Category()
  87. self.categories[uuid].load(data['categories'][uuid])
  88. if 'deletedCategories' in data:
  89. for uuid in data.get('deletedCategories',{}):
  90. self.deletedCategories[uuid]=Category()
  91. self.deletedCategories[uuid].load(data['deletedCategories'][uuid])
  92. return True
  93.  
  94. def export(self):
  95. contributors={}
  96. for email in self.contributors:
  97. contributors[email]=self.contributors[email].export()
  98.  
  99. deletedContributors={}
  100. for email in self.deletedContributors:
  101. deletedContributors[email]=self.deletedContributors[email].export()
  102.  
  103. contributions={}
  104. for uuid in self.contributions:
  105. contributions[uuid]=self.contributions[uuid].export()
  106.  
  107. deletedContributions={}
  108. for uuid in self.deletedContributions:
  109. deletedContributions[uuid]=self.deletedContributions[uuid].export()
  110.  
  111. categories={}
  112. for uuid in self.categories:
  113. categories[uuid]=self.categories[uuid].export()
  114.  
  115. deletedCategories={}
  116. for uuid in self.deletedCategories:
  117. deletedCategories[uuid]=self.deletedCategories[uuid].export()
  118.  
  119. return {
  120. 'uuid': self.uuid,
  121. 'name': self.name,
  122. 'contributors': contributors,
  123. 'deletedContributors': deletedContributors,
  124. 'contributions': contributions,
  125. 'deletedContributions': deletedContributions,
  126. 'categories': categories,
  127. 'deletedCategories': deletedCategories
  128. }
  129.  
  130. def restoreContributor(self, email):
  131. contributor=Contributor()
  132. contributor.load(self.deletedContributors[email].export())
  133. for uuid in self.deletedContributions.keys():
  134. if self.deletedContributions[uuid].contributor==contributor.email and self.deletedContributions[uuid].lastChange==contributor.deletionTime:
  135. self.contributions[uuid]=Contribution()
  136. self.contributions[uuid].load(self.deletedContributions[uuid].export())
  137. # Restored contribution must not be more up to date than other
  138. self.contributions[uuid].lastChange=0
  139. del self.deletedContributions[uuid]
  140. contributor.deletionTime=None
  141. self.contributors[email]=contributor
  142. del self.deletedContributors[email]
  143.  
  144. def deleteContributor(self, email, time):
  145. contributor=Contributor()
  146. contributor.load(self.contributors[email].export())
  147. contributor.deletionTime=time
  148. for uuid in self.contributions.keys():
  149. if self.contributions[uuid].contributor==email:
  150. self.deletedContributions[uuid]=Contribution()
  151. self.deletedContributions[uuid].load(self.contributions[uuid].export())
  152. self.deletedContributions[uuid].lastChange=time
  153. del self.contributions[uuid]
  154. self.deletedContributors[email]=contributor
  155. del self.contributors[email]
  156.  
  157. def sync(self, group):
  158. ret=Group()
  159. ret.uuid=self.uuid
  160.  
  161. # FIXME : Add lastChange on group to permit name choice between to object
  162. ret.name=group.name
  163.  
  164. ## Deleted Contributors
  165. for email in self.deletedContributors.keys():
  166. if email not in group.deletedContributors:
  167. logging.debug('Contributor %s not deleted on the other' % email)
  168. lastChange=0
  169. for uuid in group.contributions:
  170. if group.contributions[uuid].contributor==email and group.contributions[uuid].lastChange>lastChange:
  171. lastChange=group.contributions[uuid].lastChange
  172. if self.deletedContributors[email].deletionTime<lastChange:
  173. logging.debug('Some modifications are more recent than my deletion, retoring contributors and his contributions')
  174. # Restore contributor and his contributions
  175. self.restoreContributor(email)
  176. continue
  177. elif email in group.contributors:
  178. logging.debug('My deletion are more recent than other modification, deleting contributors and his contributions in the other group')
  179. # Delete contributor and his contributions
  180. group.deleteContributor(email,self.deletedContributors[email].deletionTime)
  181. ret.deletedContributors[email]=self.deletedContributors[email]
  182.  
  183. for email in group.deletedContributors.keys():
  184. if email not in ret.deletedContributors:
  185. logging.debug('Contributor %s not deleted on me' % email)
  186. lastChange=0
  187. for uuid in ret.contributions:
  188. if ret.contributions[uuid].contributor==email and ret.contributions[uuid].lastChange>lastChange:
  189. lastChange=ret.contributions[uuid].lastChange
  190. if group.deletedContributors[email].deletionTime<lastChange:
  191. logging.debug('Some of my modifications are more recent than the other deletion, retoring contributors and his contributions in the other group')
  192. # Restore contributor and his contributions
  193. group.restoreContributor(email)
  194. continue
  195. elif email in group.contributors:
  196. # Delete contributor and his contributions
  197. logging.debug('The other group deletion are more recent than my modifications, deleting my contributor and his contributions')
  198. ret.deleteContributor(email,group.deletedContributors[email].deletionTime)
  199. ret.deletedContributors[email]=group.deletedContributors[email]
  200.  
  201. ## Contributors
  202. for email in self.contributors:
  203. if email not in ret.deletedContributors:
  204. ret.contributors[email]=self.contributors[email]
  205. for email in group.contributors:
  206. if email not in ret.contributors and email not in ret.deletedContributors:
  207. ret.contributors[email]=group.contributors[email]
  208.  
  209. ## Deleted Contributions
  210. for uuid in self.deletedContributions:
  211. if uuid in group.deletedContributions:
  212. ret.deletedContributions[uuid]=self.deletedContributions[uuid].sync(group.deletedContributions[uuid])
  213. else:
  214. ret.deletedContributions[uuid]=self.deletedContributions[uuid]
  215. for uuid in group.deletedContributions:
  216. if uuid not in ret.deletedContributions:
  217. ret.deletedContributions[uuid]=group.deletedContributions[uuid]
  218. elif ret.deletedContributions[uuid].lastChange<group.deletedContributions[uuid].lastChange:
  219. ret.deletedContributions[uuid]=group.deletedContributions[uuid]
  220.  
  221. ## Contributions
  222. for uuid in self.contributions:
  223. if uuid in group.contributions:
  224. ret.contributions[uuid]=self.contributions[uuid].sync(group.contributions[uuid])
  225. elif uuid not in ret.deletedContributions:
  226. ret.contributions[uuid]=self.contributions[uuid]
  227. elif self.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange:
  228. ret.contributions[uuid]=self.contributions[uuid]
  229. del ret.deletedContributions[uuid]
  230. for uuid in group.contributions:
  231. if uuid not in ret.contributions:
  232. if uuid not in ret.deletedContributions:
  233. ret.contributions[uuid]=group.contributions[uuid]
  234. elif group.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange:
  235. ret.contributions[uuid]=group.contributions[uuid]
  236. del ret.deletedContributions[uuid]
  237.  
  238. ## Categories
  239. for uuid in self.categories:
  240. if uuid in group.categories:
  241. ret.categories[uuid]=self.categories[uuid].sync(group.categories[uuid])
  242. elif uuid not in ret.deletedCategories:
  243. ret.categories[uuid]=self.categories[uuid]
  244. elif self.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange:
  245. ret.categories[uuid]=self.categories[uuid]
  246. del ret.deletedCategories[uuid]
  247. for uuid in group.categories:
  248. if uuid not in ret.categories:
  249. if uuid not in ret.deletedCategories:
  250. ret.categories[uuid]=group.categories[uuid]
  251. elif group.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange:
  252. ret.categories[uuid]=group.categories[uuid]
  253. del ret.deletedCategories[uuid]
  254.  
  255. return ret
  256.  
  257.  
  258. class Contribution(object):
  259.  
  260. def __init__(self):
  261. self.uuid=None
  262. self.contributor=None
  263. self.title=None
  264. self.cost=None
  265. self.date=None
  266. self.category=None
  267. self.lastChange=None
  268.  
  269. def load(self,data):
  270. self.uuid=data.get('uuid',None)
  271. self.contributor=data.get('contributor',None)
  272. self.title=data.get('title',None)
  273. self.cost=data.get('cost',None)
  274. self.date=data.get('date',None)
  275. self.category=data.get('category',None)
  276. self.lastChange=data.get('lastChange',None)
  277.  
  278. def export(self):
  279. return {
  280. 'uuid': self.uuid,
  281. 'contributor': self.contributor,
  282. 'cost': self.cost,
  283. 'title': self.title,
  284. 'date': self.date,
  285. 'category': self.category,
  286. 'lastChange': self.lastChange
  287. }
  288.  
  289. def sync(self, c):
  290. if c.lastChange>self.lastChange:
  291. return c
  292. else:
  293. return self
  294.  
  295. class Contributor(object):
  296.  
  297. def __init__(self):
  298. self.name=None
  299. self.email=None
  300. self.deletionTime=None
  301.  
  302. def load(self,data):
  303. self.name=data.get('name',None)
  304. self.email=data.get('email',None)
  305. self.deletionTime=data.get('deletionTime',None)
  306.  
  307. def export(self):
  308. ret={
  309. 'name': self.name,
  310. 'email': self.email
  311. }
  312. if self.deletionTime is not None:
  313. ret['deletionTime']=self.deletionTime
  314. return ret
  315.  
  316. class Category(object):
  317.  
  318. def __init__(self):
  319. self.uuid=None
  320. self.name=None
  321. self.color=None
  322. self.lastChange=None
  323.  
  324. def load(self,data):
  325. self.uuid=data.get('uuid',None)
  326. self.name=data.get('name',None)
  327. self.color=data.get('color',None)
  328. self.lastChange=data.get('lastChange',None)
  329.  
  330. def export(self):
  331. ret={
  332. 'uuid': self.uuid,
  333. 'name': self.name,
  334. 'color': self.color,
  335. 'lastChange': self.lastChange
  336. }
  337. return ret
  338.  
  339. def sync(self, c):
  340. if c.lastChange>self.lastChange:
  341. return c
  342. else:
  343. return self
  344.  
  345. if __name__ == '__main__':
  346. import testdata
  347. import json
  348.  
  349. data=json.loads(testdata.group_as_json)
  350. data2=json.loads(testdata.group_as_json2)
  351.  
  352. gl=GroupList()
  353. gl.load(data2)
  354.  
  355. print gl.toJSON(True)
  356.  
  357. print 'Try sync groups'
  358. gl2=GroupList()
  359. gl2.load(data)
  360. gl_sync=gl.sync(gl2)
  361. print 'Sync group : %s' % gl_sync
  362. print gl_sync.toJSON(True)