def iter_exportable_languages(publishable: Publishable,
                              user: User = None) -> Iterable[str]:
    """Iterates over all the translations for the given object that can be
    statically exported.

    :param publishable: The publishable object to evaluate.

    :param user: The user for which the export should be performed. Defaults to
        the active user.

    :return: An iterable sequence of language codes.
    """
    if publishable.x_staticpub_exportable and publishable.enabled:

        if user is None:
            user = app.user

        if publishable.per_language_publication:
            for language in publishable.enabled_translations:
                if publishable.is_accessible(
                        user=user,
                        language=language,
                        website=PublishableObject.any_website):
                    yield language
        else:
            if publishable.is_accessible(
                    user=user, website=PublishableObject.any_website):
                yield None
Exemplo n.º 2
0
    def test_translation_permitted(self):

        from cocktail.translations import language_context
        from woost.models import (Publishable, ReadPermission,
                                  ReadTranslationPermission, set_current_user)

        set_current_user(self.user)

        self.everybody_role.permissions.append(
            ReadPermission(matching_items={
                "type": "woost.models.publishable.Publishable"
            }))

        self.everybody_role.permissions.append(
            ReadTranslationPermission(matching_languages=["ca", "es"]))

        self.everybody_role.permissions.append(
            ReadTranslationPermission(matching_languages=["en"],
                                      authorized=False))

        self.config.languages = ["ca", "es", "en"]
        self.config.published_languages = []

        a = Publishable()
        a.per_language_publication = True
        a.insert()

        b = Publishable()
        b.per_language_publication = False
        b.insert()

        with language_context("ca"):
            a.translation_enabled = True
            assert a.is_accessible()
            assert b.is_accessible()
            assert self.accessible_items() == set([a, b])

        for language in "es", "en", "de":
            with language_context(language):
                assert not a.is_accessible()
                assert b.is_accessible()
                assert self.accessible_items() == set([b])