Xadmin plug-in mechanism and xadmin plug-in mechanism
If @ filter_hook is added to the view method of xadmin, it can be used as the hook function of the plug-in.
For example, there are many methods with the above mark in the ListAdminView class,
@filter_hook def get_context(self): """ Prepare the context for templates. """ self.title = _('%s List') % force_unicode(self.opts.verbose_name) model_fields = [(f, f.name in self.list_display, self.get_check_field_url(f)) for f in (self.opts.fields + self.get_model_method_fields()) if f.name not in self.list_exclude] new_context = { 'module_name': force_unicode(self.opts.verbose_name_plural), 'title': self.title, 'cl': self, 'model_fields': model_fields, 'clean_select_field_url': self.get_query_string(remove=[COL_LIST_VAR]), 'has_add_permission': self.has_add_permission(), 'app_label': self.app_label, 'brand_name': self.opts.verbose_name_plural, 'brand_icon': self.get_model_icon(self.model), 'add_url': self.model_admin_url('add'), 'result_headers': self.result_headers(), 'results': self.results() } context = super(ListAdminView, self).get_context() context.update(new_context) return context @filter_hook def get_response(self, context, *args, **kwargs): pass
In the above Code, the get_context method is used as a plug-in hook function. When this method is called, The ListAdminView registered plug-in will be traversed to find the method with the same name as get_context in the plug-in, and the get_context
The execution result is passed as the second parameter (the first parameter is self) to the get_context method of the plug-in. Therefore, we can modify the result before the get_context method returns the result. Because of this, the method with the same name of the plug-in will have one more parameter than the method of the view (used to receive the return value from the previous method ). But this is not absolute. If the hook method does not return a value, the plug-in method does not need to set one more parameter.
For example, if we want to add a variable (var) to the context that ListAdminView sends to the template, we can define a plug-in as follows:
The following is the implementation principle of the plug-in mechanism, which is actually the wrap target method. In the decorator, traverse the plug-in to form a list of plug-in methods for the target method, and then recursively execute the target method after it is executed.
Note: func if fargs [1] = '_' else func (), that is to say, you can decide whether to pass the previous result or the previous method based on the parameter name.
You can control the execution sequence of methods and make some changes before the execution of the previous method.
def filter_chain(filters, token, func, *args, **kwargs): if token == -1: return func() else: def _inner_method(): fm = filters[token] fargs = getargspec(fm)[0] if len(fargs) == 1: # Only self arg result = func() if result is None: return fm() else: raise IncorrectPluginArg(u'Plugin filter method need a arg to receive parent method result.') else: return fm(func if fargs[1] == '__' else func(), *args, **kwargs) return filter_chain(filters, token - 1, _inner_method, *args, **kwargs)def filter_hook(func): tag = func.__name__ func.__doc__ = "``filter_hook``\n\n" + (func.__doc__ or "") @functools.wraps(func) def method(self, *args, **kwargs): def _inner_method(): return func(self, *args, **kwargs) if self.plugins: filters = [(getattr(getattr(p, tag), 'priority', 10), getattr(p, tag)) for p in self.plugins if callable(getattr(p, tag, None))] filters = [f for p, f in sorted(filters, key=lambda x:x[0])] return filter_chain(filters, len(filters) - 1, _inner_method, *args, **kwargs) else: return _inner_method() return method
How does the eclipse plug-in mechanism facilitate web development?
You mean to configure tomcat in eclipse, and start tocmat from eclipse.
This is simple
How is php plug-in implemented?