Skip to main content

Named instance script run hooks for plone.recipe.zope2instance

Project description

collective.runhook

Named instance script run hooks for plone.recipe.zope2instance.

Implement your run hook in your package as a named Python function accepting context and request as its arguments:

def whoami(context, request):
    from AccessControl.SecurityManagement import getSecurityManager
    user = getSecurityManager().getUser()

    from pprint import pprint
    pprint({
        'context': context.__repr__(),
        'user': user.__repr__(),
        'getId': user.getId(),
        'getUserName': user.getUserName(),
        'getRoles': user.getRoles(),
        'getRolesInContext': user.getRolesInContext(context)
    })

Remember to include transaction commit when you want to modify the database:

import transaction
transaction commit

Register your function as a named hook for collective.runhook in your packages setup.py as a setuptools entrypoint:

from setuptools import setup

setup(
    ...
    entry_points="""
    # -*- Entry points: -*-
    ...
    [collective.runhook]
    whoami = my.package:whoami
    """
)

Add collective.runhook as a dependency of your package, or include it in your buildout’s instance part:

[buildout]
parts = instance
...

[instance]
recipe = plone.recipe.zope2instance
...
eggs =
    Plone
    ...
    collective.runhook

Run the buildout and execute your hook as you wish:

$ bin/instance runhook whoami
...
{'context': '<Application at >',
 'getId': None,
 'getRoles': ('manage', 'Authenticated'),
 'getRolesInContext': ['manage', 'Authenticated'],
 'getUserName': 'System Processes',
 'user': "<UnrestrictedUser 'System Processes'>"}

collective.runhook obeys the same instance script arguments as the run command:

$ bin/instance -OPlone runhook whoami
...
{'context': '<PloneSite at /Plone>',
 'getId': None,
 'getRoles': ('manage', 'Authenticated'),
 'getRolesInContext': ['manage', 'Authenticated'],
 'getUserName': 'System Processes',
 'user': "<UnrestrictedUser 'System Processes'>"}

As a bonus, collective.runhook can authenticate the hook as a user given as ZOPE_USER environment variable:

$ ZOPE_USER=datakurre bin/instance -OPlone runhook whoami
...
{'context': '<PloneSite at /Plone>',
 'getId': 'datakurre',
 'getRoles': ['Member', 'Reviewer', 'Site Administrator', 'Authenticated'],
 'getRolesInContext': ['Member',
                       'Reviewer',
                       'Site Administrator',
                       'Authenticated'],
 'getUserName': 'datakurre',
 'user': "<PloneUser 'datakurre'>"}

Changelog

0.9.0 (2014-09-24)

  • First release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

collective.runhook-0.9.0.zip (14.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page