Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New i18n settings #2

Merged
merged 5 commits into from Oct 3, 2012
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions cms/admin/forms.py
Expand Up @@ -3,6 +3,7 @@
from cms.forms.widgets import UserSelectAdminWidget
from cms.models import (Page, PagePermission, PageUser, ACCESS_PAGE,
PageUserGroup)
from cms.utils.i18n import get_language_tuple, get_language_list
from cms.utils.mail import mail_page_user_change
from cms.utils.page import is_valid_page_slug
from cms.utils.page_resolver import get_page_from_path, is_valid_url
Expand Down Expand Up @@ -60,7 +61,7 @@ class PageAddForm(forms.ModelForm):
help_text=_('The default title'))
slug = forms.CharField(label=_("Slug"), widget=forms.TextInput(),
help_text=_('The part of the title that is used in the URL'))
language = forms.ChoiceField(label=_("Language"), choices=settings.CMS_LANGUAGES,
language = forms.ChoiceField(label=_("Language"), choices=get_language_tuple(),
help_text=_('The current language of the content fields.'))

class Meta:
Expand All @@ -74,13 +75,7 @@ def __init__(self, *args, **kwargs):
if not self.fields['site'].initial:
self.fields['site'].initial = Site.objects.get_current().pk
site_id = self.fields['site'].initial
languages = []
language_mappings = dict(settings.LANGUAGES)
if site_id in settings.CMS_SITE_LANGUAGES:
for lang in settings.CMS_SITE_LANGUAGES[site_id]:
languages.append((lang, language_mappings.get(lang, lang)))
else:
languages = settings.CMS_LANGUAGES
languages = get_language_tuple(site_id)
self.fields['language'].choices = languages
if not self.fields['language'].initial:
self.fields['language'].initial = get_language()
Expand Down Expand Up @@ -142,7 +137,7 @@ def clean_slug(self):

def clean_language(self):
language = self.cleaned_data['language']
if not language in dict(settings.CMS_LANGUAGES).keys():
if not language in get_language_list():
raise ValidationError("Given language does not match language settings.")
return language

Expand Down
24 changes: 7 additions & 17 deletions cms/admin/pageadmin.py
Expand Up @@ -17,6 +17,7 @@
from cms.utils import (copy_plugins, helpers, moderator, permissions, plugins,
get_template_from_request, get_language_from_request,
placeholder as placeholder_utils, admin as admin_utils, cms_static_url)
from cms.utils.i18n import get_language_dict, get_language_list, get_language_tuple, get_language_object
from cms.utils.page_resolver import is_valid_url
from cms.utils.admin import jsonify_request
from cms.utils.permissions import has_plugin_permission
Expand Down Expand Up @@ -441,13 +442,13 @@ def get_form(self, request, obj=None, **kwargs):
installed_plugins = plugin_pool.get_all_plugins(placeholder_name, obj)
plugin_list = CMSPlugin.objects.filter(language=language, placeholder=placeholder, parent=None).order_by('position')
other_plugins = CMSPlugin.objects.filter(placeholder=placeholder, parent=None).exclude(language=language)
dict_cms_languages = dict(settings.CMS_LANGUAGES)
dict_cms_languages = get_language_dict()
for plugin in other_plugins:
if (not plugin.language in copy_languages) and (plugin.language in dict_cms_languages):
copy_languages[plugin.language] = dict_cms_languages[plugin.language]

language = get_language_from_request(request, obj)
if copy_languages and len(settings.CMS_LANGUAGES) > 1:
if copy_languages and len(get_language_list()) > 1:
show_copy = True
widget = PluginEditor(attrs={
'installed': installed_plugins,
Expand Down Expand Up @@ -565,14 +566,7 @@ def _get_site_languages(self, obj):
site_id = None
if obj:
site_id = obj.site_id
languages = []
if site_id and site_id in settings.CMS_SITE_LANGUAGES:
for lang in settings.CMS_SITE_LANGUAGES[site_id]:
lang_label = dict(settings.CMS_LANGUAGES).get(lang, dict(settings.LANGUAGES).get(lang, lang))
languages.append((lang, lang_label))
else:
languages = settings.CMS_LANGUAGES
return languages
return get_language_tuple(site_id)

def update_language_tab_context(self, request, obj, context=None):
if not context:
Expand Down Expand Up @@ -675,11 +669,7 @@ def changelist_view(self, request, extra_context=None):
site_id = int(site_id)

# languages
languages = []
if site_id and site_id in settings.CMS_SITE_LANGUAGES:
languages = settings.CMS_SITE_LANGUAGES[site_id]
else:
languages = [lang[0] for lang in settings.CMS_LANGUAGES]
languages = get_language_list(site_id)

# parse the cookie that saves which page trees have
# been opened already and extracts the page ID
Expand Down Expand Up @@ -985,7 +975,7 @@ def delete_translation(self, request, object_id, extra_context=None):
raise PermissionDenied

message = _('Title and plugins with language %(language)s was deleted') % {
'language': [name for code, name in settings.CMS_LANGUAGES if code == language][0]
'language': get_language_object(language)['name']
}
self.log_change(request, titleobj, message)
self.message_user(request, message)
Expand Down Expand Up @@ -1188,7 +1178,7 @@ def copy_plugins(self, request):

if not page.has_change_permission(request):
return HttpResponseForbidden(ugettext("You do not have permission to change this page"))
if not language or not language in [ lang[0] for lang in settings.CMS_LANGUAGES ]:
if not language or not language in get_language_list():
return HttpResponseBadRequest(ugettext("Language must be set to a supported language!"))
if language == copy_from:
return HttpResponseBadRequest(ugettext("Language must be different than the copied language!"))
Expand Down
5 changes: 3 additions & 2 deletions cms/api.py
Expand Up @@ -7,6 +7,7 @@
calling these methods!
"""
import datetime
from cms.utils.i18n import get_language_list

from django.conf import settings
from django.contrib.auth.models import User
Expand Down Expand Up @@ -125,7 +126,7 @@ def create_page(title, template, language, menu_title=None, slug=None,
assert template in [tpl[0] for tpl in settings.CMS_TEMPLATES]

# validate language:
assert language in [lang[0] for lang in settings.CMS_LANGUAGES]
assert language in get_language_list()

# set default slug:
if not slug:
Expand Down Expand Up @@ -220,7 +221,7 @@ def create_title(language, title, page, menu_title=None, slug=None,
See docs/extending_cms/api_reference.rst for more info
"""
# validate language:
assert language in [lang[0] for lang in settings.CMS_LANGUAGES]
assert language in get_language_list()

# validate page
assert isinstance(page, Page)
Expand Down
4 changes: 2 additions & 2 deletions cms/appresolver.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
from cms.apphook_pool import apphook_pool
from cms.utils.i18n import force_language
from cms.utils.i18n import force_language, get_language_list
from cms.utils.moderator import get_page_queryset

from django.conf import settings
Expand Down Expand Up @@ -31,7 +31,7 @@ def applications_page_check(request, current_page=None, path=None):
# This removes the non-CMS part of the URL.
path = request.path.replace(reverse('pages-root'), '', 1)
# check if application resolver can resolve this
for lang, lang_name in settings.CMS_LANGUAGES:
for lang in get_language_list():
if path.startswith(lang+"/"):
path = path[len(lang+"/"):]
for resolver in APP_RESOLVERS:
Expand Down
6 changes: 4 additions & 2 deletions cms/conf/__init__.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from patch import post_patch, post_patch_check
from patch import post_patch, post_patch_check, pre_patch
import warnings


Expand All @@ -9,7 +9,7 @@ def patch_settings():
"""Merge settings with global cms settings, so all required attributes
will exist. Never override, just append non existing settings.

Also check for setting inconstistence if settings.DEBUG
Also check for setting inconsistencies if settings.DEBUG
"""
if patch_settings.ALREADY_PATCHED:
return
Expand All @@ -24,6 +24,8 @@ def patch_settings():
from cms.conf import global_settings
# patch settings

pre_patch()

# merge with global cms settings
for attr in dir(global_settings):
if attr == attr.upper() and not hasattr(settings, attr):
Expand Down
53 changes: 32 additions & 21 deletions cms/conf/global_settings.py
Expand Up @@ -70,34 +70,45 @@
# Wheter the cms has a softroot functionionality
CMS_SOFTROOT = False

#Hide untranslated Pages
CMS_HIDE_UNTRANSLATED = True

#Fall back to another language if the requested page isn't available in the preferred language
CMS_LANGUAGE_FALLBACK = True

#Configuration on how to order the fallbacks for languages.
# example: {'de': ['en', 'fr'],
# 'en': ['de'],
# }
CMS_LANGUAGE_CONF = {}
# Defines which languages should be offered and what are the defaults
# example:
# CMS_LANGUAGES = {
# 1: [
# {
# 'code': 'en',
# 'name': _('English'),
# 'fallbacks': ['de', 'fr'],
# 'public': True,
# 'hide_untranslated': True,
# 'redirect_on_fallback':False,
# },
# {
# 'code': 'de',
# 'name': _('Deutsch'),
# 'fallbacks': ['en', 'fr'],
# 'public': True,
# },
# {
# 'code': 'fr',
# 'public': False,
# }
# ],
# 'default': {
# 'fallbacks': ['en', 'de', 'fr'],
# 'redirect_on_fallback':True,
# 'public': False,
# 'hide_untranslated': False,
# }
#}

#CMS_LANGUAGES = {}

# Defines which languages should be offered.
CMS_LANGUAGES = settings.LANGUAGES

# If you have different sites with different languages you can configure them here
# and you will only be able to edit the languages that are actually on the site.
# example: {1:['en','de'],
# 2:['en','fr'],
# 3:['en'],}
CMS_SITE_LANGUAGES = {}

CMS_SITE_CHOICES_CACHE_KEY = 'CMS:site_choices'
CMS_PAGE_CHOICES_CACHE_KEY = 'CMS:page_choices'

# Languages that are visible in the frontend (Language Chooser)
CMS_FRONTEND_LANGUAGES = [x[0] for x in CMS_LANGUAGES]


# Path for CMS media (uses <MEDIA_ROOT>/cms by default)
CMS_MEDIA_PATH = 'cms/'
Expand Down
39 changes: 38 additions & 1 deletion cms/conf/patch.py
Expand Up @@ -4,9 +4,16 @@
from django.utils.translation import ugettext_lazy as _
from sekizai.helpers import validate_template

def pre_patch():
"""Patch settings for dynamic defaults"""
if not getattr(settings, 'CMS_LANGUAGES', False):
settings.CMS_LANGUAGES = {settings.SITE_ID:[]}
for code, name in settings.LANGUAGES:
lang = {'code':code, 'name':_(name)}
settings.CMS_LANGUAGES[settings.SITE_ID].append(lang)

def post_patch():
"""Patch settings after global are adde
"""Patch settings after global are added
"""
if settings.CMS_TEMPLATE_INHERITANCE:
# Append the magic inheritance template
Expand Down Expand Up @@ -46,3 +53,33 @@ def post_patch_check():
"I can't find the namespaces in %r."
% template[0]
)
VALID_LANG_PROPS = ['code', 'name', 'fallbacks', 'hide_untranslated', 'redirect_on_fallback', 'public']
try:
for site in settings.CMS_LANGUAGES.keys():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.keys() is useless here.

try:
int(site)
except ValueError:
if not site =="default":
raise ImproperlyConfigured("CMS_LANGUAGES can only be filled with integers (site ids) and 'default' for\n"
" default values. %s is not a valid key." % site)
for lang in settings.CMS_LANGUAGES[site]:
if site == "default":
if lang not in VALID_LANG_PROPS:
raise ImproperlyConfigured("CMS_LANGUAGES has an invalid property on the site %(site)s and language %(language)s: %(property)s" % {'site':site, 'language':lang['code'], 'property':key})
continue
if not "code" in lang.keys():
raise ImproperlyConfigured("CMS_LANGUAGES has language without a 'code' property")
if not 'name' in lang.keys():
raise ImproperlyConfigured("CMS_LANGUAGES has a language without a 'name' property")
for key in lang.keys():
if key not in VALID_LANG_PROPS:
raise ImproperlyConfigured("CMS_LANGUAGES has an invalid property on the site %(site)s and language %(language)s: %(property)s" % {'site':site, 'language':lang['code'], 'property':key})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pep8? this is a little over 80 characters....

except:
raise ImproperlyConfigured("CMS_LANGUAGES has changed. Please refer to the docs.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should really be backwards compatible for at least one release with warnings instead of exceptions.








Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's up with the empty lines here?

6 changes: 3 additions & 3 deletions cms/menu.py
Expand Up @@ -6,7 +6,7 @@
from cms.models.permissionmodels import PagePermission, GlobalPagePermission
from cms.models.titlemodels import Title
from cms.utils import get_language_from_request
from cms.utils.i18n import get_fallback_languages
from cms.utils.i18n import get_fallback_languages, hide_untranslated
from cms.utils.moderator import get_page_queryset, get_title_queryset
from cms.utils.plugins import current_site
from menus.base import Menu, NavigationNode, Modifier
Expand Down Expand Up @@ -229,7 +229,7 @@ def get_nodes(self, request):
'site':site,
}

if settings.CMS_HIDE_UNTRANSLATED:
if hide_untranslated(lang, site.pk):
filters['title_set__language'] = lang

pages = page_queryset.published().filter(**filters).order_by("tree_id", "lft")
Expand Down Expand Up @@ -272,7 +272,7 @@ def get_nodes(self, request):
nodes.append(page_to_node(page, home, home_cut))
ids.remove(page.pk)

if ids: # get fallback languages
if ids and not hide_untranslated(lang): # get fallback languages if allowed
fallbacks = get_fallback_languages(lang)
for lang in fallbacks:
titles = list(get_title_queryset(request).filter(page__in=ids, language=lang))
Expand Down
2 changes: 2 additions & 0 deletions cms/models/pagemodel.py
Expand Up @@ -530,6 +530,8 @@ def get_languages(self):
self.all_languages = Title.objects.filter(page=self).values_list("language", flat=True).distinct()
self.all_languages = list(self.all_languages)
self.all_languages.sort()
for x in xrange(len(self.all_languages)):
self.all_languages[x] = str(self.all_languages[x])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wat? self.all_languages = map(str, self.all_languages).

return self.all_languages

def get_cached_ancestors(self, ascending=True):
Expand Down
2 changes: 1 addition & 1 deletion cms/plugins/inherit/cms_plugins.py
Expand Up @@ -36,7 +36,7 @@ def render(self, context, instance, placeholder):
page = instance.from_page
else:
page = instance.page
if not instance.page.publisher_is_draft and page.publisher_is_draft:
if settings.CMS_MODERATOR and not instance.page.publisher_is_draft and page.publisher_is_draft:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look i18n related

page = page.publisher_public

plugins = get_cmsplugin_queryset(request).filter(
Expand Down
4 changes: 2 additions & 2 deletions cms/plugins/inherit/models.py
@@ -1,11 +1,11 @@
from cms.utils.i18n import get_language_tuple
from django.db import models
from django.utils.translation import ugettext_lazy as _
from cms.models import CMSPlugin, Page
from django.conf import settings

class InheritPagePlaceholder(CMSPlugin):
"""
Provides the ability to inherit plugins for a certain placeholder from an associated "parent" page instance
"""
from_page = models.ForeignKey(Page, null=True, blank=True, help_text=_("Choose a page to include its plugins into this placeholder, empty will choose current page"))
from_language = models.CharField(_("language"), max_length=5, choices=settings.CMS_LANGUAGES, blank=True, null=True, help_text=_("Optional: the language of the plugins you want"))
from_language = models.CharField(_("language"), max_length=5, choices=get_language_tuple(), blank=True, null=True, help_text=_("Optional: the language of the plugins you want"))
13 changes: 11 additions & 2 deletions cms/plugins/utils.py
Expand Up @@ -4,6 +4,7 @@

from cms.plugin_pool import plugin_pool
from cms.utils import get_language_from_request
from cms.utils.i18n import get_redirect_on_fallback, get_fallback_languages
from cms.utils.moderator import get_cmsplugin_queryset

def get_plugins(request, placeholder, lang=None):
Expand All @@ -24,9 +25,17 @@ def assign_plugins(request, placeholders, lang=None):
if not placeholders:
return
lang = lang or get_language_from_request(request)

request_lang = lang
if hasattr(request, "current_page") and request.current_page is not None:
languages = request.current_page.get_languages()
if not lang in languages and not get_redirect_on_fallback(lang):
fallbacks = get_fallback_languages(lang)
for fallback in fallbacks:
if fallback in languages:
request_lang = fallback
break
# get all plugins for the given placeholders
qs = get_cmsplugin_queryset(request).filter(placeholder__in=placeholders, language=lang, parent__isnull=True).order_by('placeholder', 'position')
qs = get_cmsplugin_queryset(request).filter(placeholder__in=placeholders, language=request_lang, parent__isnull=True).order_by('placeholder', 'position')
plugin_list = downcast_plugins(qs)

# split the plugins up by placeholder
Expand Down