Ejemplo n.º 1
0
class PropertyTranslationDomainGetter(BaseGetter):
    """
  Get the translation domain
  """
    _need__name__ = 1

    # This can be called from the Web
    func_code = func_code()
    func_code.co_varnames = ('self', )
    func_code.co_argcount = 1
    func_defaults = ()

    def __init__(self, id, key, property_type, default=None, storage_id=None):
        self._id = id
        self.__name__ = id
        self._original_key = key.replace('_translation_domain', '')
        self._default = default
        if storage_id is None:
            storage_id = "%s%s" % (ATTRIBUTE_PREFIX, key)
        self._storage_id = storage_id
        self._is_tales_type = (property_type == 'tales')

    def __call__(self, instance, *args, **kw):
        if len(args) > 0:
            default = args[0]
        else:
            default = self._default
        # No acquisition on properties
        value = getattr(aq_base(instance), self._storage_id, None)
        if value is None:
            # second try to get it from portal type
            ptype_domain = None
            ptype = instance.getPortalType()
            ptypes_tool = instance.getPortalObject()['portal_types']
            typeinfo = ptypes_tool.getTypeInfo(ptype)
            if typeinfo is None:
                ptype_domain = ''
            else:
                domain_dict = typeinfo.getPropertyTranslationDomainDict()
                domain = domain_dict.get(self._original_key)
                if domain is None:
                    ptype_domain = ''
                else:
                    ptype_domain = domain.getDomainName()
            if ptype_domain is '' and default is not None:
                # then get the default property defined on property sheet
                value = default
            else:
                value = ptype_domain
        if value is None:
            value = ''
        if self._is_tales_type and kw.get('evaluate', 1):
            return evaluateTales(instance, value)
        else:
            return value

    psyco.bind(__call__)
Ejemplo n.º 2
0
class TranslatedPropertyTester(Method):
    """
    Tests if an attribute value exists
  """
    _need__name__ = 1

    # This is required to call the method form the Web
    func_code = func_code()
    func_code.co_varnames = ('self', )
    func_code.co_argcount = 1
    func_defaults = ()

    def __init__(self,
                 id,
                 key,
                 property_id,
                 property_type,
                 language,
                 warning=0):
        self._id = id
        self.__name__ = id
        self._property_id = property_id
        self._property_type = property_type
        self._null = type_definition[property_type]['null']
        self._language = language
        self._warning = warning

    def __call__(self, instance, *args, **kw):
        if self._warning:
            LOG("ERP5Type Deprecated Tester Id:", 0, self._id)
        domain = instance.getProperty('%s_translation_domain' %
                                      self._property_id)

        if domain == TRANSLATION_DOMAIN_CONTENT_TRANSLATION:
            if self._language is None:
                language = kw.get('language') or instance.getPortalObject(
                ).Localizer.get_selected_language()
            else:
                language = self._language
            try:
                return instance.getPropertyTranslation(self._property_id,
                                                       language) is not None
            except KeyError:
                return False
        else:
            return instance.getProperty(self._property_id) is not None
Ejemplo n.º 3
0
class TranslationPropertySetter(Accessor.Accessor):
    """
  Set a translation into language-property pair dict.
  """
    _need__name__ = 1

    # Generic Definition of Method Object
    # This is required to call the method form the Web
    # More information at http://www.zope.org/Members/htrd/howto/FunctionTemplate
    func_code = func_code()
    func_code.co_varnames = ('self', 'value')
    func_code.co_argcount = 2
    func_defaults = ()

    def __init__(self, id, key, property_id, property_type, language):
        self._id = id
        self.__name__ = id
        self._property_id = property_id
        self._language = language
        self._cast = type_definition[property_type]['cast']
        self._null = type_definition[property_type]['null']

    def __call__(self, instance, *args, **kw):
        value = args[0]
        modified_object_list = []

        domain = instance.getProperty('%s_translation_domain' %
                                      self._property_id)
        if domain == TRANSLATION_DOMAIN_CONTENT_TRANSLATION:
            if value in self._null:
                instance.deletePropertyTranslation(self._property_id,
                                                   self._language)
            else:
                original_property_value = instance.getProperty(
                    self._property_id)
                instance.setPropertyTranslation(self._property_id,
                                                self._language,
                                                original_property_value,
                                                self._cast(args[0]))
                modified_object_list.append(instance)
        else:
            pass
            #raise RuntimeError, 'The property %s.%s is not writable.' % (instance.portal_type, self._property_id)
        return modified_object_list
