Skip to content

Commit

Permalink
initialize abc.ABCMeta class using six #8
Browse files Browse the repository at this point in the history
  • Loading branch information
cahytinne committed May 22, 2017
1 parent f90fffa commit 5636f47
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 33 deletions.
11 changes: 0 additions & 11 deletions pyramid_urireferencer/protected_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from pyramid.httpexceptions import (
HTTPInternalServerError,
HTTPNotImplemented,
HTTPConflict)
from webob import Response

Expand All @@ -33,16 +32,6 @@ def protected_operation(fn):
def advice(parent_object, *args, **kw):
referencer = pyramid_urireferencer.get_referencer(parent_object.request.registry)
uri = referencer.get_uri(parent_object.request)
if uri is None:
if parent_object.request.headers.get("Accept", None) == "application/json":
return Response(
status='501 Not Implemented',
json_body={"message": "Unable to retrieve the uri."},
content_type='application/json'
)
else:
log.error("Urireferencer: Unable to retrieve the uri.")
raise HTTPNotImplemented(detail="Urireferencer: Unable to retrieve the uri.")
registery_response = referencer.is_referenced(uri)
if registery_response.has_references:
if parent_object.request.headers.get("Accept", None) == "application/json":
Expand Down
5 changes: 3 additions & 2 deletions pyramid_urireferencer/referencer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import abc
import six
import requests

import logging
Expand All @@ -9,6 +10,7 @@
log = logging.getLogger(__name__)


@six.add_metaclass(abc.ABCMeta)
class AbstractReferencer:
"""
This is an abstract class that defines what a Referencer needs to be able to handle.
Expand All @@ -20,7 +22,6 @@ class AbstractReferencer:
* Check if a certain uri is being used in another application by query
a central registry.
"""
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def get_uri(self, request):
Expand Down Expand Up @@ -55,12 +56,12 @@ def is_referenced(self, uri):
"""


@six.add_metaclass(abc.ABCMeta)
class Referencer(AbstractReferencer):
"""
This is an implementation of the :class:`AbstractReferencer` that adds a
generic :meth:`is_referenced` method and a plain :meth:`references` method..
"""
__metaclass__ = abc.ABCMeta

def __init__(self, registry_url, **kwargs):
"""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests==2.9.1
pyramid==1.6.1
six==1.10.0
12 changes: 0 additions & 12 deletions tests/test_protected_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,3 @@ def test_protected_operation_500_json(self, is_referenced_mock):

is_referenced_call = is_referenced_mock.mock_calls[0]
self.assertEqual('https://id.erfgoed.net/resources/1', is_referenced_call[1][0])

def test_protected_operation_implementation_error(self):
dummy = DummyParentError()
self.assertRaises(HTTPNotImplemented, dummy.protected_dummy)

dummy.request.headers = {"Accept": "application/json"}
res = dummy.protected_dummy()
self.assertEqual(501, res.status_code)
self.assertEqual(res.json_body["message"],
"Unable to retrieve the uri.")
self.assertEqual("application/json", res.content_type)

8 changes: 0 additions & 8 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,3 @@ def get_uri(self, request):
return 'https://id.erfgoed.net/resources/1'


class TestReferencerError(Referencer):
def references(self, uri, request):
return None

def get_uri(self, request):
return None


0 comments on commit 5636f47

Please sign in to comment.