Back to index

plone3  3.1.7
widgets.py
Go to the documentation of this file.
00001 from Acquisition import aq_inner
00002 from Products.Five import BrowserView
00003 
00004 
00005 # map from mimetypes used in allowable_content_types to mimetypes that are stored
00006 # in the base unit
00007 MIMETYPES_MAPPING = {
00008     'text/x-python' : 'text/python-source',
00009     'text/restructured': 'text/x-rst',
00010 }
00011 
00012 
00013 class SelectionWidget(BrowserView):
00014     """View used in Archetypes language and selection widget.
00015 
00016     We start with a browser view for this widget.  We use a test
00017     request and some very simple content to initialize it.
00018 
00019     >>> from zope.publisher.browser import TestRequest
00020     >>> class SimpleContent(object):
00021     ...     def getCharset(self):
00022     ...         return 'utf-8'
00023     >>> widget = SelectionWidget(SimpleContent(), TestRequest())
00024 
00025     Test with a simple vocabulary
00026 
00027     >>> vocab = ('a', 'b', 'c')
00028     >>> widget.getSelected(vocab, 'a')
00029     ['a']
00030     >>> widget.getSelected(vocab, 'A')
00031     []
00032     >>> widget.getSelected(vocab, 'd')
00033     []
00034 
00035     Test with a DisplayList
00036 
00037     >>> from Products.Archetypes.utils import DisplayList
00038     >>> friends = DisplayList([('Monty Python', u'monty'), (u'Guido van Rossum', u'guido')])
00039     >>> widget.getSelected(friends, 'monty')
00040     []
00041     >>> widget.getSelected(friends, u'guido')
00042     []
00043     >>> widget.getSelected(friends, 'Spanish Inquisition')
00044     []
00045 
00046     getSelected is used to get a list of selected vocabulary items.
00047     In the widget, we repeat on the vocabulary, comparing
00048     its values with those returned by getSelected. So,    
00049     we always return the same encoding as in the vocabulary.
00050 
00051     >>> widget.getSelected(friends, u'Monty Python')
00052     ['Monty Python']
00053     >>> widget.getSelected(friends, 'Monty Python')
00054     ['Monty Python']
00055     >>> widget.getSelected(friends, u'Guido van Rossum')
00056     [u'Guido van Rossum']
00057     >>> widget.getSelected(friends, 'Guido van Rossum')
00058     [u'Guido van Rossum']
00059 
00060     Test with an IntDisplayList:
00061 
00062     >>> from Products.Archetypes.utils import IntDisplayList
00063     >>> quarter_vocabulary = IntDisplayList([(0, '0'), (15, '15'), (30, '30'), (45, '45')])
00064     >>> widget.getSelected(quarter_vocabulary, 5)
00065     []
00066     >>> widget.getSelected(quarter_vocabulary, 15)
00067     [15]
00068     >>> widget.getSelected(quarter_vocabulary, '15')
00069     []
00070 
00071     """
00072 
00073     def getSelected(self, vocab, value):
00074         
00075         context = aq_inner(self.context)
00076 
00077         site_charset = context.getCharset()
00078 
00079         # compile a dictionary from the vocabulary of
00080         # items in {encodedvalue : originalvalue} format
00081         vocabKeys = {}
00082         for key in vocab:
00083             # vocabulary keys can only be strings or integers
00084             if isinstance(key, str):
00085                 vocabKeys[key.decode(site_charset)] = key
00086             else:
00087                 vocabKeys[key] = key
00088         
00089         # compile a dictonary of {encodedvalue : oldvalue} items
00090         # from value -- which may be a sequence, string or integer.
00091         values = {}
00092         if isinstance(value, tuple) or isinstance(value, list):
00093             for v in value:
00094                 new = v
00095                 if isinstance(v, int):
00096                     v = str(v)
00097                 elif isinstance(v, str):
00098                     new = v.decode(site_charset)
00099                 values[new] = v
00100         else:
00101             if isinstance(value, str):
00102                 new = value.decode(site_charset)
00103             elif isinstance(value, int):
00104                 new = value
00105             else:
00106                 new = str(value)
00107             values[new] = value
00108 
00109         # now, build a list of the vocabulary keys
00110         # in their original charsets.
00111         selected = []
00112         for v in values:
00113             ov = vocabKeys.get(v)
00114             if ov:
00115                 selected.append(ov)
00116 
00117         return selected
00118 
00119 
00120 class TextareaWidget(BrowserView):
00121     """View used in Archetypes textarea widget."""
00122 
00123     def getSelected(self, mimetypes, contenttype):
00124         # An object can have only one contenttype at a time and mimetypes
00125         # are limited to ASCII-only characters. We already assumed to get all
00126         # values in all lowercase, so we don't do any case-juggling.
00127 
00128         contenttype = MIMETYPES_MAPPING.get(contenttype, contenttype)
00129 
00130         if contenttype in mimetypes:
00131             return (contenttype, )
00132 
00133         return ()
00134 
00135     def lookupMime(self, name):
00136         context = aq_inner(self.context)
00137         mimetool = context.mimetypes_registry
00138         mimetypes = mimetool.lookup(name)
00139         if len(mimetypes):
00140             return mimetypes[0].name()
00141         else:
00142             return name