Skip to content

Instantly share code, notes, and snippets.

@miohtama
Created February 1, 2016 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miohtama/75acbfc571fa85c662af to your computer and use it in GitHub Desktop.
Save miohtama/75acbfc571fa85c662af to your computer and use it in GitHub Desktop.
Move Plone portlets left and right between Products.ContentWellPortlets managers
<browser:page
for="*"
name="move_portlet"
permission="cmf.ManagePortal"
class=".views.MovePortlet"
/>
<browser:page
for="*"
name="manage-portlets-macros"
class="plone.app.portlets.browser.manage.ManageContextualPortlets"
template="templates/edit-manager-macros.pt"
permission="plone.app.portlets.ManageOwnPortlets"
layer=".interfaces.IThemeSpecific"
/>
<div metal:define-macro="portlet-add-form" class="section" i18n:domain="plone">
<form method="post" action="#"
tal:define="portlets view/addable_portlets"
tal:condition="portlets"
tal:attributes="action view/context_url">
<input type="hidden" name="referer" tal:attributes="value view/referer" />
<label class="hiddenStructure"
i18n:translate="label_add_portlet">
Add portlet
</label>
<select name=":action" onchange="this.form.submit()">
<option tal:attributes="value python:request['ACTUAL_URL'].replace(view.context_url(), '')"
i18n:translate="label_add_portlet_ellipsis">
Add portlet&hellip;
</option>
<tal:options repeat="portlet portlets">
<option tal:attributes="value string:${portlet/addview}"
tal:content="portlet/title"
i18n:translate="" />
</tal:options>
</select>
<noscript>
<input class="standalone"
type="submit"
value="Add portlet"
i18n:attributes="value label_add_portlet;"/>
</noscript>
</form>
</div>
<div metal:define-macro="current-portlets-list" class="portletAssignments"
i18n:domain="plone">
<h4 i18n:translate="heading_portlets_assigned_here">
Portlets assigned here
</h4>
<div>
<tal:block repeat="portlet view/portlets">
<div tal:define="hiddenPortletClass python:not portlet['visible'] and 'blockedPortlet' or ''"
tal:attributes="class string:managedPortlet portlet ${hiddenPortletClass};
data-portlethash portlet/hash;
data-viewname view/view_name;">
<div class="portletHeader" tal:define="authenticator context/@@authenticator/authenticator">
<span class="managedPortletActions">
<!-- Up -->
<form class="portlet-action up" method="POST" tal:attributes="action portlet/up_url"
tal:condition="not:repeat/portlet/start">
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button title="Move up" i18n:attributes="title" type="submit"
tal:attributes="name string:${portlet/name}-up">&#9650;</button>
</form>
<!-- Left -->
<form class="portlet-action left" method="POST" tal:attributes="action python:portlet['hide_url'].replace('@@toggle-visibility', 'move_portlet')">
<input type="hidden" name="direction" value="left" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button title="Move left" i18n:attributes="title" type="submit"
tal:attributes="name string:${portlet/name}-left">&#9664;</button>
</form>
<!-- Right -->
<form class="portlet-action right" method="POST" tal:attributes="action python:portlet['hide_url'].replace('@@toggle-visibility', 'move_portlet')">
<input type="hidden" name="direction" value="right" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button title="Move right" i18n:attributes="title" type="submit"
tal:attributes="name string:${portlet/name}-right">&#9654;</button>
</form>
<form class="portlet-action down" method="POST" tal:attributes="action portlet/down_url"
tal:condition="not:repeat/portlet/end">
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button title="Move down" i18n:attributes="title" type="submit"
tal:attributes="name string:${portlet/name}-down">&#9660;</button>
</form>
<form class="portlet-action" method="POST" tal:attributes="action portlet/show_url"
tal:condition="not: portlet/visible">
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button type="submit" i18n:translate="label_show_item"
tal:attributes="name string:${portlet/name}-show">Show</button>
</form>
<form class="portlet-action" method="POST" tal:attributes="action portlet/hide_url"
tal:condition="portlet/visible">
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button type="submit" i18n:translate="label_hide_item"
tal:attributes="name string:${portlet/name}-hide">Hide</button>
</form>
<form class="portlet-action delete" method="POST" tal:attributes="action portlet/delete_url">
<input type="hidden" name="referer" tal:attributes="value view/url_quote_referer" />
<input type="hidden" name="name" tal:attributes="value portlet/name" />
<input type="hidden" name="viewname" tal:attributes="value view/view_name" />
<span tal:replace="structure authenticator"/>
<button i18n:attributes="title" title="Remove" type="submit"
tal:attributes="name string:${portlet/name}-remove">&times;</button>
</form>
</span>
<a tal:omit-tag="not:portlet/editview"
tal:attributes="href string:${portlet/editview}?referer=${view/url_quote_referer}"
tal:content="portlet/title" />
</div>
</div>
</tal:block>
</div>
</div>
"""Left and right portlets moving support for Products.ContentWellPortlets."""
import urllib
from Acquisition import aq_inner
from plone.portlets.interfaces import IPortletManager, IPortletAssignmentMapping
from zope.component import getMultiAdapter
from zope.component import getUtility
# Method: Gets the portlet assignment (behaves similar to a dict)
def getPortletAssignmentMapping(context, name):
portlet_manager = getUtility(IPortletManager, name=name, context=context)
return getMultiAdapter((context, portlet_manager), IPortletAssignmentMapping, context=context)
# http://stackoverflow.com/a/23816108/315168
def rfind(li, key):
return max(loc for loc, val in enumerate(li) if val == key)
# The order we shuffle portlet around in different managers
# TODO: Now we implement this for one content wells section only
ORDER = [
"ContentWellPortlets.BelowPortletManager6",
"ContentWellPortlets.BelowPortletManager1",
"ContentWellPortlets.BelowPortletManager2",
"ContentWellPortlets.BelowPortletManager3",
"ContentWellPortlets.BelowPortletManager4",
"ContentWellPortlets.BelowPortletManager6",
"ContentWellPortlets.BelowPortletManager1"]
class MovePortlet:
"""Move portlets between content wells left and right."""
def __init__(self, context, request):
# context is contentwellportlet managre
self.context = context
self.request = request
def __call__(self):
""" Shuffle a portlet to a chosen direction."""
# '++contextportlets++ContentWellPortlets.BelowPortletManager1'
manager_name = self.context.id[self.context.id.rfind("+") + 1:]
# assignment_name = self.context.name()
assignments = aq_inner(self.context)
name = self.request.form["name"]
original = assignments[name]
if self.request.form["direction"] == "left":
next_manager = ORDER[rfind(ORDER, manager_name) - 1]
else:
next_manager = ORDER[ORDER.index(manager_name) + 1]
content_item = self.context.__parent__
sideways_mappings = getPortletAssignmentMapping(content_item, next_manager)
sideways_mappings[name] = original
del assignments[name]
referer = self.request.form["referer"]
referer = urllib.unquote_plus(referer)
self.request.response.redirect(referer)
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment