Exemplo n.º 1
0
 def _itercheck(_dict):
     for k in _dict:
         if is_lazy_string(_dict[k]):
             _dict[k] = unicode(_dict[k])
         if isinstance(_dict[k], dict):
             _itercheck(_dict[k])
         if isinstance(_dict[k], (tuple, list)):
             for i, x in enumerate(_dict[k]):
                 if is_lazy_string(x):
                     _dict[k][i] = unicode(x)
Exemplo n.º 2
0
 def group(self):
     if isinstance(self._group, basestring):
         return self._group
     elif speaklater.is_lazy_string(self._group):
         return self._group.value
     else:
         return self._group()
Exemplo n.º 3
0
 def object_name_plural(self):
     """Plural version of `object_name`. Uses the inflect library for a default value."""
     return self._inflect.plural(
         self.object_name
         if not is_lazy_string(self.object_name)
         else str(self.object_name)
     )
Exemplo n.º 4
0
def serialize(obj, additional_attrs=(), ignore_error=False):
    """Converts some kinds of object to serilized object. (In fact, it does 
    not serialize object, but converts to generally serializable object.)
    """
    if obj is None:
        return None
    elif isinstance(obj, tuple(_user_model_map.keys())):
        for type in _user_model_map.iterkeys():
            if isinstance(obj, type):
                return _user_model_map[type](obj, additional_attrs,
                                             ignore_error)
    elif callable(getattr(obj, 'serialize', None)):
        d = obj.serialize()
        d.update(_serialize_addtional(obj, additional_attrs, ignore_error))
        return d
    elif ismoduleinstance(obj, 'werkzeug.FileStorage'):  # Werkzeug filestorage
        return _serialize_file_storage(obj, additional_attrs, ignore_error)
    elif isinstance(obj, datetime):
        return _serialize_date(obj, additional_attrs, ignore_error)
    elif isinstance(obj, tuple):
        return tuple(
            map(lambda x: serialize(x, additional_attrs, ignore_error), obj))
    elif isinstance(obj, (numbers.Number, basestring)):
        return obj
    elif is_lazy_string(obj):
        return u(obj)
    elif isinstance(obj, collections.Mapping):
        d = {}
        for k, v in dict(obj).iteritems():
            d[k] = serialize(v, additional_attrs, ignore_error)
        return d
    elif is_list_like(obj):
        return [serialize(x, additional_attrs, ignore_error) for x in obj]
    else:
        raise NotSerializable(repr(obj))
Exemplo n.º 5
0
 def default(self, obj):
     if is_lazy_string(obj):
         try:
             return unicode(obj)  # python 2
         except NameError:
             return str(obj)  # python 3
     return super(CustomJSONEncoder, self).default(obj)
Exemplo n.º 6
0
    def __init__(self, *args, nav_group=None, icon_class=None):
        self.label = None
        if len(args) and (isinstance(args[0], str) or is_lazy_string(args[0])):
            self.label = args[0]
            args = args[1:]
        self.route = None
        self.sub_nodes = None
        self.nav_group = nav_group
        self.icon_class = icon_class

        # cache permission-related items
        self._is_permitted = {}
        self._permitted_sub_nodes = {}

        if len(args) == 0:
            raise Exception(_('must provide a NavURL or a list of NavItems'))

        if isinstance(args[0], NavURL):
            self.route = args[0]
            if len(args) > 1:
                args = args[1:]
            else:
                return

        if len(args):
            self.sub_nodes = args
            if not self.nav_group:
                self.nav_group = simplify_string(self.label or '__root__')
Exemplo n.º 7
0
 def lazy_gettext(string, **variables):
     if is_lazy_string(string):
         return string
     translations = TrytonTranslations(module, 'nereid')
     return make_lazy_string(
         translations.lazy_ugettext, string, **variables
     )
Exemplo n.º 8
0
 def _wrapper(*args, **kwargs):
     rv = f(*args, **kwargs)
     if not rv:
         return ''
     if is_lazy_string(rv):
         rv = rv.value
     return rv.encode('utf-8') if isinstance(rv, unicode) else str(rv)
