|
| 1 | +title: django.utils.translation activate code examples |
| 2 | +category: page |
| 3 | +slug: django-utils-translation-activate-examples |
| 4 | +sortorder: 500011494 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.utils.translation activate |
| 7 | +meta: Python example code for the activate function from the django.utils.translation module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +activate is a function within the django.utils.translation module of the Django project. |
| 11 | + |
| 12 | + |
| 13 | +## Example 1 from django-cms |
| 14 | +[django-cms](https://github.com/divio/django-cms) |
| 15 | +([project website](https://www.django-cms.org/en/)) is a Python-based |
| 16 | +content management system (CMS) [library](https://pypi.org/project/django-cms/) |
| 17 | +for use with Django web apps that is open sourced under the |
| 18 | +[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE) |
| 19 | +license. |
| 20 | + |
| 21 | +[**django-cms / cms / plugin_pool.py**](https://github.com/divio/django-cms/blob/develop/cms/./plugin_pool.py) |
| 22 | + |
| 23 | +```python |
| 24 | +# plugin_pool.py |
| 25 | +from operator import attrgetter |
| 26 | + |
| 27 | +from django.core.exceptions import ImproperlyConfigured |
| 28 | +from django.conf.urls import url, include |
| 29 | +from django.template.defaultfilters import slugify |
| 30 | +from django.utils.encoding import force_text |
| 31 | +from django.utils.functional import cached_property |
| 32 | +from django.utils.module_loading import autodiscover_modules |
| 33 | +~~from django.utils.translation import get_language, deactivate_all, activate |
| 34 | +from django.template import TemplateDoesNotExist, TemplateSyntaxError |
| 35 | + |
| 36 | +from six import string_types, text_type |
| 37 | + |
| 38 | +from cms.exceptions import PluginAlreadyRegistered, PluginNotRegistered |
| 39 | +from cms.plugin_base import CMSPluginBase |
| 40 | +from cms.utils.conf import get_cms_setting |
| 41 | +from cms.utils.helpers import normalize_name |
| 42 | + |
| 43 | + |
| 44 | +class PluginPool(object): |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + self.plugins = {} |
| 48 | + self.discovered = False |
| 49 | + |
| 50 | + def _clear_cached(self): |
| 51 | + if 'registered_plugins' in self.__dict__: |
| 52 | + del self.__dict__['registered_plugins'] |
| 53 | + |
| 54 | + if 'plugins_with_extra_menu' in self.__dict__: |
| 55 | + del self.__dict__['plugins_with_extra_menu'] |
| 56 | + |
| 57 | + if 'plugins_with_extra_placeholder_menu' in self.__dict__: |
| 58 | + |
| 59 | + |
| 60 | +## ... source file abbreviated to get to activate examples ... |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + def get_plugin(self, name): |
| 65 | + self.discover_plugins() |
| 66 | + return self.plugins[name] |
| 67 | + |
| 68 | + def get_patterns(self): |
| 69 | + self.discover_plugins() |
| 70 | + |
| 71 | + lang = get_language() |
| 72 | + deactivate_all() |
| 73 | + |
| 74 | + try: |
| 75 | + url_patterns = [] |
| 76 | + for plugin in self.registered_plugins: |
| 77 | + p = plugin() |
| 78 | + slug = slugify(force_text(normalize_name(p.__class__.__name__))) |
| 79 | + url_patterns += [ |
| 80 | + url(r'^plugin/%s/' % (slug,), include(p.plugin_urls)), |
| 81 | + ] |
| 82 | + finally: |
| 83 | +~~ activate(lang) |
| 84 | + |
| 85 | + return url_patterns |
| 86 | + |
| 87 | + def get_system_plugins(self): |
| 88 | + self.discover_plugins() |
| 89 | + return [plugin.__name__ for plugin in self.plugins.values() if plugin.system] |
| 90 | + |
| 91 | + @cached_property |
| 92 | + def registered_plugins(self): |
| 93 | + return self.get_all_plugins() |
| 94 | + |
| 95 | + @cached_property |
| 96 | + def plugins_with_extra_menu(self): |
| 97 | + plugin_classes = [cls for cls in self.registered_plugins |
| 98 | + if cls._has_extra_plugin_menu_items] |
| 99 | + return plugin_classes |
| 100 | + |
| 101 | + @cached_property |
| 102 | + def plugins_with_extra_placeholder_menu(self): |
| 103 | + plugin_classes = [cls for cls in self.registered_plugins |
| 104 | + if cls._has_extra_placeholder_menu_items] |
| 105 | + return plugin_classes |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +## ... source file continues with no further activate examples... |
| 111 | + |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +## Example 2 from django-sitetree |
| 116 | +[django-sitetree](https://github.com/idlesign/django-sitetree) |
| 117 | +([project documentation](https://django-sitetree.readthedocs.io/en/latest/) |
| 118 | +and |
| 119 | +[PyPI package information](https://pypi.org/project/django-sitetree/)) |
| 120 | +is a [Django](/django.html) extension that makes it easier for |
| 121 | +developers to add site trees, menus and breadcrumb navigation elements |
| 122 | +to their web applications. |
| 123 | + |
| 124 | +The django-sitetree project is provided as open source under the |
| 125 | +[BSD 3-Clause "New" or "Revised" License](https://github.com/idlesign/django-sitetree/blob/master/LICENSE). |
| 126 | + |
| 127 | +[**django-sitetree / sitetree / tests / test_templatetags.py**](https://github.com/idlesign/django-sitetree/blob/master/sitetree/tests/test_templatetags.py) |
| 128 | + |
| 129 | +```python |
| 130 | +# test_templatetags.py |
| 131 | +import pytest |
| 132 | +from django.template.base import TemplateSyntaxError |
| 133 | +~~from django.utils.translation import activate, deactivate_all |
| 134 | + |
| 135 | +from sitetree.exceptions import SiteTreeError |
| 136 | +from sitetree.settings import ALIAS_THIS_ANCESTOR_CHILDREN, ALIAS_THIS_CHILDREN, ALIAS_THIS_PARENT_SIBLINGS, \ |
| 137 | + ALIAS_THIS_SIBLINGS, ALIAS_TRUNK |
| 138 | + |
| 139 | + |
| 140 | +def test_items_hook(template_render_tag, template_context, common_tree): |
| 141 | + |
| 142 | + from sitetree.toolbox import register_items_hook |
| 143 | + |
| 144 | + with pytest.raises(SiteTreeError): |
| 145 | + register_items_hook(lambda: []) |
| 146 | + |
| 147 | + def my_processor(tree_items, tree_sender): |
| 148 | + for item in tree_items: |
| 149 | + item.hint = f'hooked_hint_{item.title}' |
| 150 | + return tree_items |
| 151 | + |
| 152 | + register_items_hook(my_processor) |
| 153 | + result = template_render_tag('sitetree', 'sitetree_tree from "mytree"', template_context()) |
| 154 | + |
| 155 | + assert 'hooked_hint_Darwin' in result |
| 156 | + assert 'hooked_hint_Australia' in result |
| 157 | + assert 'hooked_hint_China' in result |
| 158 | + |
| 159 | + |
| 160 | +## ... source file abbreviated to get to activate examples ... |
| 161 | + |
| 162 | + |
| 163 | + from sitetree.toolbox import register_i18n_trees |
| 164 | + |
| 165 | + build_tree( |
| 166 | + {'alias': 'i18tree'}, |
| 167 | + [{'title': 'My title', 'url': '/url_default/'}], |
| 168 | + ) |
| 169 | + build_tree( |
| 170 | + {'alias': 'i18tree_ru'}, |
| 171 | + [{'title': 'Заголовок', 'url': '/url_ru/'}], |
| 172 | + ) |
| 173 | + build_tree( |
| 174 | + {'alias': 'i18tree_pt-br'}, |
| 175 | + [{'title': 'Meu Título', 'url': '/url_pt-br/'}], |
| 176 | + ) |
| 177 | + build_tree( |
| 178 | + {'alias': 'i18tree_zh-hans'}, |
| 179 | + [{'title': '我蒂特', 'url': '/url_zh-hans/'}], |
| 180 | + ) |
| 181 | + register_i18n_trees(['i18tree']) |
| 182 | + |
| 183 | +~~ activate('en') |
| 184 | + result = template_render_tag('sitetree', 'sitetree_tree from "i18tree"', template_context()) |
| 185 | + |
| 186 | + assert '/url_default/' in result |
| 187 | + assert 'My title' in result |
| 188 | + |
| 189 | +~~ activate('ru') |
| 190 | + result = template_render_tag('sitetree', 'sitetree_tree from "i18tree"', template_context()) |
| 191 | + |
| 192 | + assert '/url_ru/' in result |
| 193 | + assert 'Заголовок' in result |
| 194 | + |
| 195 | +~~ activate('pt-br') |
| 196 | + result = template_render_tag('sitetree', 'sitetree_tree from "i18tree"', template_context()) |
| 197 | + |
| 198 | + assert '/url_pt-br/' in result |
| 199 | + assert 'Meu Título' in result |
| 200 | + |
| 201 | +~~ activate('zh-hans') |
| 202 | + result = template_render_tag('sitetree', 'sitetree_tree from "i18tree"', template_context()) |
| 203 | + |
| 204 | + assert '/url_zh-hans/' in result |
| 205 | + assert '我蒂特' in result |
| 206 | + |
| 207 | + deactivate_all() |
| 208 | + |
| 209 | + |
| 210 | +def test_restricted(user_create, template_render_tag, template_context, common_tree): |
| 211 | + context = template_context() |
| 212 | + result = template_render_tag('sitetree', 'sitetree_tree from "mytree"', context) |
| 213 | + |
| 214 | + assert '"/contacts/australia/darwin/"' in result |
| 215 | + assert '"/contacts/australia/alice/"' not in result |
| 216 | + |
| 217 | + context = template_context(user=user_create()) |
| 218 | + result = template_render_tag('sitetree', 'sitetree_tree from "mytree"', context) |
| 219 | + |
| 220 | + assert '"/contacts/australia/darwin/"' not in result |
| 221 | + assert '"/contacts/australia/alice/"' in result |
| 222 | + |
| 223 | + |
| 224 | +def test_permissions(user_create, build_tree, template_render_tag, template_context): |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +## ... source file continues with no further activate examples... |
| 229 | + |
| 230 | +``` |
| 231 | + |
0 commit comments