forked from django-cms/django-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
115 lines (97 loc) · 3.69 KB
/
utils.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
from __future__ import with_statement
from cms.utils.i18n import force_language, hide_untranslated
from django.conf import settings
import warnings
from cms.models.titlemodels import Title
def mark_descendants(nodes):
for node in nodes:
node.descendant = True
mark_descendants(node.children)
def cut_levels(nodes, level):
"""
For cutting the nav_extender levels if you have a from_level in the navigation.
"""
result = []
if nodes:
if nodes[0].level == level:
return nodes
for node in nodes:
result += cut_levels(node.children, level)
return result
def find_selected(nodes):
"""
Finds a selected nav_extender node
"""
for node in nodes:
if hasattr(node, "selected"):
return node
elif hasattr(node, "ancestor"):
result = find_selected(node.children)
if result:
return result
def set_language_changer(request, func):
"""
Sets a language chooser function that accepts one parameter: language
The function should return a url in the supplied language
normally you would want to give it the get_absolute_url function with an optional language parameter
example:
def get_absolute_url(self, language=None):
reverse('product_view', args=[self.get_slug(language=language)])
Use this function in your nav extender views that have i18n slugs.
"""
request._language_changer = func
def language_changer_decorator(language_changer):
"""
A decorator wrapper for set_language_changer.
from menus.utils import language_changer_decorator
@language_changer_decorator(function_get_language_changer_url)
def my_view_function(request, somearg):
pass
"""
def _decorator(func):
def _wrapped(request, *args, **kwargs):
set_language_changer(request, language_changer)
return func(request, *args, **kwargs)
_wrapped.__name__ = func.__name__
_wrapped.__doc__ = func.__doc__
return _wrapped
return _decorator
class DefaultLanguageChanger(object):
def __init__(self, request):
self.request = request
self._app_path = None
@property
def app_path(self):
if self._app_path is None:
page_path = self.get_page_path(self.request.LANGUAGE_CODE)
if page_path:
self._app_path = self.request.path[len(page_path):]
else:
self._app_path = self.request.path
return self._app_path
def __call__(self, lang):
return '%s%s' % (self.get_page_path(lang), self.app_path)
def get_page_path(self, lang):
page = getattr(self.request, 'current_page', None)
if page:
with force_language(lang):
try:
return page.get_absolute_url(language=lang, fallback=False)
except Title.DoesNotExist:
if hide_untranslated(lang):
return '/%s/' % lang
else:
return page.get_absolute_url(language=lang, fallback=True)
else:
return '/%s/' % lang
def simple_language_changer(func):
warnings.warn("simple_language_changer is deprecated and will be removed in "
"2.5! This is the default behaviour now for non CMS managed views and is no longer needed.",
DeprecationWarning)
def _wrapped(request, *args, **kwargs):
set_language_changer(request, DefaultLanguageChanger(request))
return func(request, *args, **kwargs)
_wrapped.__name__ = func.__name__
_wrapped.__doc__ = func.__doc__
return _wrapped