Exemplo n.º 9
0
	def default(self, obj):
		if is_lazy_string(obj):
			try:
				return unicode(obj) #python 2
			except NameError:
				return str(obj)  #python 3
		return super(CustomJSONEncoder, self).default(obj)
Exemplo n.º 10
0
 def lazy_gettext(string, **variables):
     if is_lazy_string(string):
         return string
     translations = TrytonTranslations(module, 'nereid')
     return make_lazy_string(
         translations.lazy_ugettext, string, **variables
     )
Exemplo n.º 11
0
def serialize(obj, additional_attrs=(), ignore_error=False):
    """Converts some kinds of object to serilized object. (In fact, it does 
    not serialize object, but converts to generally serializable object.)
    """
    if obj is None:
        return None
    elif isinstance(obj, tuple(_user_model_map.keys())):
        for type in _user_model_map.iterkeys():
            if isinstance(obj, type):
                return _user_model_map[type](obj, additional_attrs,
                    ignore_error)
    elif callable(getattr(obj, 'serialize', None)):
        d = obj.serialize()
        d.update(_serialize_addtional(obj, additional_attrs, ignore_error))
        return d
    elif ismoduleinstance(obj, 'werkzeug.FileStorage'): # Werkzeug filestorage
        return _serialize_file_storage(obj, additional_attrs, ignore_error)
    elif isinstance(obj, datetime):
        return _serialize_date(obj, additional_attrs, ignore_error)
    elif isinstance(obj, tuple):
        return tuple(map(lambda x:serialize(x, additional_attrs, ignore_error),
            obj))
    elif isinstance(obj, (numbers.Number, basestring)):
        return obj
    elif is_lazy_string(obj):
        return u(obj)
    elif isinstance(obj, collections.Mapping):
        d={}
        for k, v in dict(obj).iteritems():
            d[k] = serialize(v, additional_attrs, ignore_error)
        return d
    elif is_list_like(obj):
        return [serialize(x, additional_attrs, ignore_error) for x in obj]
    else:
        raise NotSerializable(repr(obj))
Exemplo n.º 12
0
 def _wrapper(*args, **kwargs):
     rv = f(*args, **kwargs)
     if not rv:
         return ""
     if is_lazy_string(rv):
         rv = rv.value
     return rv.encode("utf-8") if isinstance(rv, unicode) else str(rv)
Exemplo n.º 13
0
 def default(self, obj):
     from speaklater import is_lazy_string
     if is_lazy_string(obj):
         try:
             return unicode(obj)
         except NameError:
             return str(obj)
     return super(CustomJSONEncoder, self).default(obj)
Exemplo n.º 14
0
def _prepare_excel_data(data):
    if isinstance(data, (list, tuple)):
        data = ', '.join(data)
    elif isinstance(data, set):
        data = ', '.join(sorted(data, key=unicode.lower))
    elif is_lazy_string(data) or isinstance(data, Markup):
        data = unicode(data)
    return data
Exemplo n.º 15
0
def get_message(key, **kwargs):
    rv = config_value('MSG_' + key)
    
    if is_lazy_string:
        if is_lazy_string(rv[0]):
            return unicode(rv[0]) % kwargs, rv[1]
        
    return rv[0] % kwargs, rv[1]
Exemplo n.º 16
0
 def default(self, o):
     from speaklater import is_lazy_string
     if is_lazy_string(o):
         try:
             return unicode(o)
         except NameError:
             return str(o)
     return super(CustomJSONEncoder, self).default(o)
Exemplo n.º 17
0
def _prepare_excel_data(data):
    if isinstance(data, (list, tuple)):
        data = ', '.join(data)
    elif isinstance(data, set):
        data = ', '.join(sorted(data, key=unicode.lower))
    elif is_lazy_string(data) or isinstance(data, Markup):
        data = unicode(data)
    return data