Ejemplo n.º 4
0
class BaobabValueGetter(Method):
    """Get the value of a baobab category
  """
    _need__name__ = 1

    # Generic Definition of Method Object
    # This is required to call the method form the Web
    func_code = func_code()
    func_code.co_varnames = ('self', )
    func_code.co_argcount = 1
    func_defaults = ()

    def __init__(self, id, method_id):
        self._id = id
        self.__name__ = id
        self._method_id = method_id

    def __call__(self, instance, *args, **kw):
        category = getattr(instance, self._method_id)(*args, **kw)
        if category is not None:
            return instance.portal_categories.resolveCategory(category)
Ejemplo n.º 5
0
class BaobabPropertyGetter(Method):
    """Get the property of a baobab category
  """
    _need__name__ = 1

    # Generic Definition of Method Object
    # This is required to call the method form the Web
    func_code = func_code()
    func_code.co_varnames = ('self', )
    func_code.co_argcount = 1
    func_defaults = ()

    def __init__(self, id, method_id, property_id):
        self._id = id
        self.__name__ = id
        self._method_id = method_id
        self._property_id = property_id

    def __call__(self, instance, *args, **kw):
        value = getattr(instance, self._method_id)(*args, **kw)
        if value is not None:
            return value.getProperty(self._property_id, *args, **kw)
Ejemplo n.º 6
0
class BaobabGetter(Method):
    """Get a category differently
  """
    _need__name__ = 1

    # Generic Definition of Method Object
    # This is required to call the method form the Web
    func_code = func_code()
    func_code.co_varnames = ('self', )
    func_code.co_argcount = 1
    func_defaults = ()

    def __init__(self, id, method_id):
        self._id = id
        self.__name__ = id
        self._method_id = method_id

    def __call__(self, instance, *args, **kw):
        portal_type = instance.getPortalType()
        skin_id = '%s_%s' % (portal_type.replace(' ', ''), self._id)
        skin = getattr(instance, skin_id, None)
        if skin is not None:
            return skin(*args, **kw)
        return getattr(instance, self._method_id)(*args, **kw)
Ejemplo n.º 7
0
class TranslatedPropertyGetter(BaseGetter):
    """
  Get the translated property
  """
    # This can be called from the Web
    func_code = func_code()
    func_code.co_varnames = ('self', )
    func_code.co_argcount = 1
    func_defaults = ()

    def __init__(self,
                 id,
                 key,
                 property_id,
                 property_type,
                 language,
                 default=None,
                 warning=0):
        self._id = id
        self.__name__ = id
        self._property_id = property_id
        self._null = type_definition[property_type]['null']
        self._language = language
        self._default = default
        self._warning = warning

    def __call__(self, instance, *args, **kw):
        if self._warning:
            LOG("ERP5Type Deprecated Getter Id:", 0, self._id)
        domain = instance.getProperty('%s_translation_domain' %
                                      self._property_id)

        if domain == TRANSLATION_DOMAIN_CONTENT_TRANSLATION:
            if len(args) > 0:
                default = args[0]
            else:
                default = instance.getProperty(self._property_id)
            if kw.get('no_original_value'):
                default = self._default

            if self._language is None:
                language = kw.get('language') or instance.getPortalObject(
                ).Localizer.get_selected_language()
            else:
                language = self._language
            try:
                return instance.getPropertyTranslation(self._property_id,
                                                       language)
            except KeyError:
                return default
        else:
            value = instance.getProperty(self._property_id)
            if domain == '' or (value in ('', None)):
                return value
            localizer = instance.getPortalObject().Localizer
            message_catalog = getattr(localizer, domain, None)
            if message_catalog is not None:
                return unicode2str(
                    message_catalog.gettext(ensure_text(value),
                                            lang=self._language))
            return value

    psyco.bind(__call__)