Exemple #1
0
    def __init__(self, *args, **kwargs):
        super(LanguageForm, self).__init__(*args, **kwargs)

        # Sort language choices according their display name
        sorted_choices = sorted(self.fields['code'].choices, key=itemgetter(1))
        self.fields['code'].choices = sorted_choices

        # Remove is_default when a default is set.
        default_language = Language.objects.default()
        if default_language and (get_wagtailtrans_setting('LANGUAGES_PER_SITE')
                                 or get_wagtailtrans_setting('SYNC_TREE')):
            del self.fields['is_default']
Exemple #2
0
def synchronize_trees(sender, instance, **kwargs):
    """synchronize the translation trees when
    a TranslatablePage is created.

    :param sender: Sender model
    :param instance: TranslatablePage instance
    :param kwargs: kwargs e.g. created

    """
    try:
        site = instance.get_site()
    except ObjectDoesNotExist:
        return

    if get_wagtailtrans_setting('LANGUAGES_PER_SITE'):
        site_default = site.sitelanguages.default_language
        is_default_language = instance.language == site_default
        other_languages = site.sitelanguages.other_languages.all()
    else:
        is_default_language = instance.language.is_default
        other_languages = Language.objects.filter(is_default=False)

    if (not kwargs.get('created') or not getattr(instance, 'language', False)
            or not is_default_language):
        return

    for lang in other_languages:
        instance.create_translation(language=lang, copy_fields=True)
Exemple #3
0
def get_canonical_pages_for_delete(page):
    """Get the translations made for this page

    :param page: Page instance
    :return: queryset or False
    """
    page = page.specific
    if (get_wagtailtrans_setting('SYNC_TREE')
            and getattr(page, 'language', False) and not page.canonical_page):
        return TranslatablePage.objects.filter(canonical_page=page)
    return False
Exemple #4
0
def register_signal_handlers():
    """Registers signal handlers.

    To create a signal for TranslatablePage we have to use wagtails
    get_page_model.

    """
    post_save.connect(create_language_permissions_and_group, sender=Language)
    if get_wagtailtrans_setting('SYNC_TREE'):
        if get_wagtailtrans_setting('LANGUAGES_PER_SITE'):
            m2m_changed.connect(update_language_trees_for_site,
                                sender=SiteLanguages.other_languages.through)
        else:
            post_save.connect(create_new_language_tree, sender=Language)

        init_new_page.connect(force_parent_language)
        for model in get_page_models():
            if hasattr(model, 'create_translation'):
                post_save.connect(synchronize_trees, sender=model)

            if hasattr(model, 'get_translations'):
                pre_delete.connect(synchronize_deletions, sender=model)
Exemple #5
0
    def can_delete(self):
        """Check if a page can be deleted
        We make the check if the translated sites are kept in sync and
        if the page is a translated page (it has a canonical page)

        :return: Boolean

        """
        has_canonical = getattr(self.page.specific, 'canonical_page', False)
        if (has_canonical and get_wagtailtrans_setting('SYNC_TREE')
                and not self.user.is_superuser):
            return False
        return super(TranslatablePagePermissionTester, self).can_delete()
Exemple #6
0
def create_new_language_tree_for_site(site, language):
    """Create a new language tree for a specific site.

    :param site: The site for which a new tree wil be created
    :param language: The language in which the tree wil be created
    """
    site_pages = site.root_page.get_children().values_list('pk', flat=True)
    default_language = (site.sitelanguages.default_language
                        if get_wagtailtrans_setting('LANGUAGES_PER_SITE') else
                        Language.objects.default())
    canonical_home_page = (TranslatablePage.objects.filter(
        pk__in=site_pages, language=default_language).first())
    if not canonical_home_page:
        # no pages created yet.
        return
    descendants = canonical_home_page.get_descendants(inclusive=True)
    for child_page in descendants:
        if not child_page.specific.has_translation(language):
            child_page.specific.create_translation(language, copy_fields=True)
Exemple #7
0
def force_parent_language(**kwargs):
    """Force the initial language of the first page, before creating..

    When adding a homepage to a site, the initial language should be set.
    By default we set the default language from the Languages model, however
    when the languages are defined per site, it's possible that the default
    language differs from the database default.

    """
    page = kwargs.get('page')
    parent = kwargs.get('parent')

    #: Force the page language according to the parent, when the parent
    #: has no language set and is a site root page, force the default language
    #: For now we assume there isn't more than 1 site rooted at the parent.
    if hasattr(parent, 'language'):
        page.language = parent.language
    elif get_wagtailtrans_setting('LANGUAGES_PER_SITE'):
        site = parent.sites_rooted_here.first()
        if site:
            lang_settings = SiteLanguages.for_site(site)
            page.language = (lang_settings.default_language
                             or Language.objects.default())
Exemple #8
0
def languages_per_site_enabled():
    return get_wagtailtrans_setting('LANGUAGES_PER_SITE')
Exemple #9
0
 def clean_is_default(self):
     """Force the `is_default` to stay the same, when in sync mode."""
     if (self.instance and get_wagtailtrans_setting('SYNC_TREE')
             and Language.objects.default()):
         return self.instance.is_default
     return self.cleaned_data['is_default']
Exemple #10
0
def test_get_wagtailtrans_setting_defaults():
    for key, value in conf.DEFAULT_SETTINGS.items():
        assert conf.get_wagtailtrans_setting(key) == value
Exemple #11
0
def test_get_wagtailtrans_setting_override():
    with override_settings(WAGTAILTRANS_SYNC_TREE=False,
                           WAGTAILTRANS_LANGUAGES_PER_SITE=True):
        assert not conf.get_wagtailtrans_setting('SYNC_TREE')
        assert conf.get_wagtailtrans_setting('LANGUAGES_PER_SITE')
Exemple #12
0
        url(r'^translate/',
            include(translations, namespace='wagtailtrans_translations')),
    ]


@hooks.register('register_settings_menu_item')
def register_language_menu_item():
    return MenuItem(
        'Languages',
        reverse('wagtailtrans_languages:index'),
        classnames='icon icon-snippet',
        order=1000,
    )


if get_wagtailtrans_setting('LANGUAGES_PER_SITE'):

    @hooks.register('insert_global_admin_js')
    def global_admin_js():
        return format_html(
            '<script type="text/javascript" src="{path}"></script>'.format(
                path=static('wagtailtrans/js/site_languages_editor.js')))


if not get_wagtailtrans_setting('SYNC_TREE'):
    """Only load hooks when WAGTAILTRANS_SYNC_TREE is disabled"""
    @hooks.register('register_page_listing_buttons')
    def page_translations_menu(page, page_perms, is_parent=False):
        if not hasattr(page, 'language'):
            return