Skip to content

Commit

Permalink
Run pre-commit.ci
Browse files Browse the repository at this point in the history
  • Loading branch information
tisto committed Sep 22, 2023
1 parent 4d5ddf7 commit 48dfa5a
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 285 deletions.
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Bugfixes:
[buchi]

- Fallback to regular views during traversal to ensure compatibility with
views beeing called with a specific Accept header.
views being called with a specific Accept header.
[buchi]


Expand Down Expand Up @@ -295,7 +295,7 @@ Bugfixes:

- Refactor traversal of REST requests by using a traversal adapter on the site
root instead of a traversal adapter for each REST service. This prevents
REST services from being overriden by other traversal adapters.
REST services from being overridden by other traversal adapters.
[buchi]


Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ allow_origin
allow_methods
A comma separated list of HTTP method names that are allowed by this CORS
policy, e.g. "DELETE,GET,OPTIONS,PATCH,POST,PUT". If not specified, all
methods for which there's a service registerd are allowed.
methods for which there's a service registered are allowed.

allow_credentials
Indicates whether the resource supports user credentials in the request.
Expand Down
1 change: 0 additions & 1 deletion src/plone/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# -*- coding: utf-8 -*-
from plone.rest.service import Service # noqa
6 changes: 3 additions & 3 deletions src/plone/rest/cors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from plone.rest.interfaces import ICORSPolicy
from zope.interface import implementer

from plone.rest.interfaces import ICORSPolicy

# CORS preflight service registry
# A mapping of method -> service_id
_services = {}
Expand All @@ -19,7 +19,7 @@ def lookup_preflight_service_id(method):


@implementer(ICORSPolicy)
class CORSPolicy(object):
class CORSPolicy:
def __init__(self, context, request):
self.context = context
self.request = request
Expand Down
5 changes: 2 additions & 3 deletions src/plone/rest/demo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
from plone.rest import Service

import json

from plone.rest import Service


class BaseService(Service):
def render(self):
Expand Down
30 changes: 14 additions & 16 deletions src/plone/rest/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,30 @@
from plone.app.redirector.interfaces import IRedirectionStorage
except ImportError:
IRedirectionStorage = None
from urllib.parse import quote, unquote

from plone.memoize.instance import memoize
from plone.rest.interfaces import IAPIRequest
from plone.rest.interfaces import ICORSPolicy
from Products.CMFCore.permissions import ManagePortal
from Products.Five.browser import BrowserView
from six.moves import urllib
from six.moves.urllib.parse import quote
from six.moves.urllib.parse import unquote
from zExceptions import NotFound

from plone.rest.interfaces import IAPIRequest, ICORSPolicy

try:
from ZPublisher.HTTPRequest import WSGIRequest

HAS_WSGI = True
except ImportError:
HAS_WSGI = False
from zope.component import adapter
from zope.component import queryMultiAdapter
from zope.component import queryUtility
from zope.component.hooks import getSite

import json
import six
import sys
import traceback

import six
from zope.component import adapter, queryMultiAdapter, queryUtility
from zope.component.hooks import getSite


@adapter(Exception, IAPIRequest)
class ErrorHandling(BrowserView):
Expand Down Expand Up @@ -61,7 +59,7 @@ def render_exception(self, exception):
if six.PY2:
name = name.decode("utf-8")
message = message.decode("utf-8")
result = {u"type": name, u"message": message}
result = {"type": name, "message": message}

policy = queryMultiAdapter((self.context, self.request), ICORSPolicy)
if policy is not None:
Expand All @@ -77,10 +75,10 @@ def render_exception(self, exception):
# NotFound exceptions need special handling because their
# exception message gets turned into HTML by ZPublisher
url = self.request.getURL()
result[u"message"] = u"Resource not found: %s" % url
result["message"] = "Resource not found: %s" % url

if getSecurityManager().checkPermission(ManagePortal, getSite()):
result[u"traceback"] = self.render_traceback(exception)
result["traceback"] = self.render_traceback(exception)

return result

Expand All @@ -101,8 +99,8 @@ def render_traceback(self, exception):
pass
else:
return (
u"ERROR: Another exception happened before we could "
u"render the traceback."
"ERROR: Another exception happened before we could "
"render the traceback."
)

raw = "\n".join(traceback.format_tb(exc_traceback))
Expand Down Expand Up @@ -192,7 +190,7 @@ def attempt_redirect(self):

query_string = self.request.QUERY_STRING
if query_string:
new_path = storage.get("%s?%s" % (old_path, query_string))
new_path = storage.get(f"{old_path}?{query_string}")
# if we matched on the query_string we don't want to include it
# in redirect
if new_path:
Expand Down
4 changes: 2 additions & 2 deletions src/plone/rest/events.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from zope.interface import alsoProvides

