Skip to main content

PyPI Version Build Status Coverage Status

aldryn-apphooks-config

Namespaces based configuration for Apphooks

Basic concepts

The concept of apphooks-config is to store all the configuration in an applications-specific model, and let the developer specify the desired option in a form. In the views the model instance specific for the current application namespace is loaded (through a mixin) and it’s thus available in the view to provide the configuration for the current namespace.

Namespaces can be created on the fly in the Page admin Advanced settings by following the steps above. When creating an application configuration, you are in fact defining a namespace, which is saved in the same field in the Page model as the plain namespaces.

Implementation step-guide

  • Define a AppHookConfig model:

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

    Implementation can be completely empty as the schema is defined in the parent (abstract) model

  • Use apphooks managers in your model:

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

AppHookConfigManager adds namespace method to manager and queryset:

Article.objects.namespace('foobar')

There is also a proper queryset, the ApphooksConfigQueryset. Parler integrated variants can be found in aldryn_apphooks_config.managers.parler. Names are AppHookConfigTranslatableManager and AppHookConfigTranslatableQueryset.

  • Define a 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)
  • Define an admin class for the AppHookConfig model:

    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', ...)
  • Define a CMSApp derived from CMSConfigApp provided by this application:

    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)
  • Implements your views inheriting the 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 provides a complete support to namespaces, so the view is not required to set anything specific to support them; the following attributes are set for the view class instance:

    • current namespace in self.namespace

    • namespace configuration (the instance of NewsBlogConfig) in self.config

    • current application in the current_app parameter passed to the Response class

Test setup

To properly setup the data for tests to run for a apphook-config enabled application, make sure you add the following code to your 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)

History

0.2.4 (2015-04-20)
  • Fixes issue where an apphook could not be changed, once set.

  • Addes optional ‘default’ kwarg to namespace_url templatetag

0.1.0 (2014-01-01)
  • First release on PyPI.

Metadata

Release files for aldryn-apphooks-config 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aldryn-apphooks-config 0.2.5
File Size Uploaded
aldryn-apphooks-config-0.2.5.tar.gz 18.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aldryn-apphooks-config 0.2.5
File Interpreter ABI Platform
aldryn_apphooks_config-0.2.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 44.2 kB

Release files / aldryn-apphooks-config-0.2.5.tar.gz

Download URL aldryn-apphooks-config-0.2.5.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e2993ddb76872fdd93cc4f8c3f467bbe30e26f1a7701b497774afddfcce1030e
BLAKE2b-256 checksum
How to use checksums
06b160e71d7031ad639aef74f906ebf1a328e91feb047327ba0e72c1717e5ede
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aldryn_apphooks_config-0.2.5-py2.py3-none-any.whl

Download URL aldryn_apphooks_config-0.2.5-py2.py3-none-any.whl
Size 26.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
27e164dad810a526c201013549df38e671aa1f4e037c19ef132ccc1d804e0880
BLAKE2b-256 checksum
How to use checksums
39d4a0e94db8316f80818f02fef3287e5994bcefa270571f725f001c53d6043b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.7.0

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

1 release file

0.5.1

1 release file

0.4.2

1 release file

0.4.1

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.7

2 release files

0.2.6

2 release files

This release

0.2.5 This release

2 release files

0.2.4

1 release file

0.2.3

2 release files

0.2.2

1 release file

0.2.1

2 release files

0.2.0

2 release files

0.1.4

1 release file

0.1.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page