django-cms 代码研究(八)app hooks

app钩子,啥玩意呢?

就是把现有的app,集成到cms的一种手段。

有两种实现方式:

1) 定义cms_app.py,如下:

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _

class MyApphook(CMSApp):
    name = _("My Apphook")
    urls = ["myapp.urls"]

apphook_pool.register(MyApphook)

官方文档查看这里:http://docs.django-cms.org/en/latest/extending_cms/app_integration.html#app-hooks

加载逻辑,通过discover_apps的load(‘cms_app‘)来加载(前提是在settings.py中未定义 CMS_APPHOOKS):在所有的installed_app中,查找cms_app模块,并自动import_module

 

2) 在setting.py中定义CMS_APPHOOKS

APPHOOKS=(
‘yourmodule.you_object1‘,‘yourmodule.you_object2‘,...
)

这是从源码中分析的来的,如下:

C:\Python27\Lib\site-packages\django_cms-3.0.3-py2.7.egg\cms\apphook_pool.py (45~57)

    def discover_apps(self):
        self.apphooks = get_cms_setting(‘APPHOOKS‘)

        if self.apphooks:
            for cls in iterload_objects(self.apphooks):
                try:
                    self.register(cls, discovering_apps=True)
                except AppAlreadyRegistered:
                    pass

        else:
            load(‘cms_app‘)

        self.discovered = True

iterload_objects,是一个生成器,如下:

def iterload_objects(import_paths):
    """
    Load a list of objects.
    """
    for import_path in import_paths:
        yield load_object(import_path)

load_object

def load_object(import_path):
    if ‘.‘ not in import_path:
        raise TypeError(
            "‘import_path‘ argument to ‘django_load.core.load_object‘ must "
            "contain at least one dot."
        )
    module_name, object_name = import_path.rsplit(‘.‘, 1)
    module = import_module(module_name)
    return getattr(module, object_name)

  



 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。