Exemplo n.º 1
0
    def is_published(self, language=None, website=None):

        if self.per_language_publication:

            language = require_language(language)

            if not self.get("translation_enabled", language):
                return False

            from woost.models import Configuration
            if not Configuration.instance.language_is_enabled(language):
                return False

        elif not self.enabled:
            return False

        if not self.is_current():
            return False

        websites_subset = self.websites
        if websites_subset and website != "any":
            if website is None:
                website = get_current_website()
            if website is None or website not in websites_subset:
                return False

        return True
Exemplo n.º 2
0
    def is_accessible(self, user=None, language=None, website=None):

        if user is None:
            user = get_current_user()

        return (self.is_published(language, website)
                and user.has_permission(ReadPermission, target=self) and
                (not self.per_language_publication
                 or user.has_permission(ReadTranslationPermission,
                                        language=require_language(language))))
Exemplo n.º 3
0
 def iter_derived_translations(self, language=None, include_self=False):
     language = require_language(language)
     for derived_lang in descend_language_tree(language,
                                               include_self=include_self):
         for chain_lang in iter_language_chain(derived_lang):
             if chain_lang == language:
                 yield derived_lang
                 break
             elif chain_lang in self.translations:
                 break
Exemplo n.º 4
0
        def impl(dataset):
            user = self.user or get_current_user()
            access_expr = PermissionExpression(user, ReadPermission)
            published_expr = IsPublishedExpression()
            dataset = access_expr.resolve_filter(query)[1](dataset)
            dataset = published_expr.resolve_filter(query)[1](dataset)

            if not user.has_permission(ReadTranslationPermission,
                                       language=require_language()):
                dataset.difference_update(
                    Publishable.per_language_publication.index.values(
                        key=True))

            return dataset
Exemplo n.º 5
0
    def get_member_value(self, obj, member, language=None):

        translated = member.translated
        expr = self.get_member_expression(member)

        if translated and language is None:
            language = require_language()

        if expr:
            if translated:
                return expr(obj, language)
            else:
                return expr(obj)
        else:
            accessor = self.accessor or schema.get_accessor(obj)
            value = obj
            for part in self._normalize_member(member).split("."):
                value = accessor.get(value, part, None, language)
                if value is None:
                    break
            return value
Exemplo n.º 6
0
    def get_uri(self,
                path=None,
                parameters=None,
                language=None,
                host=None,
                encode=True):

        uri = app.url_resolver.get_path(self, language=language)

        if uri is not None:
            if self.per_language_publication:
                uri = app.language.translate_uri(
                    path=uri, language=require_language(language))

            if path:
                uri = make_uri(uri, *path)

            if parameters:
                uri = make_uri(uri, **parameters)

            if host == "?":
                websites = self.websites
                if websites and get_current_website() not in websites:
                    host = websites[0].hosts[0]
                else:
                    host = None
            elif host == "!":
                if self.websites:
                    host = self.websites[0].hosts[0]
                else:
                    from woost.models import Configuration
                    website = (get_current_website()
                               or Configuration.instances.websites[0])
                    host = website.hosts[0]

            uri = self._fix_uri(uri, host, encode)

        return uri
Exemplo n.º 7
0
 def language_is_enabled(self, language=None):
     language_subset = self.get_setting("published_languages")
     if not language_subset:
         return True
     else:
         return require_language(language) in language_subset
Exemplo n.º 8
0
        def __set__(self,
                    instance,
                    value,
                    language=None,
                    previous_value=undefined):

            member = self.member

            # Set multiple translations at once
            if member.translated and isinstance(value, TranslatedValues):
                for lang, lang_value in value.items():
                    self.__set__(instance, lang_value, lang)
                return

            if member.translated or member.translation_source:

                if member.translation_source:
                    translated_member = member.translation_source
                    translated_object = instance.translated_object
                    language = instance.language
                else:
                    translated_member = member
                    translated_object = instance
                    language = require_language(language)

                # Determine which translations are affected by the change, by
                # following the language fallback tree. This is recorded and
                # supplied to the changing / changed events. The main use case for
                # this information is field indexing.
                translation_changes = {}

                for derived_lang \
                in translated_object.iter_derived_translations(language):
                    translation_changes[derived_lang] = \
                        translated_object.get(
                            translated_member,
                            language = derived_lang
                        )

                # For translated members, make sure the translation for the specified
                # language exists, and then resolve the assignment against it
                if member.translated:
                    target = instance.translations.get(language)
                    if target is None:
                        target = instance.new_translation(language)
                else:
                    target = instance
            else:
                target = instance
                translation_changes = None

            if previous_value is undefined:
                previous_value = getattr(target, self.__priv_key, None)

            if translation_changes is not None:
                translation_changes[language] = previous_value

            # Value normalization and hooks
            if member.normalization:
                value = member.normalization(value)

            value = self.normalization(instance, member, value)

            # Collections require special treatment
            if self._is_collection and not self._translations_collection:

                collection = previous_value

                # When setting the collection for the first time, wrap it with
                # an instrumented instance of the appropiate type
                if value is not None and collection is None:
                    coll_type = self.get_instrumented_collection_type(value)
                    if coll_type:
                        collection = coll_type(None, instance, member)
                        setattr(target, self.__priv_key, collection)

                if isinstance(collection, RelationCollection):
                    collection.set_content(value)
                else:
                    setattr(target, self.__priv_key, value)

            # Regular values
            else:
                try:
                    changed = (type(value) is not type(previous_value)
                               or (value != previous_value))
                except TypeError:
                    changed = True

                # Trigger the 'changing' event
                if changed:
                    change_context = {}
                    event = instance.changing(
                        member=member.translation_source or member,
                        language=language,
                        value=value,
                        previous_value=previous_value,
                        translation_changes=translation_changes,
                        change_context=change_context)

                    value = event.value

                    if member.translation_source:
                        event = instance.translated_object.changing(
                            member=member.translation_source,
                            language=instance.language,
                            value=value,
                            previous_value=previous_value,
                            translation_changes=translation_changes,
                            change_context=change_context)

                    value = event.value

                setattr(target, self.__priv_key, value)

                # Update the opposite end of a bidirectional reference
                if self._bidirectional_reference and value != previous_value:

                    # TODO: translated bidirectional references
                    if previous_value is not None:
                        _update_relation("unrelate",
                                         instance,
                                         previous_value,
                                         member,
                                         relocation=value is not None)

                    if value is not None:
                        _update_relation("relate", instance, value, member)

                try:
                    changed = (value != previous_value)
                except TypeError:
                    changed = True

                if changed:
                    instance.changed(member=member.translation_source
                                     or member,
                                     language=language,
                                     value=value,
                                     previous_value=previous_value,
                                     translation_changes=translation_changes,
                                     added=None,
                                     removed=None,
                                     change_context=change_context)

                    if member.translation_source:
                        instance.translated_object.changed(
                            member=member.translation_source,
                            language=instance.language,
                            value=value,
                            previous_value=previous_value,
                            translation_changes=translation_changes,
                            added=None,
                            removed=None,
                            change_context=change_context)