allow subclasses to modify the parents model hooks

fixes bug 1210387

This change modifies the model hook processing to call the method on the
instance if a hook is registered as a string.  If the hook is a
callable, then callable is called with the hook arguments.

Change-Id: Id14ae89f3f12a500920d248226e0ecba8e35e74c
This commit is contained in:
Mark McClain 2013-08-09 00:53:33 -04:00
parent fd1c78a815
commit 3f3346e598
3 changed files with 17 additions and 9 deletions

View File

@ -100,11 +100,16 @@ class CommonDbMixin(object):
for _name, hooks in self._model_query_hooks.get(model, for _name, hooks in self._model_query_hooks.get(model,
{}).iteritems(): {}).iteritems():
query_hook = hooks.get('query') query_hook = hooks.get('query')
filter_hook = hooks.get('filter') if isinstance(query_hook, basestring):
query_hook = getattr(self, query_hook, None)
if query_hook: if query_hook:
query = query_hook(self, context, model, query) query = query_hook(context, model, query)
filter_hook = hooks.get('filter')
if isinstance(filter_hook, basestring):
filter_hook = getattr(self, filter_hook, None)
if filter_hook: if filter_hook:
query_filter = filter_hook(self, context, model, query_filter) query_filter = filter_hook(context, model, query_filter)
# NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
# condition, raising an exception # condition, raising an exception
@ -142,8 +147,11 @@ class CommonDbMixin(object):
for _name, hooks in self._model_query_hooks.get(model, for _name, hooks in self._model_query_hooks.get(model,
{}).iteritems(): {}).iteritems():
result_filter = hooks.get('result_filters', None) result_filter = hooks.get('result_filters', None)
if isinstance(result_filter, basestring):
result_filter = getattr(self, result_filter, None)
if result_filter: if result_filter:
query = result_filter(self, query, filters) query = result_filter(query, filters)
return query return query
def _get_collection_query(self, context, model, filters=None, def _get_collection_query(self, context, model, filters=None,

View File

@ -120,9 +120,9 @@ class L3_NAT_db_mixin(l3.RouterPluginBase):
db_base_plugin_v2.NeutronDbPluginV2.register_model_query_hook( db_base_plugin_v2.NeutronDbPluginV2.register_model_query_hook(
models_v2.Network, models_v2.Network,
"external_net", "external_net",
_network_model_hook, '_network_model_hook',
_network_filter_hook, '_network_filter_hook',
_network_result_filter_hook) '_network_result_filter_hook')
def _get_router(self, context, id): def _get_router(self, context, id):
try: try:

View File

@ -61,9 +61,9 @@ class PortBindingMixin(portbindings_base.PortBindingBaseMixin):
db_base_plugin_v2.NeutronDbPluginV2.register_model_query_hook( db_base_plugin_v2.NeutronDbPluginV2.register_model_query_hook(
models_v2.Port, models_v2.Port,
"portbindings_port", "portbindings_port",
_port_model_hook, '_port_model_hook',
None, None,
_port_result_filter_hook) '_port_result_filter_hook')
def _process_portbindings_create_and_update(self, context, port_data, def _process_portbindings_create_and_update(self, context, port_data,
port): port):