1 | # -*- coding: utf-8 -*- |
---|
2 | # Miscellaneous functions to use from inside different parts of this package. |
---|
3 | |
---|
4 | from zope.component import getUtility |
---|
5 | |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | |
---|
8 | from plone.registry.recordsproxy import RecordsProxy, _marker |
---|
9 | from plone.registry.interfaces import IRegistry |
---|
10 | |
---|
11 | from quintagroup.dropdownmenu.interfaces import IDropDownMenuSettings |
---|
12 | |
---|
13 | |
---|
14 | PROP_MAPPING = { |
---|
15 | 'show_content_tabs': 'disable_folder_sections', |
---|
16 | 'show_nonfolderish_tabs': 'disable_nonfolderish_sections', |
---|
17 | } |
---|
18 | |
---|
19 | def getDropDownMenuSettings(context): |
---|
20 | """Return dropdown menu settings""" |
---|
21 | registry = getUtility(IRegistry) |
---|
22 | return DropDownMenuRecordsProxy(context, registry, IDropDownMenuSettings, |
---|
23 | omitted=()) |
---|
24 | |
---|
25 | class DropDownMenuRecordsProxy(RecordsProxy): |
---|
26 | """Override a few dropdown menu specific settings to get from portal |
---|
27 | properties instead of taking them from plone.registry components""" |
---|
28 | |
---|
29 | def __init__(self, context, registry, schema, omitted=()): |
---|
30 | # override initialization to pass context for further purposes; |
---|
31 | # yes, I know, this is badly to depend on context in utilities, |
---|
32 | # but I haven't come up with some better way to proxy plone.registry yet |
---|
33 | # skip __setattr__ |
---|
34 | self.__dict__['__context__'] = context |
---|
35 | super(DropDownMenuRecordsProxy, self).__init__(registry, schema, |
---|
36 | omitted) |
---|
37 | |
---|
38 | def __getattr__(self, name): |
---|
39 | if name not in self.__schema__: |
---|
40 | raise AttributeError(name) |
---|
41 | value = _marker |
---|
42 | if name in ('show_content_tabs', 'show_nonfolderish_tabs'): |
---|
43 | context = self.__context__ |
---|
44 | prop_tool = getToolByName(context, 'portal_properties') |
---|
45 | if 'site_properties' in prop_tool.objectIds(): |
---|
46 | sheet = prop_tool._getOb('site_properties') |
---|
47 | if sheet.hasProperty(PROP_MAPPING[name]): |
---|
48 | value = not sheet.getProperty(PROP_MAPPING[name]) |
---|
49 | else: |
---|
50 | value = self.__registry__.get(self.__prefix__ + name, _marker) |
---|
51 | if value is _marker: |
---|
52 | value = self.__schema__[name].missing_value |
---|
53 | return value |
---|
54 | |
---|
55 | def __repr__(self): |
---|
56 | return "<DropDownMenuRecordsProxy for %s>" % \ |
---|
57 | self.__schema__.__identifier__ |
---|