Back to index

plone3  3.1.7
static.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getUtility
00003 
00004 from plone.portlets.interfaces import IPortletDataProvider
00005 from plone.app.portlets.portlets import base
00006 
00007 from plone.i18n.normalizer.interfaces import IIDNormalizer
00008 
00009 from zope import schema
00010 from zope.formlib import form
00011 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00012 
00013 from plone.portlet.static import PloneMessageFactory as _
00014 
00015 from plone.app.form.widgets.wysiwygwidget import WYSIWYGWidget
00016 
00017 
00018 class IStaticPortlet(IPortletDataProvider):
00019     """A portlet which renders predefined static HTML.
00020 
00021     It inherits from IPortletDataProvider because for this portlet, the
00022     data that is being rendered and the portlet assignment itself are the
00023     same.
00024     """
00025 
00026     header = schema.TextLine(
00027         title=_(u"Portlet header"),
00028         description=_(u"Title of the rendered portlet"),
00029         required=True)
00030 
00031     text = schema.Text(
00032         title=_(u"Text"),
00033         description=_(u"The text to render"),
00034         required=True)
00035 
00036     omit_border = schema.Bool(
00037         title=_(u"Omit portlet border"),
00038         description=_(u"Tick this box if you want to render the text above "
00039                       "without the standard header, border or footer."),
00040         required=True,
00041         default=False)
00042 
00043     footer = schema.TextLine(
00044         title=_(u"Portlet footer"),
00045         description=_(u"Text to be shown in the footer"),
00046         required=False)
00047 
00048     more_url = schema.ASCIILine(
00049         title=_(u"Details link"),
00050         description=_(u"If given, the header and footer "
00051                       "will link to this URL."),
00052         required=False)
00053 
00054     hide = schema.Bool(
00055         title=_(u"Hide portlet"),
00056         description=_(u"Tick this box if you want to temporarily hide "
00057                       "the portlet without losing your text."),
00058         required=True,
00059         default=False)
00060 
00061 
00062 class Assignment(base.Assignment):
00063     """Portlet assignment.
00064 
00065     This is what is actually managed through the portlets UI and associated
00066     with columns.
00067     """
00068 
00069     implements(IStaticPortlet)
00070 
00071     header = _(u"title_static_portlet", default=u"Static text portlet")
00072     text = u""
00073     omit_border = False
00074     footer = u""
00075     more_url = ''
00076     hide = False
00077 
00078     def __init__(self, header=u"", text=u"", omit_border=False, footer=u"",
00079                  more_url='', hide=False):
00080         self.header = header
00081         self.text = text
00082         self.omit_border = omit_border
00083         self.footer = footer
00084         self.more_url = more_url
00085         self.hide = hide
00086 
00087     @property
00088     def title(self):
00089         """This property is used to give the title of the portlet in the
00090         "manage portlets" screen. Here, we use the title that the user gave.
00091         """
00092         return self.header
00093 
00094 
00095 class Renderer(base.Renderer):
00096     """Portlet renderer.
00097 
00098     This is registered in configure.zcml. The referenced page template is
00099     rendered, and the implicit variable 'view' will refer to an instance
00100     of this class. Other methods can be added and referenced in the template.
00101     """
00102 
00103     render = ViewPageTemplateFile('static.pt')
00104 
00105     @property
00106     def available(self):
00107         return not self.data.hide
00108 
00109     def css_class(self):
00110         """Generate a CSS class from the portlet header
00111         """
00112         header = self.data.header
00113         normalizer = getUtility(IIDNormalizer)
00114         return "portlet-static-%s" % normalizer.normalize(header)
00115 
00116     def has_link(self):
00117         return bool(self.data.more_url)
00118 
00119     def has_footer(self):
00120         return bool(self.data.footer)
00121 
00122 
00123 class AddForm(base.AddForm):
00124     """Portlet add form.
00125 
00126     This is registered in configure.zcml. The form_fields variable tells
00127     zope.formlib which fields to display. The create() method actually
00128     constructs the assignment that is being added.
00129     """
00130     form_fields = form.Fields(IStaticPortlet)
00131     form_fields['text'].custom_widget = WYSIWYGWidget
00132     label = _(u"title_add_static_portlet",
00133               default=u"Add static text portlet")
00134     description = _(u"description_static_portlet",
00135                     default=u"A portlet which can display static HTML text.")
00136 
00137     def create(self, data):
00138         return Assignment(**data)
00139 
00140 
00141 class EditForm(base.EditForm):
00142     """Portlet edit form.
00143 
00144     This is registered with configure.zcml. The form_fields variable tells
00145     zope.formlib which fields to display.
00146     """
00147     form_fields = form.Fields(IStaticPortlet)
00148     form_fields['text'].custom_widget = WYSIWYGWidget
00149     label = _(u"title_edit_static_portlet",
00150               default=u"Edit static text portlet")
00151     description = _(u"description_static_portlet",
00152                     default=u"A portlet which can display static HTML text.")