Exemplo n.º 18
0
def test_title(db):
    user = User(first_name='Guinea', last_name='Pig')
    db.session.add(user)
    db.session.flush()
    assert user.title == ''
    user.title = UserTitle.prof
    assert user.title == UserTitle.prof.title
    assert is_lazy_string(user.title)
    assert User.find_one(title=UserTitle.prof) == user
Exemplo n.º 19
0
def fix_xls_value(value):
    """
    Perform any data type fixes that must be made
    prior to sending a value to be written by the spreadsheet library
    """
    if is_lazy_string(value):
        return six.text_type(value)

    return value
Exemplo n.º 20
0
def fix_xls_value(value):
    """
    Perform any data type fixes that must be made
    prior to sending a value to be written by the spreadsheet library
    """
    if is_lazy_string(value):
        return six.text_type(value)

    return value
Exemplo n.º 21
0
def test_title(db):
    user = User(first_name='Guinea', last_name='Pig')
    db.session.add(user)
    db.session.flush()
    assert user.title == ''
    user.title = UserTitle.prof
    assert user.title == UserTitle.prof.title
    assert is_lazy_string(user.title)
    assert User.find_one(title=UserTitle.prof) == user
Exemplo n.º 22
0
def _prepare_excel_data(data, tz=None):
    if isinstance(data, (list, tuple)):
        data = '; '.join(data)
    elif isinstance(data, set):
        data = '; '.join(sorted(data, key=unicode.lower))
    elif is_lazy_string(data) or isinstance(data, Markup):
        data = unicode(data)
    elif isinstance(data, datetime):
        data = format_datetime(data, timezone=tz).decode('utf-8')
    return data
Exemplo n.º 23
0
 def default(self, o):
     if is_lazy_string(o):
         return six.text_type(o)
     if isinstance(o, BaseTzInfo):
         return o.zone
     if isinstance(o, RoleAccessProxy):
         return dict(o)
     if isinstance(o, furl):
         return o.url
     return super(JSONEncoder, self).default(o)
Exemplo n.º 24
0
def _prepare_excel_data(data, tz=None):
    if isinstance(data, (list, tuple)):
        data = ', '.join(data)
    elif isinstance(data, set):
        data = ', '.join(sorted(data, key=unicode.lower))
    elif is_lazy_string(data) or isinstance(data, Markup):
        data = unicode(data)
    elif isinstance(data, datetime):
        data = format_datetime(data, timezone=tz).decode('utf-8')
    return data
Exemplo n.º 25
0
 def __call__(self, form, field):
     if self._original_message and (not is_lazy_string(self.message)
                                    and not self.message):
         # Creat on first usage within app context.
         cv = config_value("MSG_" + self._original_message)
         if cv:
             self.message = make_lazy_string(_local_xlate, cv[0])
         else:
             self.message = self._original_message
     return super(ValidatorMixin, self).__call__(form, field)
Exemplo n.º 26
0
 def default(self, obj):
     if is_lazy_string(obj):
         return unicode(obj)
     elif isinstance(obj, bson.objectid.ObjectId):
         return str(obj)
     elif isinstance(obj, datetime.datetime):
         return obj.isoformat()
     elif hasattr(obj, 'serialize'):
         return obj.serialize()
     elif hasattr(obj, '_data'): # Serialize Raw data for Document and EmbeddedDocument
         return obj._data
     return super(UDataJsonEncoder, self).default(obj)
Exemplo n.º 27
0
 def encode(self, obj, *args, **kwargs):
     if isinstance(obj, dict):
         new_obj = type(obj)()
         for key, value in six.iteritems(obj):
             if is_lazy_string(key):
                 try:
                     key = unicode(key)
                 except NameError:
                     key = str(key)
             new_obj[key] = value
         obj = new_obj 
     return super(JSONEncoder, self).encode(obj, *args, **kwargs)
Exemplo n.º 28
0
 def _validate(var):
     if isinstance(var, (tuple, list)):
         for i, v in enumerate(var):
             var[i] = _validate(v)
     elif isinstance(var, dict):
         for k in var:
             var[k] = _validate(var[k])
     else:
         if is_lazy_string(var):
             return unicode(var)
         return var
     return var
