• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nephila / djangocms-apphook-setup / 6310150077

26 Sep 2023 08:34AM UTC coverage: 91.398%. Remained the same
6310150077

push

github

web-flow
[pre-commit.ci] pre-commit autoupdate (#31)

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.0.290 → v0.0.291](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.290...v0.0.291)
- [github.com/asottile/pyupgrade: v3.11.0 → v3.13.0](https://github.com/asottile/pyupgrade/compare/v3.11.0...v3.13.0)
- [github.com/adamchainz/django-upgrade: 1.14.1 → 1.15.0](https://github.com/adamchainz/django-upgrade/compare/1.14.1...1.15.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

32 of 36 branches covered (0.0%)

85 of 93 relevant lines covered (91.4%)

10.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.11
/djangocms_apphook_setup/base.py
1
import warnings
12✔
2

3
from django.contrib.sites.models import Site
12✔
4

5

6
class AutoCMSAppMixin:
12✔
7
    auto_setup = {
12✔
8
        "enabled": True,
9
        "home title": None,
10
        "page title": None,
11
        "namespace": None,
12
        "config_fields": {},
13
        "config_translated_fields": {},
14
        "sites": True,
15
    }
16

17
    @classmethod
12✔
18
    def _create_page(
12✔
19
        cls, page, lang, auto_title, cms_app=None, parent=None, namespace=None, site=None, set_home=False
20
    ):
21
        """
22
        Create a single page or titles
23

24
        :param page: Page instance
25
        :param lang: language code
26
        :param auto_title: title text for the newly created title
27
        :param cms_app: Apphook Class to be attached to the page
28
        :param parent: parent page (None when creating the home page)
29
        :param namespace: application instance name (as provided to the ApphookConfig)
30
        :param set_home: mark as home page (on django CMS 3.5 only)
31

32
        :return: draft copy of the created page
33
        """
34
        from cms.api import create_page, create_title
12✔
35
        from cms.utils.conf import get_templates
12✔
36

37
        default_template = get_templates()[0][0]
12✔
38
        if page is None:
12✔
39
            page = create_page(
12✔
40
                auto_title,
41
                language=lang,
42
                parent=parent,
43
                site=site,
44
                template=default_template,
45
                in_navigation=True,
46
                published=True,
47
            )
48
            page.application_urls = cms_app
12✔
49
            page.application_namespace = namespace
12✔
50
            page.save()
12✔
51
            page.publish(lang)
12✔
52
        elif lang not in page.get_languages():
12✔
53
            create_title(language=lang, title=auto_title, page=page)
12✔
54
            page.publish(lang)
12✔
55
        if set_home:
12✔
56
            page.set_as_homepage()
12✔
57
        return page.get_draft_object()
12✔
58

59
    @classmethod
12✔
60
    def _create_config(cls):
12✔
61
        """
62
        Creates an ApphookConfig instance
63

64
        ``AutoCMSAppMixin.auto_setup['config_fields']`` is used to fill in the data
65
        of the instance.
66

67
        :return: ApphookConfig instance
68
        """
69
        return cls.app_config.objects.create(namespace=cls.auto_setup["namespace"], **cls.auto_setup["config_fields"])
12✔
70

71
    @classmethod
12✔
72
    def _create_config_translation(cls, config, lang):
12✔
73
        """
74
        Creates a translation for the given ApphookConfig
75

76
        Only django-parler kind of models are currently supported.
77

78
        ``AutoCMSAppMixin.auto_setup['config_translated_fields']`` is used to fill in the data
79
        of the instance for all the languages.
80

81
        :param config: ApphookConfig instance
82
        :param lang: language code for the language to create
83
        """
84
        config.set_current_language(lang, initialize=True)
12✔
85
        for field, data in cls.auto_setup["config_translated_fields"].items():
12✔
86
            setattr(config, field, data)
12✔
87
        config.save_translations()
12✔
88

89
    @classmethod
12✔
90
    def _setup_pages(cls, config):
12✔
91
        """
92
        Create the page structure.
93

94
        It created a home page (if not exists) and a sub-page, and attach the Apphook to the
95
        sub-page.
96
        Pages titles are provided by ``AutoCMSAppMixin.auto_setup``
97

98
        :param setup_config: boolean to control whether creating the ApphookConfig instance
99
        """
100
        from cms.exceptions import NoHomeFound
12✔
101
        from cms.models import Page
12✔
102
        from cms.utils import get_language_list
12✔
103
        from django.conf import settings
12✔
104
        from django.utils.translation import override
12✔
105

106
        app_page = None
12✔
107
        get_url = False
12✔
108
        if getattr(settings, "ALDRYN_SEARCH_CMS_PAGE", False):
12!
109
            from aldryn_search.search_indexes import TitleIndex
×
110

111
            def fake_url(self, obj):
×
112
                return ""
×
113

114
            get_url = TitleIndex.get_url
×
115
            TitleIndex.get_url = fake_url
×
116
        site = Site.objects.get_current()
12✔
117
        auto_sites = cls.auto_setup.get("sites", True)
12✔
118
        if auto_sites is True or site.pk in auto_sites:
12✔
119
            if getattr(cls, "app_config", False):
12✔
120
                configs = cls.app_config.objects.all()
12✔
121
                if not configs.exists():
12✔
122
                    config = cls._create_config()
12✔
123
                else:
124
                    config = configs.first()
12✔
125

126
            langs = get_language_list(site.pk)
12✔
127
            if not Page.objects.on_site(site.pk).filter(application_urls=cls.__name__).exists():
12!
128
                for lang in langs:
12✔
129
                    with override(lang):
12✔
130
                        if config:
12✔
131
                            if cls.auto_setup["config_translated_fields"]:
12✔
132
                                cls._create_config_translation(config, lang)
12✔
133
                            namespace = config.namespace
12✔
134
                        elif cls.app_name:
12✔
135
                            namespace = cls.app_name
12✔
136
                        else:
137
                            namespace = None
12✔
138
                        try:
12✔
139
                            home = Page.objects.get_home(site.pk).get_draft_object()
12✔
140
                        except NoHomeFound:
12✔
141
                            home = None
12✔
142
                        set_home = hasattr(Page, "set_as_homepage")
12✔
143
                        home = cls._create_page(home, lang, cls.auto_setup["home title"], site=site, set_home=set_home)
12✔
144
                        app_page = cls._create_page(
12✔
145
                            app_page, lang, cls.auto_setup["page title"], cls.__name__, home, namespace, site=site
146
                        )
147
        if get_url:
12!
148
            TitleIndex.get_url = get_url
×
149

150
    @classmethod
12✔
151
    def setup(cls):
12✔
152
        """
153
        Main method to auto setup Apphook
154

155
        It must be called after the Apphook registration::
156

157
            apphook_pool.register(MyApp)
158
            MyApp.setup()
159
        """
160
        try:
12✔
161
            if cls.auto_setup and cls.auto_setup.get("enabled", False):
12!
162
                if not cls.auto_setup.get("home title", False):
12✔
163
                    warnings.warn('"home title" is not set in {}.auto_setup attribute'.format(cls))
12✔
164
                    return
12✔
165
                if not cls.auto_setup.get("page title", False):
12✔
166
                    warnings.warn('"page title" is not set in {}.auto_setup attribute'.format(cls))
12✔
167
                    return
12✔
168
                if cls.app_name and not cls.auto_setup.get("namespace", False):
12✔
169
                    warnings.warn('"page title" is not set in {}.auto_setup attribute'.format(cls))
12✔
170
                    return
12✔
171
                config = None
12✔
172
                cls._setup_pages(config)
12✔
173
        except Exception:
×
174
            # Ignore any error during setup. Worst case: pages are not created, but the instance
175
            # won't break
176
            pass
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc