Code Monkey home page Code Monkey logo

djangocms-apphook-setup's Introduction

djangocms-apphook-setup

Join the Gitter chat Latest PyPI version Python versions Latest CI build status Test coverage Code Climate License

Utility function to auto setup apphooks on project startup.

Supported Django versions:

  • Django: 3.2, 4.1, 4.2

Supported django CMS versions:

  • django CMS: 3.9, 3.11

Features

The mixin included in this utility allows to automatically add an Apphook to a django CMS project on the first access to the website.

This is intended for use by the django CMS application developers by extending their own CMSApp classes.

This behavior simplify the initial setup of a project and lower the barrier for the end user.

The setup function included here does the following:

  • Check if the Apphook is already added to a CMS page
  • If it is, it skips any further step
  • If not:
    • Creates the home page (if not present)
    • Creates a sub page of the home
    • Adds the application Apphook to it

In case the application uses aldryn-apphooks-config, a Apphook Config instance is created and added to the application page together with the Apphook.

Note

To avoid issues with AldrynSearch during the creation of pages, the url of the pages is faked; this is normally not an issues as the pages will be reindexed whenever the content is updated.

Usage

This utility can be used by extending the CMSApp class, adding the auto_setup attribute with relevant configuration options and triggering setup at the end of cms_app.py:

@apphook_pool.register
class App4(AutoCMSAppMixin, CMSConfigApp):
    name = _('App4')
    urls = ['sample_app_4.urls']
    app_name = 'app4'
    app_config = App4Config
    # djangocms-apphook-setup attribute
    auto_setup = {
        'enabled': True,
        'home title': 'home title',
        'page title': 'page 4 title',
        'namespace': 'namespace',
        'config_fields': {'random_option': True},
        'config_translated_fields': {'app_title': 'app title', 'object_name': 'name'},
        'sites': True,
    }


# trigger djangocms-apphook-setup function
App4.setup()

Customizing ApphookConfig instances creation

While config_fields and config_translated_fields should cover most use cases when it comes to ApphookConfig instances creation, you may need more control over the process.

For this, it's possible to override AutoCMSAppMixin._create_config and AutoCMSAppMixin._create_config_translation.

Default implementation:

@classmethod
def _create_config(cls):
    return cls.app_config.objects.create(
        namespace=cls.auto_setup['namespace'], **cls.auto_setup['config_fields']
    )

@classmethod
def _create_config_translation(cls, config, lang):
    config.set_current_language(lang, initialize=True)
    for field, data in cls.auto_setup['config_translated_fields'].items():
        setattr(config, field, data)
    config.save_translations()

You can completely redefine the methods, provided you return an ApphookConfig instance in _create_config.

Configuration options

The behavior of the setup function can be customized by setting the following keys in the auto_setup attribute:

  • enabled: If True the setup is invoked; a good option is to use a setting to control this to allow application users to disable the behavior (default: True)
  • home title: Title of the home page if created by the setup function; this must be set in the application CMSApp, otherwise the setup function will exit with a warning.
  • page title: Title of the page created by the setup function; this must be set in the application CMSApp, otherwise the setup function will exit with a warning.
  • namespace: Application instance name used when attaching the Apphook; this must be set in the application CMSApp if an app_name is defined, otherwise the setup function will exit with a warning.
  • config_fields: Fields used when creating the ApphookConfigModel instance; use this attribute for non-translated fields.
  • config_translated_fields: Fields used when creating the ApphookConfigModel instance; use this attribute for translated fields (currently only django-parler is supported).
  • sites: List of site ids for which to create the pages; if set to True (the default value) pages will be created for all sites. A single apphook config is created for all the sites; instance is created only on first page creation.

Notes on testing

As this utility works by triggering setup function at import time, extra steps must be taken in the tests to unload the modules between the tests (this is only needed when testing the setup).

Example cleanup to be included in setUp method:

def setUp(self):
    super(SetupAppBaseTest, self).setUp()
    from cms.apphook_pool import apphook_pool

    delete = [
        'my_app',
        'my_app.cms_app',
    ]
    for module in delete:
        if module in sys.modules:
            del sys.modules[module]
    MyApphoolConfigModel.cmsapp = None
    apphook_pool.clear()

djangocms-apphook-setup's People

Contributors

pre-commit-ci[bot] avatar protoroto avatar yakky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

djangocms-apphook-setup's Issues

Monkey-patch typo that removes TitleIndex get_urls method permanently

In This commit you reimplemented base.py file that contains monkey-patching for TitleIndex. In the previous version, you always return original get_url method, BUT in a current version, you have tabbed returning of original method INSIDE if statement that checks if pages with apphook don't exist.
I'll provide a pull request for fixing that issue.
And also, please provide hard versions between djangocms_blog and djangocms-apphook-setup.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.