forked from django-cms/django-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_bases.py
54 lines (44 loc) · 1.59 KB
/
menu_bases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db.models import Q
from django.core.exceptions import ValidationError
from cms.apphook_pool import apphook_pool
from cms.models import Page
from menus.base import Menu
class CMSAttachMenu(Menu):
cms_enabled = True
instance = None
name = None
def __init__(self, *args, **kwargs):
super(CMSAttachMenu, self).__init__(*args, **kwargs)
if self.cms_enabled and not self.name:
raise ValidationError(
"the menu %s is a CMSAttachMenu but has no name defined!" %
self.__class__.__name__)
@classmethod
def get_apphooks(cls):
"""
Returns a list of apphooks to which this CMSAttachMenu is attached.
Calling this does NOT produce DB queries.
"""
apps = []
for key, _ in apphook_pool.get_apphooks():
app = apphook_pool.get_apphook(key)
if cls in app.get_menus():
apps.append(app)
return apps
@classmethod
def get_instances(cls):
"""
Return a list (queryset, really) of all CMS Page objects (in this case)
that are currently using this CMSAttachMenu either directly as a
navigation_extender, or, as part of an apphook.
Calling this DOES perform a DB query.
"""
parent_apps = []
for app in cls.get_apphooks():
parent_apps.append(app.__class__.__name__)
return Page.objects.filter(
Q(application_urls__in=parent_apps)
| Q(navigation_extenders=cls.__name__)
)