Exemplo n.º 29
0
 def encode(self, obj, *args, **kwargs):
     if isinstance(obj, dict):
         new_obj = type(obj)()
         for key, value in six.iteritems(obj):
             if is_lazy_string(key):
                 try:
                     key = unicode(key)
                 except NameError:
                     key = str(key)
             new_obj[key] = value
         obj = new_obj 
     return super(JSONEncoder, self).encode(obj, *args, **kwargs)
Exemplo n.º 30
0
    def __init__(self, button):
        if isinstance(button, str) or is_lazy_string(button):
            button = {
                'text': button,
                'type': 'text',
            }

        else:
            if 'type' not in button:
                button['type'] = 'text'

        super().__init__(button)
Exemplo n.º 31
0
 def default(self, obj):
     if is_lazy_string(obj):
         return unicode(obj)
     elif isinstance(obj, bson.objectid.ObjectId):
         return str(obj)
     elif isinstance(obj, datetime.datetime):
         return obj.isoformat()
     elif hasattr(obj, 'serialize'):
         return obj.serialize()
     # Serialize Raw data for Document and EmbeddedDocument.
     elif hasattr(obj, '_data'):
         return obj._data
     return super(UDataJsonEncoder, self).default(obj)
Exemplo n.º 32
0
 def errors_with_data(self):
     # Convert lazy_gettext error strings into unicode so they don't cause problems downstream
     # (like when pickling)
     return {
         name: {
             'data':
             f.data,
             'errors': [
                 six.text_type(e) if is_lazy_string(e) else e
                 for e in f.errors
             ]
         }
         for name, f in iteritems(self._fields) if f.errors
     }
Exemplo n.º 33
0
def obj_to_string(obj):
    '''Render an object into a unicode string if possible'''
    if not obj:
        return None
    elif isinstance(obj, bytes):
        return obj.decode('utf-8')
    elif isinstance(obj, basestring):
        return obj
    elif is_lazy_string(obj):
        return obj.value
    elif hasattr(obj, '__html__'):
        return obj.__html__()
    else:
        return str(obj)
Exemplo n.º 34
0
 def default(self, data):
     if is_lazy_string(data):
         return unicode(data)
     elif isinstance(data, bson.objectid.ObjectId):
         return str(data)
     elif isinstance(data, datetime.datetime):
         return data.isoformat()
     elif hasattr(data, 'serialize'):
         return data.serialize()
     # Serialize Raw data for Document and EmbeddedDocument.
     elif hasattr(data, '_data'):
         return data._data
     else:
         return super(EsJSONSerializer, self).default(data)
Exemplo n.º 35
0
 def default(self, o):
     if is_lazy_string(o):
         return six.text_type(o)
     if isinstance(o, BaseTzInfo):
         return o.zone
     if isinstance(o, (date, datetime, time)):
         return o.isoformat()
     if isinstance(o, abc.Mapping):
         return dict(o)
     if isinstance(o, furl):
         return o.url
     if isinstance(o, types.GeneratorType):
         return list(o)
     if isinstance(o, MarkdownComposite):
         return {'text': o.text, 'html': o.html}
     return super(JSONEncoder, self).default(o)
Exemplo n.º 36
0
def flash(message, category='message'):
    """
    Lazy strings are no real strings so pickling them results in strange issues.
    Pickling cannot be avoided because of the way sessions work. Hence, this
    special flash function converts lazy strings to unicode content.

    .. versionadded:: 3.0.4.1

    :param message: the message to be flashed.
    :param category: the category for the message.  The following values
                     are recommended: ``'message'`` for any kind of message,
                     ``'error'`` for errors, ``'info'`` for information
                     messages and ``'warning'`` for warnings.  However any
                     kind of string can be used as category.
    """
    if is_lazy_string(message):
        message = unicode(message)
    return _flash(message, category)
