跳转到主要内容

基于命名空间的Apphooks配置

项目描述

PyPI Version Coverage Status

aldryn-apphooks-config

基于命名空间的Apphooks配置

基本概念

apphooks-config的概念是将所有配置存储在应用程序特定的模型中,并让开发者以表单的形式指定所需的选项。在视图中,加载特定于当前应用程序命名空间的自定义模型实例(通过混入),从而在视图中提供当前命名空间的配置。

命名空间可以在< span class="docutils literal">页面管理中的< strong>高级设置中动态创建,步骤如上所示。在创建应用程序配置时,您实际上是在定义一个命名空间,该命名空间与普通命名空间一起保存在< span class="docutils literal">页面模型中的相同字段中。

贡献

我们对所有帮助创建和维护此包的贡献者表示感激。

贡献者名单在贡献页面上。

支持版本

Python:3.9 - 3.11 Django:3.2 - 4.2 django CMS:3.9 - 3.11

实施步骤指南

  • cms_appconfig.py中定义AppHookConfig模型

    from aldryn_apphooks_config.models import AppHookConfig
    
    class NewsBlogConfig(AppHookConfig):
        pass

    实施可以完全为空,因为架构是在父(抽象)模型中定义的

  • 在您的模型中使用apphooks管理器

    from aldryn_apphooks_config.managers import AppHookConfigManager
    
    class Article(models.Model):
        title = models.CharField()
    
        objects = AppHookConfigManager()

AppHookConfigManager向管理器和查询集添加namespace方法

Article.objects.namespace('foobar')

还有一个适当的查询集,即ApphooksConfigQueryset。在aldryn_apphooks_config.managers.parler中可以找到集成的Parler变体。名称是AppHookConfigTranslatableManagerAppHookConfigTranslatableQueryset

  • cms_appconfig.py中定义ConfigForm

    from app_data import AppDataForm
    from django import forms
    from aldryn_newsblog.models import NewsBlogConfig
    from aldryn_apphooks_config.utils import setup_config
    
    class BlogOptionForm(AppDataForm):
        # fields are totally arbitrary: any form field supported by
        # django-appdata is supported
        show_authors = forms.BooleanField(required=False)
        ...
    
    # this function will register the provided form with the model created
    # at the above step
    setup_config(BlogOptionForm, NewsBlogConfig)
    
    # setup_config can be used as a decorator too, but the `model`
    # attribute must be added to the form class
    @setup_config
    class BlogOptionForm(AppDataForm):
        model = NewsBlogConfig
  • 为AppHookConfig模型定义一个管理类(通常在admin.py中)

    from django.contrib import admin
    from aldryn_apphooks_config.admin import BaseAppHookConfig
    
    class BlogConfigAdmin(BaseAppHookConfig):
    
        def get_config_fields(self):
            # this method **must** be implemented and **must** return the
            # fields defined in the above form, with the ``config`` prefix
            # This is dependent on the django-appdata API
            return ('config.show_authors', ...)
  • 定义由应用程序提供的CMSApp派生的CMSConfigApp(在cms_app.py/cms_apps.py中)

    from aldryn_apphooks_config.app_base import CMSConfigApp
    from cms.apphook_pool import apphook_pool
    from django.utils.translation import ugettext_lazy as _
    from .models import NewsBlogConfig
    
    
    class NewsBlogApp(CMSConfigApp):
        name = _('NewsBlogApp')
        urls = ['aldryn_newsblog.urls']
        app_name = 'aldryn_newsblog'
        # this option is specific of CMSConfigApp, and links the
        # CMSApp to a specific AppHookConfig model
        app_config = NewsBlogConfig
    
    apphook_pool.register(NewsBlogApp)
  • 通过继承AppConfigMixin实现您的视图

    from django.views.generic.detail import DetailView
    from aldryn_apphooks_config.mixins import AppConfigMixin
    
    class ArticleDetail(AppConfigMixin, DetailView):
        def get_queryset(self):
            return Article.objects.namespace(self.namespace)

    AppConfigMixin为命名空间提供全面支持,因此视图不需要设置任何特定于支持它们的内容;以下属性设置为视图类实例

    • 当前命名空间在self.namespace

    • 命名空间配置(NewsBlogConfig的实例)在self.config

    • 当前应用程序在传递给Response类的current_app参数中

