Skip to content

Commit

Permalink
referencer.get_uri #8
Browse files Browse the repository at this point in the history
  • Loading branch information
cahytinne committed May 22, 2017
1 parent e542f42 commit 8600ea0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 1 addition & 2 deletions pyramid_urireferencer/protected_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def protected_operation(fn):
The parent_object must contain:
* a request
* with a registry.queryUtility(IReferencer)
* a function named `urireferencer_get_uri` to retrieve the uri
:raises pyramid.httpexceptions.HTTPConflict: Signals that we don't want to
delete a certain URI because it's still in use somewhere else.
:raises pyramid.httpexceptions.HTTPInternalServerError: Raised when we were
Expand All @@ -32,7 +31,7 @@ def protected_operation(fn):

def advice(parent_object, *args, **kw):
referencer = pyramid_urireferencer.get_referencer(parent_object.request.registry)
uri = parent_object.urireferencer_get_uri()
uri = referencer.get_uri(parent_object.request)
registery_response = referencer.is_referenced(uri)
if registery_response.has_references:
if parent_object.request.headers.get("Accept", None) == "application/json":
Expand Down
15 changes: 13 additions & 2 deletions pyramid_urireferencer/referencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ class AbstractReferencer:
"""
This is an abstract class that defines what a Referencer needs to be able to handle.
It does two things:
It does three things:
* Extract a uri from the request. This is the uri that needs to be checked.
* Check if a uri is being used in this application and report on this.
* 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):
"""
This method extracts a uri from the request. This is the uri that needs to be checked.
:param request: :class:`pyramid.request.Request` with useful configuration information and connections
of the application (registry, route_url, session) to determine the references
:rtype: string uri: URI of the resource we need to check for
"""

@abc.abstractmethod
def references(self, uri, request):
"""
Expand Down
1 change: 0 additions & 1 deletion tests/test_protected_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def __init__(self):
}
config.include('pyramid_urireferencer')

self.urireferencer_get_uri = lambda: 'https://id.erfgoed.net/resources/1'

@protected_operation
def protected_dummy(self):
Expand Down
8 changes: 6 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_is_referenced(self):
reg_response_success_ref1 = registry_adapter(RegistryResponse(uri, True, False, 0, []), {})

referencer = TestReferencer(url)
self.assertIsNone(referencer.references(uri))
self.assertIsNone(referencer.references(uri, 'test'))
response = referencer.is_referenced(uri)
self.assertIsInstance(response, RegistryResponse)
self.assertEqual(response.success, False)
Expand All @@ -66,5 +66,9 @@ def test_is_referenced(self):


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

def get_uri(self, request):
return 'https://id.erfgoed.net/resources/1'

0 comments on commit 8600ea0

Please sign in to comment.