Created
June 19, 2018 11:23
-
-
Save zopyx/fc4c57c6fdbf04c5f1f275c7483187f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zExceptions | |
from Products.CMFPlone.factory import addPloneSite | |
from Products.Five.browser import BrowserView | |
import plone.api | |
from plone.app.textfield.value import RichTextValue | |
from plone.app.theming.browser.controlpanel import ThemingControlpanel | |
import json | |
import lxml.html | |
class MyThemingControlpanel(ThemingControlpanel): | |
def authorize(self): | |
return True | |
class API(BrowserView): | |
def recreate_plone_site(self): | |
""" Recreate a Plone site """ | |
data = json.loads(self.request.BODY) | |
site_id = str(data['site_id']) | |
extension_ids = data['extension_ids'] | |
root = self.context.restrictedTraverse('/') | |
if site_id in root.objectIds(): | |
print 'Deleting Plone site "{0}"'.format(site_id) | |
root.manage_delObjects([site_id]) | |
print 'Creating Plone site "{0}" with {1}'.format( | |
site_id, extension_ids) | |
addPloneSite(root, site_id, extension_ids=extension_ids) | |
print 'Created Plone site "{0}" with {1}'.format( | |
site_id, extension_ids) | |
self.request.form['form.button.Enable'] = 'DONE' | |
self.request.form['themeName'] = 'barceloneta' | |
view = MyThemingControlpanel(root[site_id], self.request) | |
view.update() | |
self.request.response.setStatus(201) | |
self.request.response.write('Created') | |
def remote_exists(self, path): | |
""" Check if `path` exists based on our own traversal. | |
The purpose of this method is to provide a traversal | |
lookup that is not dependent on Acquisition but on | |
real traversal. | |
E.g. a request to `/plone/papers/conference/papers` would | |
resolve to the first `papers` folder if the second | |
`papers` folder does not exist. | |
""" | |
current = self.context.restrictedTraverse('/') | |
for c in path.split('/'): | |
if not c: | |
continue | |
if c in current.objectIds(): | |
current = current[c] | |
else: | |
raise zExceptions.NotFound(path) | |
self.request.response.setStatus(200) | |
self.request.response.write('FOUND') | |
def setuid(self, uid): | |
""" Set given `uid` on current context object """ | |
from plone.protect.interfaces import IDisableCSRFProtection | |
from zope.interface import alsoProvides | |
alsoProvides(self.request, IDisableCSRFProtection) | |
setattr(self.context, '_plone.uuid', uid) | |
self.context.reindexObject(idxs=['UID']) | |
self.request.response.setStatus(200) | |
def convert_to_uids(self): | |
""" Convert all links inside a RichText field from path to UID """ | |
from plone.protect.interfaces import IDisableCSRFProtection | |
from zope.interface import alsoProvides | |
alsoProvides(self.request, IDisableCSRFProtection) | |
catalog = plone.api.portal.get_tool('portal_catalog') | |
for brain in catalog(): | |
obj = brain.getObject() | |
try: | |
html = obj.text.raw | |
except AttributeError: | |
continue | |
root = lxml.html.fromstring(html) | |
for img in root.xpath('//img') : | |
src = img.attrib['src'] | |
if src.startswith('resolveuid/'): | |
continue | |
src_parts = src.split('/') | |
scale = '' | |
if src_parts[-1] in ('image_preview', 'image_large', 'image_mini', 'image_thumb', 'image_tile', 'image_icon', 'image_listing'): | |
src = '/'.join(src_parts[:-1]) | |
scale = src_parts[-1].replace('image_', '') | |
target = self.context.restrictedTraverse(src, None) | |
if target is not None: | |
img.attrib['src'] = 'resolveuid/{}'.format(target.UID()) | |
class_ = img.attrib.get('class', '') | |
if scale: | |
img.attrib['class'] = 'scale-{} '.format(scale) + class_ | |
html = lxml.html.tostring(root) | |
obj.text = RichTextValue(html, 'text/html', 'text/html') | |
self.request.response.setStatus(200) | |
self.request.response.write('DONE') | |
def set_navigationroot(self): | |
""" Set INavigationRoot on current context object """ | |
from Products.Five.utilities.marker import mark | |
from plone.app.layout.navigation.interfaces import INavigationRoot | |
from plone.protect.interfaces import IDisableCSRFProtection | |
from zope.interface import alsoProvides | |
alsoProvides(self.request, IDisableCSRFProtection) | |
mark(self.context, INavigationRoot) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment