class not_translated_yet(BrowserView):
    """ View to inform user that the view requested is not translated yet,
        and shows the already translated related content.
    """
    __call__ = ViewPageTemplateFile('templates/not_translated_yet.pt')

    implements(IPublishTraverse)

    def __init__(self, context, request):
        super(not_translated_yet, self).__init__(context, request)
        self.tg = None

    def publishTraverse(self, request, name):
        if self.tg is None:  # ../@@not_translated_yet/translationgroup
            self.tg = TranslationManager(name)
        else:
            raise NotFound(self, name, request)
        return self

    def already_translated(self):
        if self.tg is not None:
            # Need to check if the user has permission
            return self.tg.get_restricted_translations().items()
        else:
            return []

    def has_any_translation(self):

        if self.tg is not None:
            return len(self.tg.get_restricted_translations().items()) > 0
        else:
            return 0

    def language_name(self, lang=None):
        """ Get the current language native name """
        if lang is None:
            lang_code = self.request.get('set_language')
        else:
            lang_code = lang
        util = getUtility(IContentLanguageAvailability)
        data = util.getLanguages(True)
        lang_info = data.get(lang_code)
        return lang_info.get('native', None) or lang_info.get('name')
    def get(self):
        uuid = self.getTranslationUuid()

        if isLanguageIndependent(self.field) and uuid:
            manager = TranslationManager(uuid)
            result = manager.get_restricted_translations()

            if len(result) >= 1:

                orig_lang = result.keys()[0]
                obj = result[orig_lang]
                name = self.field.__name__
                try:
                    value = getattr(aq_base(obj), name)
                except AttributeError:
                    pass
                else:
                    return value

        if self.field.default is None:
            return NO_VALUE

        return self.field.default
 def publishTraverse(self, request, name):
     if self.tg is None:  # ../@@not_translated_yet/translationgroup
         self.tg = TranslationManager(name)
     else:
         raise NotFound(self, name, request)
     return self
    def getClosestDestination(self):
        """Get the "closest translated object" URL.
        """
        # We sould travel the parent chain using the catalog here,
        # but I think using the acquisition chain is faster
        # (or well, __parent__ pointers) because the catalog
        # would require a lot of queries, while technically,
        # having done traversal up to this point you should
        # have the objects in memory already

        # As we don't have any content object we are going to look
        # for the best option

        root = getToolByName(self.context, 'portal_url')
        ltool = getToolByName(self.context, 'portal_languages')

        # We are useing TranslationManager to get the translations of a string tg
        manager = TranslationManager(self.tg)
        context = None
        languages = manager.get_translations()
        if len(languages) == 0:
            # If there is no results there are no translations
            # we move to portal root
            return self.wrapDestination(root(), postpath=False)

        # We are going to see if there is the prefered language translation
        # Otherwise we get the first as context to look for translation
        prefered = ltool.getPreferredLanguage()
        if prefered in languages:
            context = languages[prefered]
        else:
            context = languages[languages.keys()[0]]

        checkPermission = getSecurityManager().checkPermission
        chain = self.getParentChain(context)
        for item in chain:
            if ISiteRoot.providedBy(item):
                # We do not care to get a permission error
                # if the whole of the portal cannot be viewed.
                # Having a permission issue on the root is fine;
                # not so much for everything else so that is checked there
                return self.wrapDestination(item.absolute_url())
            elif IFactoryTempFolder.providedBy(item) or \
                    IFactoryTool.providedBy(item):
                # TempFolder or portal_factory, can't have a translation
                continue
            try:
                canonical = ITranslationManager(item)
            except TypeError:
                if not ITranslatable.providedBy(item):
                    # In case there it's not translatable go to parent
                    # This solves the problem when a parent is not ITranslatable
                    continue
                else:
                    raise
            translation = canonical.get_translation(self.lang)
            if translation and (
                INavigationRoot.providedBy(translation) or
                bool(checkPermission('View', translation))
            ):
                # Not a direct translation, therefore no postpath
                # (the view might not exist on a different context)
                return self.wrapDestination(translation.absolute_url(),
                                            postpath=False)
        # Site root's the fallback
        return self.wrapDestination(root(), postpath=False)