Zionetrix::Git
Repositories
Help
Report an Issue
MyCoServer
Code
Commits
Branches
Tags
Search
Tree:
04db6c0
Branches
Tags
master
MyCoServer
mycoserver
group.py
Fix management of deletedContribution during sync process
Benjamin Renard
commited
04db6c0
at 2014-01-28 22:02:05
group.py
Blame
History
Raw
#!/usr/bin/python # -*- coding: utf-8 -*- import logging import urllib log = logging.getLogger(__name__) class GroupList(object): def __init__(self): self.groups={} self.lastChange=False def load(self,data): if 'lastChange' in data: self.lastChange=data['lastChange'] if 'groups' in data: for g in data['groups']: self.groups[g]=Group() self.groups[g].load(data['groups'][g]) def export(self): groups={} for uuid in self.groups: groups[uuid]=self.groups[uuid].export() return { 'lastChange': self.lastChange, 'groups': groups } def toJSON(self,pretty=False): if pretty: return json.dumps(self.export(),indent=4, separators=(',', ': ')) else: return json.dumps(self.export()) def sync(self,groups): ret=GroupList() if groups.lastChange<self.lastChange: ret.lastChange=self.lastChange else: ret.lastChange=groups.lastChange for uuid in groups.groups: if uuid in self.groups: ret.groups[uuid]=self.groups[uuid].sync(groups.groups[uuid]) else: ret.groups[uuid]=groups.groups[uuid] for uuid in self.groups: if uuid not in ret.groups: ret.groups[uuid]=self.groups[uuid] return ret class Group(object): def __init__(self): self.uuid=None self.name=None self.contributors={} self.contributions={} self.deletedContributions={} def load(self,data): try: self.uuid=data['uuid'] self.name=data['name'] for email in data['contributors']: self.contributors[email]=Contributor() self.contributors[email].load(data['contributors'][email]) for uuid in data['contributions']: self.contributions[uuid]=Contribution() self.contributions[uuid].load(data['contributions'][uuid]) if 'deletedContributions' in data: for uuid in data['deletedContributions']: self.deletedContributions[uuid]=Contribution() self.deletedContributions[uuid].load(data['deletedContributions'][uuid]) return True except Exception,e: logging.error('Error loading JSON data : %s',e) return False def export(self): contributors={} for email in self.contributors: contributors[email]=self.contributors[email].export() contributions={} for uuid in self.contributions: contributions[uuid]=self.contributions[uuid].export() deletedContributions={} for uuid in self.deletedContributions: deletedContributions[uuid]=self.deletedContributions[uuid].export() return { 'uuid': self.uuid, 'name': self.name, 'contributors': contributors, 'contributions': contributions, 'deletedContributions': deletedContributions } def sync(self, group): ret=Group() ret.uuid=self.uuid # FIXME : Add lastChange on group to permit name choice between to object ret.name=group.name ## Contributors ret.contributors=self.contributors for email in group.contributors: if email not in ret.contributors: ret.contributors[email]=group.contributors[email] ## Deleted Contributions for uuid in self.deletedContributions: if uuid in group.deletedContributions: ret.deletedContributions[uuid]=self.deletedContributions[uuid].sync(group.deletedContributions[uuid]) else: ret.deletedContributions[uuid]=self.deletedContributions[uuid] for uuid in group.deletedContributions: if uuid not in ret.deletedContributions: ret.deletedContributions[uuid]=group.deletedContributions[uuid] elif ret.deletedContributions[uuid].lastChange<group.deletedContributions[uuid].lastChange: ret.deletedContributions[uuid]=group.deletedContributions[uuid] ## Contributions for uuid in self.contributions: if uuid in group.contributions: ret.contributions[uuid]=self.contributions[uuid].sync(group.contributions[uuid]) elif uuid not in ret.deletedContributions: ret.contributions[uuid]=self.contributions[uuid] elif self.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange: ret.contributions[uuid]=self.contributions[uuid] del ret.deletedContributions[uuid] for uuid in group.contributions: if uuid not in ret.contributions: if uuid not in ret.deletedContributions: ret.contributions[uuid]=group.contributions[uuid] elif group.contributions[uuid].lastChange>ret.deletedContributions[uuid].lastChange: ret.contributions[uuid]=group.contributions[uuid] del ret.deletedContributions[uuid] return ret class Contribution(object): def __init__(self): self.uuid=None self.contributor=None self.title=None self.cost=None self.date=None self.lastChange=None def load(self,data): self.uuid=data['uuid'] self.contributor=data['contributor'] self.title=data['title'] self.cost=data['cost'] self.date=data['date'] self.lastChange=data['lastChange'] def export(self): return { 'uuid': self.uuid, 'contributor': self.contributor, 'cost': self.cost, 'title': self.title, 'date': self.date, 'lastChange': self.lastChange } def sync(self, c): if c.lastChange>self.lastChange: return c else: return self class Contributor(object): def __init__(self): self.name=None self.email=None def load(self,data): self.name=data['name'] self.email=data['email'] def export(self): return { 'name': self.name, 'email': self.email } if __name__ == '__main__': import testdata import json data=json.loads(testdata.group_as_json) data2=json.loads(testdata.group_as_json2) gl=GroupList() gl.load(data2) print gl.toJSON(True) print 'Try sync groups' gl2=GroupList() gl2.load(data) gl_sync=gl.sync(gl2) print 'Sync group : %s' % gl_sync print gl_sync.toJSON(True)