from plone.rest.cors import lookup_preflight_service_id
from plone.rest.interfaces import IAPIRequest
from plone.rest.negotiation import lookup_service_id
from zope.interface import alsoProvides


def mark_as_api_request(request, accept):
Expand Down
1 change: 0 additions & 1 deletion src/plone/rest/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from zope.interface import Interface


Expand Down
4 changes: 1 addition & 3 deletions src/plone/rest/negotiation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

# Service registry
# A mapping of method -> type name -> subtype name -> service id
_services = {}
Expand Down Expand Up @@ -42,7 +40,7 @@ def register_service(method, media_type):
"""Register a service for the given request method and media type and
return it's service id.
"""
service_id = u"{}_{}_{}_".format(method, media_type[0], media_type[1])
service_id = f"{method}_{media_type[0]}_{media_type[1]}_"
types = _services.setdefault(method, {})
subtypes = types.setdefault(media_type[0], {})
subtypes[media_type[1]] = service_id
Expand Down
1 change: 0 additions & 1 deletion src/plone/rest/patches.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from plone.rest.interfaces import IAPIRequest


Expand Down
9 changes: 4 additions & 5 deletions src/plone/rest/service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
from plone.rest.interfaces import ICORSPolicy
from plone.rest.interfaces import IService
from zope.component import queryMultiAdapter
from zope.interface import implementer

from plone.rest.interfaces import ICORSPolicy, IService


@implementer(IService)
class Service(object):
class Service:
def __call__(self):
policy = queryMultiAdapter((self.context, self.request), ICORSPolicy)
if policy is not None:
Expand All @@ -29,4 +28,4 @@ def __getattribute__(self, name):
# include credentials
if name == "__roles__" and self.request._rest_cors_preflight:
return ["Anonymous"]
return super(Service, self).__getattribute__(name)
return super().__getattribute__(name)
12 changes: 4 additions & 8 deletions src/plone/rest/testing.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# -*- coding: utf-8 -*-
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.rest.service import Service
from plone.app.testing import FunctionalTesting, IntegrationTesting, PloneSandboxLayer
from plone.testing import z2

from zope.configuration import xmlconfig

from plone.rest.service import Service

class PloneRestLayer(PloneSandboxLayer):

class PloneRestLayer(PloneSandboxLayer):
defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

def setUpZope(self, app, configurationContext):
Expand All @@ -31,7 +27,7 @@ def setUpZope(self, app, configurationContext):

class InternalServerErrorService(Service):
def __call__(self):
from six.moves.urllib.error import HTTPError
from urllib.error import HTTPError

raise HTTPError(
"http://nohost/plone/500-internal-server-error",
Expand Down
1 change: 0 additions & 1 deletion src/plone/rest/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# -*- coding: utf-8 -*-
19 changes: 8 additions & 11 deletions src/plone/rest/tests/test_cors.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# -*- coding: utf-8 -*-
from ZPublisher.pubevents import PubStart
from plone.app.testing import popGlobalRegistry
from plone.app.testing import pushGlobalRegistry
from plone.rest.cors import CORSPolicy
from plone.rest.interfaces import ICORSPolicy
from plone.rest.testing import PLONE_REST_INTEGRATION_TESTING
import unittest

from plone.app.testing import popGlobalRegistry, pushGlobalRegistry
from zExceptions import Unauthorized
from zope.component import provideAdapter
from zope.event import notify
from zope.interface import Interface
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from ZPublisher.pubevents import PubStart

import unittest
from plone.rest.cors import CORSPolicy
from plone.rest.interfaces import ICORSPolicy
from plone.rest.testing import PLONE_REST_INTEGRATION_TESTING


class TestCORSPolicy(unittest.TestCase):

layer = PLONE_REST_INTEGRATION_TESTING

def setUp(self):
Expand Down Expand Up @@ -195,7 +193,6 @@ def test_preflight_cors_sets_status_code_200(self):


class TestCORS(unittest.TestCase):

layer = PLONE_REST_INTEGRATION_TESTING

def setUp(self):
Expand Down Expand Up @@ -231,7 +228,7 @@ def test_simple_cors_gets_processed(self):

def test_preflight_request_without_cors_policy_doesnt_render_service(self):
# "Unregister" the current CORS policy
class NoCORSPolicy(object):
class NoCORSPolicy:
def __new__(cls, context, request):
return None

Expand Down
Loading

0 comments on commit 48dfa5a

Please sign in to comment.