Back to index

plone3  3.1.7
Public Member Functions
Archetypes.browser.widgets.SelectionWidget Class Reference
Inheritance diagram for Archetypes.browser.widgets.SelectionWidget:
Inheritance graph
[legend]
Collaboration diagram for Archetypes.browser.widgets.SelectionWidget:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def getSelected

Detailed Description

View used in Archetypes language and selection widget.

We start with a browser view for this widget.  We use a test
request and some very simple content to initialize it.

>>> from zope.publisher.browser import TestRequest
>>> class SimpleContent(object):
...     def getCharset(self):
...         return 'utf-8'
>>> widget = SelectionWidget(SimpleContent(), TestRequest())

Test with a simple vocabulary

>>> vocab = ('a', 'b', 'c')
>>> widget.getSelected(vocab, 'a')
['a']
>>> widget.getSelected(vocab, 'A')
[]
>>> widget.getSelected(vocab, 'd')
[]

Test with a DisplayList

>>> from Products.Archetypes.utils import DisplayList
>>> friends = DisplayList([('Monty Python', u'monty'), (u'Guido van Rossum', u'guido')])
>>> widget.getSelected(friends, 'monty')
[]
>>> widget.getSelected(friends, u'guido')
[]
>>> widget.getSelected(friends, 'Spanish Inquisition')
[]

getSelected is used to get a list of selected vocabulary items.
In the widget, we repeat on the vocabulary, comparing
its values with those returned by getSelected. So,    
we always return the same encoding as in the vocabulary.

>>> widget.getSelected(friends, u'Monty Python')
['Monty Python']
>>> widget.getSelected(friends, 'Monty Python')
['Monty Python']
>>> widget.getSelected(friends, u'Guido van Rossum')
[u'Guido van Rossum']
>>> widget.getSelected(friends, 'Guido van Rossum')
[u'Guido van Rossum']

Test with an IntDisplayList:

>>> from Products.Archetypes.utils import IntDisplayList
>>> quarter_vocabulary = IntDisplayList([(0, '0'), (15, '15'), (30, '30'), (45, '45')])
>>> widget.getSelected(quarter_vocabulary, 5)
[]
>>> widget.getSelected(quarter_vocabulary, 15)
[15]
>>> widget.getSelected(quarter_vocabulary, '15')
[]

Definition at line 13 of file widgets.py.


Member Function Documentation

def Archetypes.browser.widgets.SelectionWidget.getSelected (   self,
  vocab,
  value 
)

Definition at line 73 of file widgets.py.

00073 
00074     def getSelected(self, vocab, value):
00075         
00076         context = aq_inner(self.context)
00077 
00078         site_charset = context.getCharset()
00079 
00080         # compile a dictionary from the vocabulary of
00081         # items in {encodedvalue : originalvalue} format
00082         vocabKeys = {}
00083         for key in vocab:
00084             # vocabulary keys can only be strings or integers
00085             if isinstance(key, str):
00086                 vocabKeys[key.decode(site_charset)] = key
00087             else:
00088                 vocabKeys[key] = key
00089         
00090         # compile a dictonary of {encodedvalue : oldvalue} items
00091         # from value -- which may be a sequence, string or integer.
00092         values = {}
00093         if isinstance(value, tuple) or isinstance(value, list):
00094             for v in value:
00095                 new = v
00096                 if isinstance(v, int):
00097                     v = str(v)
00098                 elif isinstance(v, str):
00099                     new = v.decode(site_charset)
00100                 values[new] = v
00101         else:
00102             if isinstance(value, str):
00103                 new = value.decode(site_charset)
00104             elif isinstance(value, int):
00105                 new = value
00106             else:
00107                 new = str(value)
00108             values[new] = value
00109 
00110         # now, build a list of the vocabulary keys
00111         # in their original charsets.
00112         selected = []
00113         for v in values:
00114             ov = vocabKeys.get(v)
00115             if ov:
00116                 selected.append(ov)
00117 
00118         return selected
00119 

Here is the call graph for this function:


The documentation for this class was generated from the following file: