# HG changeset patch # User Sylvain Thénault # Date 1484816011 -3600 # Node ID 6f36275a6e7418871c8e6109282e9f057fb6a8a4 # Parent 6c320c41b1a0890dea60d05be0f211858cb2527d [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former and enhance their docstring. diff -r 6c320c41b1a0 -r 6f36275a6e74 cubicweb/hooks/syncschema.py --- a/cubicweb/hooks/syncschema.py Wed Jan 18 12:36:02 2017 +0100 +++ b/cubicweb/hooks/syncschema.py Thu Jan 19 09:53:31 2017 +0100 @@ -1,4 +1,4 @@ -# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of CubicWeb. @@ -1088,44 +1088,41 @@ MemSchemaCWRTypeDel(self._cw, rtype=name) -class AfterAddCWComputedRTypeHook(SyncSchemaHook): - """after a CWComputedRType entity has been added: - * register an operation to add the relation type to the instance's - schema on commit - - We don't know yet this point if a table is necessary - """ - __regid__ = 'syncaddcwcomputedrtype' - __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType') - events = ('after_add_entity',) +class AfterAddCWRTypeHook(SyncSchemaHook): + """After a CWRType entity has been added, register an operation to add the + relation type to the instance's schema on commit. - def __call__(self): - entity = self.entity - rtypedef = ybo.ComputedRelation(name=entity.name, - eid=entity.eid, - rule=entity.rule) - MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef) - - -class AfterAddCWRTypeHook(SyncSchemaHook): - """after a CWRType entity has been added: - * register an operation to add the relation type to the instance's - schema on commit - - We don't know yet this point if a table is necessary + We don't know yet at this point if a table is necessary, it will depend on + further addition of relation definitions. """ __regid__ = 'syncaddcwrtype' __select__ = SyncSchemaHook.__select__ & is_instance('CWRType') events = ('after_add_entity',) def __call__(self): + rtypedef = self.rtype_def() + MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef) + + def rtype_def(self): entity = self.entity - rtypedef = ybo.RelationType(name=entity.name, - description=entity.description, - inlined=entity.cw_edited.get('inlined', False), - symmetric=entity.cw_edited.get('symmetric', False), - eid=entity.eid) - MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef) + return ybo.RelationType(name=entity.name, + description=entity.description, + inlined=entity.cw_edited.get('inlined', False), + symmetric=entity.cw_edited.get('symmetric', False), + eid=entity.eid) + + +class AfterAddCWComputedRTypeHook(AfterAddCWRTypeHook): + """After a CWComputedRType entity has been added, register an operation to + add the relation type to the instance's schema on commit. + """ + __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType') + + def rtype_def(self): + entity = self.entity + return ybo.ComputedRelation(name=entity.name, + eid=entity.eid, + rule=entity.rule) class BeforeUpdateCWRTypeHook(SyncSchemaHook):