Back to index

plone3  3.1.7
ATReferenceBrowserWidget.py
Go to the documentation of this file.
00001 from types import StringType
00002 
00003 from AccessControl import ClassSecurityInfo
00004 from Acquisition import aq_base
00005 
00006 from Products.Archetypes.Registry import registerWidget,registerPropertyType
00007 from Products.Archetypes.utils import shasattr
00008 from Products.Archetypes.Widget import ReferenceWidget
00009 
00010 
00011 class ReferenceBrowserWidget(ReferenceWidget):
00012     _properties = ReferenceWidget._properties.copy()
00013     _properties.update({
00014         'macro' : "referencebrowser",
00015         'size' : '',
00016         'helper_js': ('referencebrowser.js',),
00017         'default_search_index':'SearchableText',
00018         'show_indexes':0,
00019         'available_indexes':{},
00020         'allow_search':1,
00021         'allow_browse':1,
00022         'startup_directory':'',
00023         'startup_directory_method':'',
00024         'base_query':'',
00025         'force_close_on_insert':0,
00026         'search_catalog':'portal_catalog',
00027         'allow_sorting':0,
00028         'show_review_state':0,
00029         'show_path':0,
00030         'only_for_review_states' : None,
00031         'image_portal_types' : (),
00032         'image_method' : None,
00033         'history_length' : 0,
00034         'restrict_browsing_to_startup_directory' : 0,
00035         'show_results_without_query': 0,
00036         'hide_inaccessible' : 0,
00037         })
00038 
00039     # default_search_index: when a user searches in the popup, this index is used by default
00040     # show_indexes: in the popup, when set to True, a drop-down list is shown with the index to be
00041     #     used for searching. If set to False, default_search_index will be used.
00042     # size: in case of single-select widget, the default is set to 30. In case of multi-select, default is 8.
00043     # available_indexes: optional dictionary that lists all the indexes that can be used
00044     #  for searching. Format: {'<catalog index>':'<friendly name'>, ... } The friendly name
00045     #  is what the end-users sees to make the indexes more sensible for him.
00046     # allow_search: shows the search section in the popup
00047     # allow_browse: shows the browse section in the popup
00048     # allow_sorting: optional reordering of references (requires multiValued=1)
00049     # startup_directory: directory where the popup opens. Optional. When omitted, the current folder
00050     #  is used
00051     # force_close_on_insert: closes the popup when the user choses insert. This overrides the behaviour
00052     #   in multiselect.
00053     # show_review_state: show the review state of objects
00054     # search_catalog: the id of an alternate search catalog to use for the query
00055     #   (i.e. member_catalog for CMFMember)
00056     # hide_inaccessible: hides all the items from the list of references for
00057     #   which users do not have the View permission (instead of showing them
00058     #   the login screen)
00059     
00060     security = ClassSecurityInfo()    
00061 
00062     security.declarePublic('getBaseQuery')
00063     def getBaseQuery(self, instance, field):
00064         """Return base query to use for content search"""
00065 
00066         query = self.base_query
00067         if query:
00068             if type(query) is StringType and shasattr(instance, query):
00069                 method = getattr(instance, query)
00070                 results = method()
00071             elif callable(query):
00072                 results = query()
00073             elif isinstance(query,dict):
00074                 results = query
00075         else:
00076             results = {}
00077 
00078         # Add portal type restrictions based on settings in field, if not part
00079         # of original base_query the template tries to do this, but ignores
00080         # allowed_types_method, which should override allowed_types
00081         if not results.has_key('portal_type'):
00082             allowed_types = getattr(field, 'allowed_types', ())
00083             allow_method = getattr(field, 'allowed_types_method', None)
00084             if allow_method is not None:
00085                 meth = getattr(instance, allow_method)
00086                 allowed_types = meth()
00087             results['portal_type']=allowed_types
00088 
00089         return results
00090 
00091     def getStartupDirectory(self, instance):
00092         """ Return the path to the startup directory. """
00093         if self.startup_directory_method:
00094             if getattr(aq_base(instance), self.startup_directory_method, False):
00095                 method = getattr(instance, self.startup_directory_method)
00096                 if callable(method):
00097                     return method()
00098                 return method
00099         return self.startup_directory
00100 
00101 
00102 registerWidget(ReferenceBrowserWidget,
00103                title='Reference Browser',
00104                description=('Reference widget that allows you to browse or search the portal for objects to refer to.'),
00105                used_for=('Products.Archetypes.Field.ReferenceField',)
00106                )
00107 
00108 registerPropertyType('default_search_index', 'string', ReferenceBrowserWidget)
00109 registerPropertyType('show_index_selector', 'boolean', ReferenceBrowserWidget)
00110 registerPropertyType('available_indexes', 'dictionary', ReferenceBrowserWidget)
00111 registerPropertyType('allow_search', 'boolean', ReferenceBrowserWidget)
00112 registerPropertyType('allow_browse', 'boolean', ReferenceBrowserWidget)
00113 registerPropertyType('startup_directory', 'string', ReferenceBrowserWidget)
00114 registerPropertyType('restrict_browsing_to_startup_directory', 'boolean', ReferenceBrowserWidget)
00115 registerPropertyType('search_catalog', 'string', ReferenceBrowserWidget)
00116 registerPropertyType('image_portal_types', 'lines', ReferenceBrowserWidget)
00117 registerPropertyType('image_method', 'string', ReferenceBrowserWidget)
00118 registerPropertyType('force_close_on_insert', 'boolean', ReferenceBrowserWidget)