source: products/quintagroup.portlet.static/trunk/quintagroup/portlet/static/staticstylishportlet.py @ 3282

Last change on this file since 3282 was 3282, checked in by vmaksymiv, 13 years ago

Added option to display portlet for anonymous users only

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1from zope.interface import implements
2
3from plone.portlets.interfaces import IPortletDataProvider
4from plone.app.portlets.portlets import base
5from plone.portlet.static import static
6from plone.app.form.widgets.wysiwygwidget import WYSIWYGWidget
7
8from zope import schema
9from zope.formlib import form
10from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
11from Products.CMFCore.utils import getToolByName
12from Acquisition import aq_inner
13
14from quintagroup.portlet.static.utils import getVocabulary
15from quintagroup.portlet.static import StaticStylishPortletMessageFactory as _
16
17
18class IStaticStylishPortlet(static.IStaticPortlet):
19    """A portlet
20
21    It inherits from IPortletDataProvider because for this portlet, the
22    data that is being rendered and the portlet assignment itself are the
23    same.
24    """
25
26    anonymous_only = schema.Bool(
27        title=_(u"Anonymous only"),
28        description=_(u"Check this to hide the portlet for logged-in users."),
29        required=False,
30        default=False,
31        )
32
33    styling = schema.Choice(title=_(u"Portlet style"),
34                            description=_(u"Choose a css style for the porlet. "
35                                          "You can manage these entries from the plone control panel."),
36                            required=False,
37                            default='',
38                            vocabulary='quintagroup.portlet.static.vocabularies.PortletCSSVocabulary',)
39
40
41class Assignment(static.Assignment):
42    """Portlet assignment.
43
44    This is what is actually managed through the portlets UI and associated
45    with columns.
46    """
47
48    implements(IStaticStylishPortlet)
49
50    styling = ''
51
52    anonymous_only = False
53
54    def __init__(self, header=u"", text=u"", omit_border=False, footer=u"",
55                 more_url='', hide=False, styling='', anonymous_only=False):
56        super(Assignment, self).__init__(header=header, text=text, omit_border=omit_border, footer=footer,
57                                         more_url=more_url)
58
59        self.styling = styling
60        self.anonymous_only = anonymous_only
61
62class Renderer(static.Renderer):
63    """Portlet renderer.
64
65    This is registered in configure.zcml. The referenced page template is
66    rendered, and the implicit variable 'view' will refer to an instance
67    of this class. Other methods can be added and referenced in the template.
68    """
69
70    render = ViewPageTemplateFile('staticstylishportlet.pt')
71
72    @property
73    def available(self):
74        if self.data.anonymous_only:
75            context = aq_inner(self.context)
76            mtool = getToolByName(context, 'portal_membership')
77            return mtool.isAnonymousUser()
78
79        return True
80
81
82class AddForm(static.AddForm):
83    """Portlet add form.
84
85    This is registered in configure.zcml. The form_fields variable tells
86    zope.formlib which fields to display. The create() method actually
87    constructs the assignment that is being added.
88    """
89    form_fields = form.Fields(IStaticStylishPortlet)
90    form_fields['text'].custom_widget = WYSIWYGWidget
91
92    label = _(u"title_add_staticstylish_portlet", default=u"Add Static Stylish text portlet")
93    description = _(u"description_staticstylish_portlet", default=u"A portlet which can display static HTML text with different styles.")
94
95    def create(self, data):
96        return Assignment(**data)
97
98
99class EditForm(static.EditForm):
100    """Portlet edit form.
101
102    This is registered with configure.zcml. The form_fields variable tells
103    zope.formlib which fields to display.
104    """
105    form_fields = form.Fields(IStaticStylishPortlet)
106    form_fields['text'].custom_widget = WYSIWYGWidget
107
108    label = _(u"title_edit_staticstylish_portlet", default=u"Edit Static Stylish text portlet")
109    description = _(u"description_staticstylish_portlet", default=u"A portlet which can display static HTML text with different styles.")
Note: See TracBrowser for help on using the repository browser.