Exemplo n.º 37
0
 def default(self, obj):
     if is_lazy_string(obj):
         return unicode(obj)
     elif isinstance(obj, bson.objectid.ObjectId):
         return str(obj)
     elif isinstance(obj, datetime.datetime):
         return obj.isoformat()
     elif hasattr(obj, 'to_dict'):
         return obj.to_dict()
     elif hasattr(obj, 'serialize'):
         return obj.serialize()
     # Serialize Raw data for Document and EmbeddedDocument.
     elif hasattr(obj, '_data'):
         return obj._data
     # Serialize raw data from Elasticsearch DSL AttrList
     elif hasattr(obj, '_l_'):
         return obj._l_
     return super(UDataJsonEncoder, self).default(obj)
Exemplo n.º 38
0
 def default(self, obj):
     if is_lazy_string(obj):
         return unicode(obj)
     elif isinstance(obj, bson.objectid.ObjectId):
         return str(obj)
     elif isinstance(obj, datetime.datetime):
         return obj.isoformat()
     elif hasattr(obj, 'to_dict'):
         return obj.to_dict()
     elif hasattr(obj, 'serialize'):
         return obj.serialize()
     # Serialize Raw data for Document and EmbeddedDocument.
     elif hasattr(obj, '_data'):
         return obj._data
     # Serialize raw data from Elasticsearch DSL AttrList
     elif hasattr(obj, '_l_'):
         return obj._l_
     return super(UDataJsonEncoder, self).default(obj)
Exemplo n.º 39
0
def flash(message, category='message'):
    """
    Lazy strings are no real strings so pickling them results in strange issues.
    Pickling cannot be avoided because of the way sessions work. Hence, this
    special flash function converts lazy strings to unicode content.

    .. versionadded:: 3.0.4.1

    :param message: the message to be flashed.
    :param category: the category for the message.  The following values
                     are recommended: ``'message'`` for any kind of message,
                     ``'error'`` for errors, ``'info'`` for information
                     messages and ``'warning'`` for warnings.  However any
                     kind of string can be used as category.
    """
    if is_lazy_string(message):
        message = unicode(message)
    return _flash(message, category)
Exemplo n.º 40
0
    def prepare(self):
        super(CookieConsentWidget, self).prepare()

        if self.options:
            cfg = self.options
        else:
            cfg = self.default_options

        # always use full URLs for themes
        theme = cfg.get('theme', 'light-floating')
        rl = request_local()
        cfg['theme'] = (
            rl['middleware'].config.res_prefix + 'tw2.cookieconsent/static/'
            + theme + ".css")

        # evaluate any lazy strings
        for key, value in cfg.items():
            if is_lazy_string(value):
                cfg[key] = str(value)

        self.add_call(js_function(
            "(function(window,cfg){window.cookieconsent_options=cfg;})"
        )(js_symbol("window"), cfg), location='head')
        inject_resources()
Exemplo n.º 41
0
def flash(message, category='message', form_errors=False):
    def form_errors_parse(form_errors_message):
        for v in form_errors_message.values():
            v = [unicode(v) for v in v]
            yield ", ".join(v)

    if isinstance(message, (str, unicode)):
        f(message=message, category=category)

    if isinstance(message, dict):
        if form_errors:
            f(message=', '.join([v for v in form_errors_parse(message)]),
              category=category)
        else:
            f(message=', '.join(message.values()), category=category)

    if isinstance(message, list):
        f(message=', '.join(message), category=category)

    if isinstance(message, tuple):
        f(message=', '.join([v for v in message]), category=category)

    if is_lazy_string(message):
        f(message=unicode(message), category=category)
Exemplo n.º 42
0
 def object_name_plural(self):
     return self._inflect.plural(
         self.object_name
         if not is_lazy_string(self.object_name)
         else str(self.object_name)
     )
Exemplo n.º 43
0
 def lazy_gettext(string, *args, **kwargs):
     if is_lazy_string(string):
         return string
     return make_lazy_string(lookup_func(), string, *args, **kwargs)
Exemplo n.º 44
0
def orig_string(lazy_string):
    """Get the original string from a lazy string"""
    return lazy_string._args[0] if is_lazy_string(lazy_string) else lazy_string
