Pull Facet selection handling into TextFacet. Allow TextFacet to be initialized with selections.

This commit is contained in:
Paul Makepeace 2011-04-25 00:03:43 -04:00
parent 140413e72a
commit d980f9b89b
2 changed files with 25 additions and 16 deletions

View File

@ -32,11 +32,11 @@ def from_camel(attr):
class Facet(object): class Facet(object):
def __init__(self, column, type, expression='value', **options): def __init__(self, column, type, expression='value',
**options):
self.type = type self.type = type
self.column_name = column self.column_name = column
self.name = column # XXX not sure what the difference is yet self.name = column # XXX not sure what the difference is yet
self.selection = []
self.expression = expression self.expression = expression
for k, v in options.items(): for k, v in options.items():
setattr(self, k, v) setattr(self, k, v)
@ -45,21 +45,9 @@ class Facet(object):
return dict([(to_camel(k), v) for k, v in self.__dict__.items() return dict([(to_camel(k), v) for k, v in self.__dict__.items()
if v is not None]) if v is not None])
def include(self, selection):
for s in self.selection:
if s['v']['v'] == selection:
return
self.selection.append({'v': {'v': selection, 'l': selection}})
def exclude(self, selection):
self.selection = [s for s in self.selection if s['v']['v'] != selection]
def reset(self):
self.selection = []
class TextFacet(Facet): class TextFacet(Facet):
def __init__(self, column, omit_blank=False, omit_error=False, select_blank=False, select_error=False, invert=False, **options): def __init__(self, column, selection=None, omit_blank=False, omit_error=False, select_blank=False, select_error=False, invert=False, **options):
super(TextFacet, self).__init__( super(TextFacet, self).__init__(
column, column,
type='list', type='list',
@ -69,6 +57,27 @@ class TextFacet(Facet):
select_error=select_error, select_error=select_error,
invert=invert, invert=invert,
**options) **options)
self.selection = []
if selection is None:
selection = []
elif not isinstance(selection, list):
selection = [selection]
for value in selection:
self.include(value)
def include(self, value):
for s in self.selection:
if s['v']['v'] == value:
return
self.selection.append({'v': {'v': value, 'l': value}})
def exclude(self, value):
self.selection = [s for s in self.selection
if s['v']['v'] != value]
def reset(self):
self.selection = []
# Capitalize 'From' to get around python's reserved word. # Capitalize 'From' to get around python's reserved word.
class NumericFacet(Facet): class NumericFacet(Facet):

View File

@ -32,7 +32,7 @@ class FacetTest(unittest.TestCase):
facet = TextFacet(column='column') facet = TextFacet(column='column')
self.assertEqual(facet.as_dict(), {'selectError': False, 'name': 'column', 'selection': [], 'expression': 'value', 'invert': False, 'columnName': 'column', 'selectBlank': False, 'omitBlank': False, 'type': 'list', 'omitError': False}) self.assertEqual(facet.as_dict(), {'selectError': False, 'name': 'column', 'selection': [], 'expression': 'value', 'invert': False, 'columnName': 'column', 'selectBlank': False, 'omitBlank': False, 'type': 'list', 'omitError': False})
facet = NumericFacet(column='column', From=1, to=5) facet = NumericFacet(column='column', From=1, to=5)
self.assertEqual(facet.as_dict(), {'from': 1, 'to': 5, 'selectBlank': True, 'name': 'column', 'selectError': True, 'expression': 'value', 'selection': [], 'selectNumeric': True, 'columnName': 'column', 'selectNonNumeric': True, 'type': 'range'}) self.assertEqual(facet.as_dict(), {'from': 1, 'to': 5, 'selectBlank': True, 'name': 'column', 'selectError': True, 'expression': 'value', 'selectNumeric': True, 'columnName': 'column', 'selectNonNumeric': True, 'type': 'range'})
def test_add_facet(self): def test_add_facet(self):
facet = TextFacet(column='Party Code') facet = TextFacet(column='Party Code')