def clone(self, user, name): from clone import Clone clone = Clone(name, user, self) clone.save() with prefix_collections(clone.get_collection_name(), System, Node): self.save(force_insert=True) with prefix_collections(clone.get_working_copy_collection_name(), System, Node): self.save(force_insert=True) return clone
def add_operation(self, operation): working_copy = self.get_working_copy() operation.apply(working_copy) with prefix_collections(self.get_working_copy_collection_name(), System, Node): working_copy.save() # for node_name, node in working_copy.nodes.iteritems(): # print node_name, node.parents self.operations.append(operation)
def apply(self, system, collection=None): for operation in self.operations: operation.apply(system) system.last_transaction = self if collection is None: system.save() else: System = import_class("System") Node = import_class("Node") with prefix_collections(collection, System, Node): system.save()
def update(self): with prefix_collections(self.get_collection_name(), System, Node): cloned_system = System.objects.get(name=self.system.name) if cloned_system.last_transaction == self.system.last_transaction: raise NothingToUpdateError("your clone is up-to-date, nothing to do") if cloned_system.last_transaction is None: transactions = Transaction.objects(system=self.system).order_by("_id") else: transactions = Transaction.objects(system=self.system, id__gt=cloned_system.last_transaction.id).order_by("_id") operations = [] for transaction in transactions: transaction.apply(cloned_system, self.get_collection_name()) operations += transaction.operations for operation in operations: pass
def merge(self, message=""): if len(self.operations) == 0: raise NothingToMergeError("no operation to merge") with prefix_collections(self.get_collection_name(), System, Node): cloned_system = System.objects.get(name=self.system.name) if self.system.last_transaction != cloned_system.last_transaction: raise MustUpdateError("modification have been merge with the master, please update") transaction = Transaction(self.system, self.operations, message) transaction.save() if self.system.last_transaction is None: to_apply = Transaction.objects(system=self.system).order_by("_id") else: to_apply = Transaction.objects(system=self.system, id__gt=self.system.last_transaction.id).order_by("_id") if len(to_apply) != 1: return #FIXME to_apply[0].apply(self.system) to_apply[0].apply(cloned_system, self.get_collection_name()) self.operations = [] self.save()
def get_working_copy(self): with prefix_collections(self.get_working_copy_collection_name(), System, Node): return System.objects.get(name=self.system.name)
def get_cloned_system(self): with prefix_collections(self.get_collection_name(), System, Node): return System.objects.get(name=self.system.name)