Exemplo n.º 45
0
def lazy_gettext(string, plugin_name=None):
    if is_lazy_string(string):
        return string
    return make_lazy_string(gettext_unicode, string, plugin_name=plugin_name)
Exemplo n.º 46
0
 def default(self, obj):
     from speaklater import is_lazy_string
     if is_lazy_string(obj):
         return str(obj)  # python 3
     return super(CustomJSONEncoder, self).default(obj)
Exemplo n.º 47
0
def orig_string(lazy_string):
    """Get the original string from a lazy string"""
    return lazy_string._args[0] if is_lazy_string(lazy_string) else lazy_string
Exemplo n.º 48
0
def force_text(val):
    if is_lazy_string(val):
        val = val.value
    return to_unicode(val)
Exemplo n.º 49
0
 def lazy_gettext(string, *args, **kwargs):
     if is_lazy_string(string):
         return string
     return make_lazy_string(lookup_func(), string, *args, **kwargs)
Exemplo n.º 50
0
 def anchor(self):
     if isinstance(self._anchor, basestring) or \
        speaklater.is_lazy_string(self._anchor):
         return self._anchor
     else:
         return self._anchor()
Exemplo n.º 51
0
    def scaffold_filters(self, name):
        """
            Return list of enabled filters
        """

        attr, joins = tools.get_field_with_path(self.model, name)

        if attr is None:
            raise Exception("Failed to find field for filter: %s" % name)

        # Figure out filters for related column
        if is_relationship(attr):
            filters = []

            for p in self._get_model_iterator(attr.property.mapper.class_):
                if hasattr(p, "columns"):
                    # TODO: Check for multiple columns
                    column = p.columns[0]

                    if column.foreign_keys or column.primary_key:
                        continue

                    visible_name = "%s / %s" % (self.get_column_name(attr.prop.table.name), self.get_column_name(p.key))

                    type_name = type(column.type).__name__
                    flt = self.filter_converter.convert(type_name, column, visible_name)

                    if flt:
                        table = column.table

                        if joins:
                            self._filter_joins[column] = joins
                        elif tools.need_join(self.model, table):
                            self._filter_joins[column] = [table]

                        filters.extend(flt)

            return filters
        else:
            is_hybrid_property = tools.is_hybrid_property(self.model, name)
            if is_hybrid_property:
                column = attr
                if isinstance(name, string_types):
                    column.key = name.split(".")[-1]
            else:
                columns = tools.get_columns_for_field(attr)

                if len(columns) > 1:
                    raise Exception("Can not filter more than on one column for %s" % name)

                column = columns[0]

            # Join not needed for hybrid properties
            if not is_hybrid_property and tools.need_join(self.model, column.table) and name not in self.column_labels:
                visible_name = "%s / %s" % (self.get_column_name(column.table.name), self.get_column_name(column.name))
            else:
                if not isinstance(name, string_types):
                    visible_name = self.get_column_name(name.property.key)
                else:
                    column_name = self.get_column_name(name)

                    def prettify():
                        return column_name.replace(".", " / ")

                    if is_lazy_string(column_name):
                        visible_name = make_lazy_string(prettify)
                    else:
                        visible_name = prettify()

            type_name = type(column.type).__name__

            flt = self.filter_converter.convert(type_name, column, visible_name, options=self.column_choices.get(name))

            if joins:
                self._filter_joins[column] = joins
            elif not is_hybrid_property and tools.need_join(self.model, column.table):
                self._filter_joins[column] = [column.table]

            return flt
Exemplo n.º 52
0
 def _get_contents(self):
     """Create strings from lazy strings."""
     return [
         str(value) if is_lazy_string(value) else value
         for value in super(LazyNpmBundle, self)._get_contents()
     ]
Exemplo n.º 53
0
 def default(self, obj):
     if is_lazy_string(obj):
         return str(obj)
     else:
         return JSONEncoder.default(self, obj)
Exemplo n.º 54
0
def force_text(val):
    if is_lazy_string(val):
        val = val.value
    return to_unicode(val)
