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 | |
---|
20 | def getDropDownMenuSettings(context): |
---|
21 | """Return dropdown menu settings""" |
---|
22 | registry = getUtility(IRegistry) |
---|
23 | return DropDownMenuRecordsProxy(context, registry, IDropDownMenuSettings, |
---|
24 | omitted=()) |
---|
25 | |
---|
26 | |
---|
27 | class DropDownMenuRecordsProxy(RecordsProxy): |
---|
28 | """Override a few dropdown menu specific settings to get from portal |
---|
29 | properties instead of taking them from plone.registry components""" |
---|
30 | |
---|
31 | def __init__(self, context, registry, schema, omitted=()): |
---|
32 | # override initialization to pass context for further purposes; |
---|
33 | # yes, I know, this is badly to depend on context in utilities, |
---|
34 | # but I haven't come up with some better way to proxy |
---|
35 | # plone.registry yet skip __setattr__ |
---|
36 | self.__dict__['__context__'] = context |
---|
37 | super(DropDownMenuRecordsProxy, self).__init__(registry, schema, |
---|
38 | omitted) |
---|
39 | |
---|
40 | def __getattr__(self, name): |
---|
41 | if name not in self.__schema__: |
---|
42 | raise AttributeError(name) |
---|
43 | value = _marker |
---|
44 | if name in ('show_content_tabs', 'show_nonfolderish_tabs'): |
---|
45 | context = self.__context__ |
---|
46 | prop_tool = getToolByName(context, 'portal_properties') |
---|
47 | if 'site_properties' in prop_tool.objectIds(): |
---|
48 | sheet = prop_tool._getOb('site_properties') |
---|
49 | if sheet.hasProperty(PROP_MAPPING[name]): |
---|
50 | value = not sheet.getProperty(PROP_MAPPING[name]) |
---|
51 | else: |
---|
52 | value = self.__registry__.get(self.__prefix__ + name, _marker) |
---|
53 | if value is _marker: |
---|
54 | value = self.__schema__[name].missing_value |
---|
55 | return value |
---|
56 | |
---|
57 | def __repr__(self): |
---|
58 | return "<DropDownMenuRecordsProxy for %s>" % \ |
---|
59 | self.__schema__.__identifier__ |
---|