From 42c724d05a7a8a87ad22df37c9df601b669df989 Mon Sep 17 00:00:00 2001 From: MrTango Date: Sat, 31 Aug 2019 16:50:40 +0300 Subject: [PATCH 1/3] Add context module with easy access to plone_context_state view helpers --- .gitignore | 1 + docs/api/context.rst | 14 +++++++ docs/context.rst | 60 ++++++++++++++++++++++++++++++ docs/index.rst | 1 + src/plone/api/context.py | 79 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 docs/api/context.rst create mode 100644 docs/context.rst create mode 100644 src/plone/api/context.py diff --git a/.gitignore b/.gitignore index a2f78d5c..5da757bd 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ /pip-selfcheck.json /_build/ /.tox/ +/.vscode/settings.json diff --git a/docs/api/context.rst b/docs/api/context.rst new file mode 100644 index 00000000..ca838245 --- /dev/null +++ b/docs/api/context.rst @@ -0,0 +1,14 @@ +.. admonition:: GitHub-only + + WARNING: If you are reading this on GitHub, DON'T! + Read the documentation at `docs.plone.org `_ + so you have working references and proper formatting. + + +.. _plone-api-context: + +plone.api.context +================= + +.. automodule:: plone.api.context + :members: diff --git a/docs/context.rst b/docs/context.rst new file mode 100644 index 00000000..d48ca4f8 --- /dev/null +++ b/docs/context.rst @@ -0,0 +1,60 @@ +.. admonition:: GitHub-only + + WARNING: If you are reading this on GitHub, DON'T! + Read the documentation at `docs.plone.org `_ + 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. diff --git a/docs/index.rst b/docs/index.rst index abf82011..c012974a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -41,6 +41,7 @@ Narrative documentation about.rst portal.rst content.rst + context.rst user.rst group.rst env.rst diff --git a/src/plone/api/context.py b/src/plone/api/context.py new file mode 100644 index 00000000..0d0e7f14 --- /dev/null +++ b/src/plone/api/context.py @@ -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() \ No newline at end of file From de242bf296fa2a1900babc3e29977d769c1c0011 Mon Sep 17 00:00:00 2001 From: MrTango Date: Sat, 31 Aug 2019 16:51:16 +0300 Subject: [PATCH 2/3] tune flake8 settings in setup.cfg to be compatible with black --- setup.cfg | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/setup.cfg b/setup.cfg index b2ecb8d6..7dc524e8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,6 +8,11 @@ exclude = bootstrap-buildout.py, ignore = + W503, + C812, + E501, + C815 +max-line-length = 88 [isort] force_alphabetical_sort=True From 761af35fe4f935247ec6addd42da7a7e85e0139f Mon Sep 17 00:00:00 2001 From: MrTango Date: Sat, 31 Aug 2019 16:57:45 +0300 Subject: [PATCH 3/3] Add changelog entry --- news/433.feature | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 news/433.feature diff --git a/news/433.feature b/news/433.feature new file mode 100644 index 00000000..20be9c94 --- /dev/null +++ b/news/433.feature @@ -0,0 +1,2 @@ +Add new context module to give easy access to often used helpers from plone_context_stae view +[MrTango] \ No newline at end of file