/
query.py
95 lines (80 loc) · 3.24 KB
/
query.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from datetime import datetime
from django.db.models import Q
from django.contrib.sites.models import Site
from publisher.query import PublisherQuerySet
from django.conf import settings
from cms.exceptions import NoHomeFound
#from cms.utils.urlutils import levelize_path
class PageQuerySet(PublisherQuerySet):
def on_site(self, site=None):
if not site:
site = Site.objects.get_current()
return self.filter(site=site)
def root(self):
"""
Return a queryset with pages that don't have parents, a.k.a. root. For
current site - used in frontend
"""
return self.on_site().filter(parent__isnull=True)
def all_root(self):
"""
Return a queryset with pages that don't have parents, a.k.a. root. For
all sites - used in frontend
"""
return self.filter(parent__isnull=True)
def valid_targets(self, page_id, request, perms, page=None):
"""
Give valid targets to move a page into the tree
"""
if page is None:
page = self.get(pk=page_id)
exclude_list = []
if page:
exclude_list.append(page.id)
for p in page.get_descendants():
exclude_list.append(p.id)
if perms != "All":
return self.filter(id__in=perms).exclude(id__in=exclude_list)
else:
return self.exclude(id__in=exclude_list)
def published(self, site=None):
pub = self.on_site().filter(published=True)
if settings.CMS_SHOW_START_DATE:
pub = pub.filter(
Q(publication_date__lt=datetime.now()) |
Q(publication_date__isnull=True)
)
if settings.CMS_SHOW_END_DATE:
pub = pub.filter(
Q(publication_end_date__gte=datetime.now()) |
Q(publication_end_date__isnull=True)
)
return pub
def expired(self):
return self.on_site().filter(
publication_end_date__lte=datetime.now())
# - seems this is not used anymore...
# def get_pages_with_application(self, path, language):
# """Returns all pages containing application for current path, or
# any parrent. Returned list is sorted by path length, longer path first.
# """
# paths = levelize_path(path)
# q = Q()
# for path in paths:
# # build q for all the paths
# q |= Q(title_set__path=path, title_set__language=language)
# app_pages = self.published().filter(q & Q(title_set__application_urls__gt='')).distinct()
# # add proper ordering
# app_pages.query.order_by.extend(('LENGTH(`cms_title`.`path`) DESC',))
# return app_pages
def get_all_pages_with_application(self):
"""Returns all pages containing applications for all sites.
Doesn't cares about the application language.
"""
return self.published().filter(title_set__application_urls__gt='').distinct()
def get_home(self, site=None):
try:
home = self.published(site).all_root().order_by("tree_id")[0]
except IndexError:
raise NoHomeFound('No Root page found. Publish at least one page!')
return home