Skip to content

Commit

Permalink
Add recursion to language updates (see #1). Change metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
disko committed May 6, 2013
1 parent cd4709a commit 462c782
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The package contains a single content type ``LanguageSection`` which is
supposed to be the container of all content in a specific language. All
content that is created in (or moved into) such a subtree will be automatically
tagged with the language of the section. This is done by subscribing to
Kotti's ``ObjectInsert``and ``ObjectUpdate`` events.
Kotti's ``ObjectInsert`` and ``ObjectUpdate`` events.

The package also extends Kotti's ``TemplateAPI`` with an additional
``language_root`` attribute that can be used as the navigation root for the
Expand Down
45 changes: 42 additions & 3 deletions kotti_multilingual/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
:author: Andreas Kaiser (disko)
"""

from logging import getLogger

from kotti.events import ObjectInsert
from kotti.events import ObjectUpdate
from kotti.events import subscribe
from pyramid.events import ContextFound
from pyramid.events import subscriber
from pyramid.httpexceptions import HTTPForbidden


log = getLogger(__name__)


@subscriber(ContextFound)
Expand All @@ -28,14 +34,47 @@ def set_context_locale(event):
request._LOCALE_ = context.language


def _set_language_to_same_as_parent(context, recurse=True):
"""
Set context's language to that of context's parent. No checks are
performed at all.
:param context: Node of which the language is to be changed.
:type context: :class:`kotti.resources.Content`
:param recurse: If ``True`` also set the language of all children. This is
required e.g. for cut and paste operations.
:type recurse: bool
"""

if context.type == 'language_section':
# LanguageSection instances must not be within another LanguageSection
if context.parent.language is not None:
raise HTTPForbidden
else:
# LanguageSection instances' language must not be set to None.
context.language = context.parent.language

if recurse:
for c in context.children:
_set_language_to_same_as_parent(c)


@subscribe(ObjectInsert)
@subscribe(ObjectUpdate)
def set_language_of_parent(event):
def update_language(event):
""" Update the language (if necessary).
:param event: Event that needs attention w.r.t. the related object's
language.
:type event: :class:`kotti.events.ObjectEvent`
"""

context = event.object

if hasattr(context, 'language') and hasattr(context, 'parent') and \
hasattr(context.parent, 'language') and hasattr(context, 'type') \
and context.type != 'language_section':
and (context.type != 'language_section') and \
(context.language != context.parent.language):

context.language = context.parent.language
_set_language_to_same_as_parent(context)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
setup(
name=project,
version=version,
description="Language Section content type for Kotti",
description="Multilangual sites with Kotti",
long_description=README + '\n\n' + CHANGES,
classifiers=[
"Environment :: Web Environment",
Expand All @@ -35,7 +35,7 @@
"Topic :: Internet :: WWW/HTTP",
"Topic :: Software Development :: User Interfaces",
],
keywords='kotti theme',
keywords='kotti add-on',
author='Andreas Kaiser',
author_email='disko@binary-punks.com',
url='https://github.com/disko/kotti_multilingual',
Expand Down

0 comments on commit 462c782

Please sign in to comment.