source: products/qPloneDropDownMenu/tags/0.2.1/utils.py @ 1141

Last change on this file since 1141 was 1140, checked in by piv, 11 years ago

fix string encoding issues, update history file, now ready to be released as 0.2.1 version

  • Property svn:eol-style set to native
File size: 2.0 KB
Line 
1# -*- coding: utf-8 -*-
2""" Utility functions """
3
4from zope.component import getMultiAdapter
5
6from OFS.DTMLMethod import addDTMLMethod
7
8from Products.CMFCore.utils import getToolByName
9from Products.CMFPlone.migrations.migration_util import safeEditProperty
10from Products.CMFCore.Expression import Expression, createExprContext
11
12from config import PROPERTY_FIELD, PROPERTY_SHEET
13
14
15TAB_HTML_SNIPPET = """<li id="portaltab-%(id)s" class="plain">
16  <a href="%(url)s" accesskey="t" title="%(desc)s">%(name)s</a>
17</li>
18"""
19
20
21def addCSS(container, sheetId, title, csshovering):
22    """ Add DTML Method object to portal root """
23    addDTMLMethod(container, sheetId, title, csshovering)
24
25def updateMenu(site):
26    pu = getToolByName(site, 'plone_utils')
27    pa = getToolByName(site, 'portal_actions')
28    portal_props = getToolByName(site, 'portal_properties')
29
30    # collect all portal tabs
31    context_state = getMultiAdapter((site, site.REQUEST),
32                                    name=u'plone_context_state')
33    actions = context_state.actions()
34    portal_tabs_view = getMultiAdapter((site, site.REQUEST),
35                                       name='portal_tabs_view')
36    portal_tabs = portal_tabs_view.topLevelTabs(actions=actions)
37
38    # dump to html
39    value = u""
40    enc = portal_props.site_properties.getProperty('default_charset', 'utf-8')
41    for tab in portal_tabs:
42        value += TAB_HTML_SNIPPET % {'id': toUnicode(tab['id'], enc),
43                                     'url': toUnicode(tab['url'], enc),
44                                     'desc': toUnicode(tab['description'], enc),
45                                     'name': toUnicode(tab['name'], enc)}
46
47    if not hasattr(portal_props.aq_base, PROPERTY_SHEET):
48        portal_props.addPropertySheet(PROPERTY_SHEET,
49                                      'DropDown Menu Properties')
50    ap = getattr(portal_props.aq_base, PROPERTY_SHEET)
51    safeEditProperty(ap, PROPERTY_FIELD, value, 'text')
52
53def toUnicode(value, enc='utf-8'):
54    if isinstance(value, str):
55        return value.decode(enc)
56    else:
57        return value
Note: See TracBrowser for help on using the repository browser.