Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mr tango/context state #433

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
/pip-selfcheck.json
/_build/
/.tox/
/.vscode/settings.json
14 changes: 14 additions & 0 deletions docs/api/context.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.. admonition:: GitHub-only

WARNING: If you are reading this on GitHub, DON'T!
Read the documentation at `docs.plone.org <http://docs.plone.org/develop/plone.api/docs/api/context.html>`_
so you have working references and proper formatting.


.. _plone-api-context:

plone.api.context
=================

.. automodule:: plone.api.context
:members:
60 changes: 60 additions & 0 deletions docs/context.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.. admonition:: GitHub-only

WARNING: If you are reading this on GitHub, DON'T!
Read the documentation at `docs.plone.org <http://docs.plone.org/develop/plone.api/docs/content.html>`_
so you have working references and proper formatting.


.. module:: plone

.. _chapter_context:

=======
Context
=======

.. _context_current_page_url_example:

Get current page url
====================

The URL to the current page, including template and query string:

.. code-block:: python

from plone import api
url = api.context.current_page_url(context, request)


.. _context_view_template_id_example:

Get view template id
====================

.. invisible-code-block: python

api.content.create(container=about, type='Document', id='contact')

To get a :class:`BrowserView` for your content, use :meth:`api.content.get_view`.
The current id of the view-template, use :meth:`api.context.view_template_id`.

.. code-block:: python

from plone import api
contact_page = api.content.get('/contact')
id = api.context.view_template_id(
context=contact_page,
request=request,
)

.. invisible-code-block: python

self.assertEqual(id, u'template-document_view')




Further reading
===============

For more information on possible flags and usage options please see the full :ref:`plone-api-context` specification.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Narrative documentation
about.rst
portal.rst
content.rst
context.rst
user.rst
group.rst
env.rst
Expand Down
2 changes: 2 additions & 0 deletions news/433.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add new context module to give easy access to often used helpers from plone_context_stae view
[MrTango]
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ exclude =
bootstrap-buildout.py,

ignore =
W503,
C812,
E501,
C815
max-line-length = 88

[isort]
force_alphabetical_sort=True
Expand Down
79 changes: 79 additions & 0 deletions src/plone/api/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
"""Module that provides information on current context."""

from copy import copy as _copy
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution
from pkg_resources import parse_version
from plone.api import portal
from plone.api.exc import InvalidParameterError
from plone.api.validation import at_least_one_of
from plone.api.validation import mutually_exclusive_parameters
from plone.api.validation import required_parameters
from plone.app.linkintegrity.exceptions import (
LinkIntegrityNotificationException,
) # noqa
from plone.app.uuid.utils import uuidToObject
from plone.uuid.interfaces import IUUID
from Products.CMFCore.WorkflowCore import WorkflowException
from zope.component import getMultiAdapter
from zope.component import getSiteManager
from zope.container.interfaces import INameChooser
from zope.interface import Interface
from zope.interface import providedBy


def get_portal_state(context, request):
return getMultiAdapter((context, request), name="plone_context_state")


@required_parameters("context", "request")
def view_template_id(context=None, request=None, **kwargs): # NOQA: C816
"""The id of the view template of the context.

:param context: [required] Context on which to get view.
:type context: context object
:param request: [required] Request on which to get view.
:type request: request object
:raises:
:class:`~plone.api.exc.MissingParameterError`,
:class:`~plone.api.exc.InvalidParameterError`
:Example: :ref:`context_view_template_id_example`
"""
pstate = get_portal_state(context, request)
return pstate.view_template_id()


@required_parameters("context", "request")
def current_page_url(context=None, request=None, **kwargs): # NOQA: C816
"""The URL to the current page, including template and query string.

:param context: [required] Context on which to get view.
:type context: context object
:param request: [required] Request on which to get view.
:type request: request object
:raises:
:class:`~plone.api.exc.MissingParameterError`,
:class:`~plone.api.exc.InvalidParameterError`
:Example: :ref:`context_current_page_url_example`
"""
pstate = get_portal_state(context, request)
return pstate.current_page_url()


@required_parameters("context", "request")
def current_base_url(context=None, request=None, **kwargs): # NOQA: C816
"""The current "actual" URL from the request, excluding the query
string.

:param context: [required] Context on which to get view.
:type context: context object
:param request: [required] Request on which to get view.
:type request: request object
:raises:
:class:`~plone.api.exc.MissingParameterError`,
:class:`~plone.api.exc.InvalidParameterError`
:Example: :ref:`context_current_base_url_example`
"""
pstate = get_portal_state(context, request)
return pstate.current_base_url()