Make config param more explicit, for GridFilter constructor

i.e. the rattail config object
This commit is contained in:
Lance Edgar 2023-02-14 17:32:04 -06:00
parent 539f4a5c31
commit ad4ec41e15
2 changed files with 18 additions and 20 deletions

View file

@ -24,12 +24,9 @@
Core Grid Classes
"""
from __future__ import unicode_literals, absolute_import
import warnings
import logging
import six
from six.moves import urllib
import sqlalchemy as sa
from sqlalchemy import orm
@ -493,8 +490,8 @@ class Grid(object):
return ""
enum = self.enums.get(column_name)
if enum and value in enum:
return six.text_type(enum[value])
return six.text_type(value)
return str(enum[value])
return str(value)
def render_gpc(self, obj, column_name):
value = self.obtain_value(obj, column_name)
@ -687,14 +684,16 @@ class Grid(object):
factory = gridfilters.AlchemyDateTimeFilter
elif isinstance(column.type, GPCType):
factory = gridfilters.AlchemyGPCFilter
kwargs['column'] = column
kwargs.setdefault('config', self.request.rattail_config)
kwargs.setdefault('encode_values', self.use_byte_string_filters)
return factory(key, column=column, config=self.request.rattail_config, **kwargs)
return factory(key, **kwargs)
def iter_filters(self):
"""
Iterate over all filters available to the grid.
"""
return six.itervalues(self.filters)
return self.filters.values()
def iter_active_filters(self):
"""
@ -1002,7 +1001,7 @@ class Grid(object):
else: # source = session
settings['{}.active'.format(prefix)] = self.get_setting(
source, settings, '{}.active'.format(prefix),
normalize=lambda v: six.text_type(v).lower() == 'true', default=False)
normalize=lambda v: str(v).lower() == 'true', default=False)
settings['{}.verb'.format(prefix)] = self.get_setting(
source, settings, '{}.verb'.format(prefix), default='')
settings['{}.value'.format(prefix)] = self.get_setting(
@ -1071,7 +1070,7 @@ class Grid(object):
if self.filterable:
for filtr in self.iter_filters():
persist('filter.{}.active'.format(filtr.key), value=lambda k: six.text_type(settings[k]).lower())
persist('filter.{}.active'.format(filtr.key), value=lambda k: str(settings[k]).lower())
persist('filter.{}.verb'.format(filtr.key))
persist('filter.{}.value'.format(filtr.key))
@ -1305,7 +1304,7 @@ class Grid(object):
'multiple_value_verbs': multiple_values,
'verb_labels': filtr.verb_labels,
'verb': filtr.verb or filtr.default_verb or filtr.verbs[0],
'value': six.text_type(filtr.value) if filtr.value is not None else "",
'value': str(filtr.value) if filtr.value is not None else "",
'data_type': filtr.data_type,
'choices': choices,
'choice_labels': choice_labels,
@ -1478,7 +1477,7 @@ class Grid(object):
value = self.obtain_value(rowobj, name)
if value is None:
value = ""
row[name] = six.text_type(value)
row[name] = str(value)
# maybe add UUID for convenience
if 'uuid' not in self.columns:

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2022 Lance Edgar
# Copyright © 2010-2023 Lance Edgar
#
# This file is part of Rattail.
#
@ -24,13 +24,10 @@
Grid Filters
"""
from __future__ import unicode_literals, absolute_import
import re
import datetime
import logging
import six
import sqlalchemy as sa
from rattail.gpc import GPC
@ -117,7 +114,7 @@ class EnumValueRenderer(ChoiceValueRenderer):
sorted_keys = list(enum.keys())
else:
sorted_keys = sorted(enum, key=lambda k: enum[k].lower())
self.options = [tags.Option(enum[k], six.text_type(k)) for k in sorted_keys]
self.options = [tags.Option(enum[k], str(k)) for k in sorted_keys]
class GridFilter(object):
@ -173,10 +170,12 @@ class GridFilter(object):
data_type = 'string' # default, but will be set from value renderer
choices = {}
def __init__(self, key, label=None, verbs=None, value_enum=None, value_renderer=None,
def __init__(self, key, config=None, label=None, verbs=None,
value_enum=None, value_renderer=None,
default_active=False, default_verb=None, default_value=None,
encode_values=False, value_encoding='utf-8', **kwargs):
self.key = key
self.config = config
self.label = label or prettify(key)
self.verbs = verbs or self.get_default_verbs()
if value_renderer:
@ -279,7 +278,7 @@ class GridFilter(object):
return value if value is not UNSPECIFIED else self.value
def encode_value(self, value):
if self.encode_values and isinstance(value, six.string_types):
if self.encode_values and isinstance(value, str):
return value.encode('utf-8')
return value
@ -536,7 +535,7 @@ class AlchemyByteStringFilter(AlchemyStringFilter):
def get_value(self, value=UNSPECIFIED):
value = super(AlchemyByteStringFilter, self).get_value(value)
if isinstance(value, six.text_type):
if isinstance(value, str):
value = value.encode(self.value_encoding)
return value
@ -590,7 +589,7 @@ class AlchemyNumericFilter(AlchemyGridFilter):
except ValueError:
return True
return bool(value and len(six.text_type(value)) > 8)
return bool(value and len(str(value)) > 8)
def filter_equal(self, query, value):
if self.value_invalid(value):