测试设置

为了正确设置测试数据以运行启用apphook-config的应用程序,请确保将以下代码添加到您的TestCase中

MyTestCase():

    def setUp(self):
        # This is the namespace represented by the AppHookConfig model instance
        self.ns_newsblog = NewsBlogConfig.objects.create(namespace='NBNS')
        self.page = api.create_page(
            'page', self.template, self.language, published=True,
            # this is the name of the apphook defined in the CMSApp class
            apphook='NewsBlogApp',
            # The namespace is the namespace field of the AppHookConfig instance created above
            apphook_namespace=self.ns_newsblog.namespace)
        # publish the page to make the apphook available
        self.page.publish(self.language)

变更日志

0.7.0 (2023-05-07)

  • 添加Django 3.2+支持

0.6.0 (2020-05-12)

  • 添加Django 3.0支持

0.5.3 (2019-10-19)

  • 修复django 2.2+上的媒体资产声明

0.5.2 (2019-01-02)

  • 将弃用的rel.to更改为remote_field.model

  • 修复示例应用的迁移

  • 修复Django 2.0及更高版本的错误

0.5.1 (2018-12-18)

  • 添加Django 2.0和2.1的支持

  • 删除对Django < 1.11的支持

  • 适应测试基础设施(tox/travis),以纳入django CMS 3.6

  • 修复setup.py

0.4.2 (2018-12-17)

  • 修复AppHookConfigWidget中Django 1.10及以下版本的问题

0.4.1 (2018-04-10)

  • 现在需要django-appdata >= 0.2.0

0.4.0 (2018-03-19)

  • 添加Django 1.11兼容性

  • 添加django CMS 3.5兼容性

  • 实现django-appdata 0.2接口

  • 删除south迁移

  • 删除对django CMS 3.3及以下版本的支持

  • 允许使用setup_config作为装饰器

0.3.3 (2017-03-06)

  • 修复MANIFEST.in中的拼写错误

0.3.2 (2017-03-06)

  • 修复setup.py问题

  • 将区域文件添加到MANIFEST.in

0.3.1 (2017-03-02)

  • 添加翻译系统

  • 添加德语翻译

0.3.0 (2017-01-06)

  • 允许覆盖AppHookConfigField属性

  • 删除Django 1.7及以下版本

  • 删除django CMS 3.1及以下版本

  • 添加Django 1.10支持

0.2.7 (2016-03-03)

  • 将命名空间设置为只读

  • 添加官方Django 1.9支持

  • 更新readme

  • 在resolve中使用path_info而不是path

0.2.6 (2015-10-05)

  • 添加Python 3.5支持

  • 添加Django 1.9a1支持

  • 代码样式清理和测试

0.2.5 (2015-09-25)

  • 添加Django 1.8和django CMS 3.2支持

  • AppHookConfigTranslatableManager.get_queryset应使用queryset_class

  • 如果不存在 app_config 字段,则跳过覆盖管理员表单

0.2.4 (2015-04-20)

  • 修复了设置后无法更改 apphook 的问题。

  • 为 namespace_url 模板标签添加了可选的 'default' 关键字参数。

0.1.0 (2014-01-01)

  • 在 PyPI 上发布了第一个版本。

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

aldryn-apphooks-config-0.7.0.tar.gz (26.3 kB 查看哈希值)

上传时间 源代码

构建分发

aldryn_apphooks_config-0.7.0-py2.py3-none-any.whl (31.8 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下组织支持