[2763] | 1 | from zope import schema |
---|
| 2 | from zope.component import getMultiAdapter |
---|
| 3 | from zope.formlib import form |
---|
| 4 | from zope.interface import implements |
---|
| 5 | |
---|
| 6 | from plone.app.portlets.portlets import base |
---|
| 7 | from plone.memoize.compress import xhtml_compress |
---|
| 8 | from plone.portlets.interfaces import IPortletDataProvider |
---|
| 9 | |
---|
| 10 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
| 11 | from Products.CMFPlone import PloneMessageFactory as _ |
---|
| 12 | |
---|
[3635] | 13 | from Products.PloneFormGen.interfaces import IPloneFormGenForm |
---|
[2763] | 14 | from plone.app.vocabularies.catalog import SearchableTextSourceBinder |
---|
| 15 | from plone.app.form.widgets.uberselectionwidget import UberSelectionWidget |
---|
| 16 | |
---|
[3635] | 17 | |
---|
[2763] | 18 | class IPFGPortlet(IPortletDataProvider): |
---|
| 19 | |
---|
| 20 | target_form = schema.Choice( |
---|
[3635] | 21 | title=_(u"Target form"), |
---|
| 22 | description=_(u"Find the form which you want to be", |
---|
| 23 | "displayed in portlet."), |
---|
| 24 | required=True, |
---|
| 25 | source=SearchableTextSourceBinder( |
---|
| 26 | {'object_provides': IPloneFormGenForm.__identifier__}, |
---|
| 27 | default_query='path:')) |
---|
[2763] | 28 | |
---|
[3636] | 29 | show_text = schema.Bool( |
---|
| 30 | title=_(u"Show form text"), |
---|
| 31 | description=_(u"If selected, the form prologue and epilogue will " |
---|
| 32 | u"shown in portlet."), |
---|
| 33 | default=False, |
---|
| 34 | required=False) |
---|
[3635] | 35 | |
---|
[3636] | 36 | |
---|
[2763] | 37 | class Assignment(base.Assignment): |
---|
| 38 | implements(IPFGPortlet) |
---|
| 39 | |
---|
[3636] | 40 | show_text = False |
---|
| 41 | |
---|
| 42 | def __init__(self, target_form='', show_text=False): |
---|
[2763] | 43 | self.target_form = target_form |
---|
[3636] | 44 | self.show_text = show_text |
---|
[2763] | 45 | |
---|
| 46 | @property |
---|
| 47 | def title(self): |
---|
| 48 | return _(u"PFG Portlet") |
---|
| 49 | |
---|
[3635] | 50 | |
---|
[2763] | 51 | class Renderer(base.Renderer): |
---|
| 52 | |
---|
| 53 | _template = ViewPageTemplateFile('pfg.pt') |
---|
| 54 | |
---|
| 55 | def __init__(self, *args): |
---|
| 56 | base.Renderer.__init__(self, *args) |
---|
| 57 | self.portal_state = getMultiAdapter((self.context, self.request), |
---|
[3635] | 58 | name=u'plone_portal_state') |
---|
[2763] | 59 | self.portal = self.portal_state.portal() |
---|
| 60 | |
---|
| 61 | def render(self): |
---|
| 62 | return xhtml_compress(self._template()) |
---|
| 63 | |
---|
| 64 | def pfg_object(self): |
---|
| 65 | pfg_path = self.data.target_form |
---|
| 66 | if pfg_path.startswith('/'): |
---|
| 67 | pfg_path = pfg_path[1:] |
---|
| 68 | |
---|
| 69 | return self.portal.restrictedTraverse(pfg_path, default=None) |
---|
| 70 | |
---|
| 71 | def pfgTitle(self): |
---|
[2816] | 72 | form = self.pfg_object() |
---|
| 73 | if form is not None: |
---|
| 74 | return form.Title() |
---|
[2763] | 75 | |
---|
| 76 | def available(self): |
---|
| 77 | return self.pfg_object() and True or False |
---|
| 78 | |
---|
| 79 | def render_form(self): |
---|
[2816] | 80 | form = self.pfg_object() |
---|
| 81 | if form is not None: |
---|
| 82 | form_path = '/'.join(form.getPhysicalPath()[2:]) |
---|
[3635] | 83 | form_view = self.portal.restrictedTraverse( |
---|
| 84 | '%s/@@embedded' % form_path) |
---|
[2816] | 85 | form_view.prefix = 'pfgportlet' |
---|
| 86 | return form_view() |
---|
[2763] | 87 | |
---|
[3635] | 88 | |
---|
[2763] | 89 | class AddForm(base.AddForm): |
---|
| 90 | |
---|
| 91 | form_fields = form.Fields(IPFGPortlet) |
---|
| 92 | form_fields['target_form'].custom_widget = UberSelectionWidget |
---|
| 93 | |
---|
| 94 | label = _(u"Add PFG Portlet") |
---|
| 95 | description = _(u"This portlet displays pfg content.") |
---|
| 96 | |
---|
| 97 | def create(self, data): |
---|
[3636] | 98 | return Assignment(target_form=data.get('target_form', ''), |
---|
| 99 | show_text=data.get('show_text', False)) |
---|
[2763] | 100 | |
---|
[3635] | 101 | |
---|
[2763] | 102 | class EditForm(base.EditForm): |
---|
| 103 | |
---|
| 104 | form_fields = form.Fields(IPFGPortlet) |
---|
| 105 | form_fields['target_form'].custom_widget = UberSelectionWidget |
---|
| 106 | |
---|
| 107 | label = _(u"Edit PFG Portlet") |
---|
| 108 | description = _(u"This portlet displays pfg content.") |
---|