Exemplo n.º 55
0
 def default(self, data):
     if is_lazy_string(data):
         return unicode(data)
     else:
         return super(EsJSONSerializer, self).default(data)
Exemplo n.º 56
0
    def scaffold_filters(self, name):
        """
            Return list of enabled filters
        """

        attr, joins = tools.get_field_with_path(self.model, name)

        if attr is None:
            raise Exception('Failed to find field for filter: %s' % name)

        # Figure out filters for related column
        if is_relationship(attr):
            filters = []

            for p in self._get_model_iterator(attr.property.mapper.class_):
                if hasattr(p, 'columns'):
                    # TODO: Check for multiple columns
                    column = p.columns[0]

                    if column.foreign_keys or column.primary_key:
                        continue

                    visible_name = '%s / %s' % (self.get_column_name(attr.prop.table.name),
                                                self.get_column_name(p.key))

                    type_name = type(column.type).__name__
                    flt = self.filter_converter.convert(type_name,
                                                        column,
                                                        visible_name)

                    if flt:
                        table = column.table

                        if joins:
                            self._filter_joins[column] = joins
                        elif tools.need_join(self.model, table):
                            self._filter_joins[column] = [table]

                        filters.extend(flt)

            return filters
        else:
            is_hybrid_property = tools.is_hybrid_property(self.model, name)
            if is_hybrid_property:
                column = attr
                if isinstance(name, string_types):
                    column.key = name.split('.')[-1]
            else:
                columns = tools.get_columns_for_field(attr)

                if len(columns) > 1:
                    raise Exception('Can not filter more than on one column for %s' % name)

                column = columns[0]

            # If filter related to relation column (represented by
            # relation_name.target_column) we collect here relation name
            joined_column_name = None
            if '.' in name:
                joined_column_name = name.split('.')[0]

            # Join not needed for hybrid properties
            if (not is_hybrid_property and tools.need_join(self.model, column.table) and
                    name not in self.column_labels):
                if joined_column_name:
                    visible_name = '%s / %s / %s' % (
                        joined_column_name,
                        self.get_column_name(column.table.name),
                        self.get_column_name(column.name)
                    )
                else:
                    visible_name = '%s / %s' % (
                        self.get_column_name(column.table.name),
                        self.get_column_name(column.name)
                    )
            else:
                if not isinstance(name, string_types):
                    visible_name = self.get_column_name(name.property.key)
                else:
                    column_name = self.get_column_name(name)

                    def prettify():
                        return column_name.replace('.', ' / ')
                    if is_lazy_string(column_name):
                        visible_name = make_lazy_string(prettify)
                    else:
                        visible_name = prettify()

            type_name = type(column.type).__name__

            flt = self.filter_converter.convert(
                type_name,
                column,
                visible_name,
                options=self.column_choices.get(name),
            )

            key_name = column
            # In case of filter related to relation column filter key
            # must be named with relation name (to prevent following same
            # target column to replace previous)
            if joined_column_name:
                key_name = "{0}.{1}".format(joined_column_name, column)
                for f in flt:
                    f.key_name = key_name

            if joins:
                self._filter_joins[key_name] = joins
            elif not is_hybrid_property and tools.need_join(self.model, column.table):
                self._filter_joins[key_name] = [column.table]

            return flt
Exemplo n.º 57
0
    def default(self, o):  # pylint: disable=method-hidden
        if is_lazy_string(o):
            return str(o)

        return BaseEncoder.default(self, o)
Exemplo n.º 58
0
def lazy_gettext(string, plugin_name=None):
    if is_lazy_string(string):
        return string
    return make_lazy_string(gettext_unicode, string, plugin_name=plugin_name)
Exemplo n.º 59
0
    def default(self, o):
        if is_lazy_string(o):
            return str(o)

        return BaseEncoder.default(self, o)
Exemplo n.º 60
0
 def _get_contents(self):
     """Create strings from lazy strings."""
     return [
         str(value) if is_lazy_string(value) else value
         for value in super(LazyNpmBundle, self)._get_contents()
     ]