Skip to content

Commit

Permalink
Merge pull request #2260 from laws-africa/indigo-webhooks
Browse files Browse the repository at this point in the history
indigo ingestors check if they're responsible for an expression FRBR URI
  • Loading branch information
longhotsummer authored Feb 4, 2025
2 parents fe9a3a1 + c209dec commit 5c68ad6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 6 deletions.
42 changes: 36 additions & 6 deletions peachjam/adapters/indigo.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,34 @@ def filter_queryset(self, qs):

return qs

def is_responsible_for(self, expression_frbr_uri):
"""Is this adapter configured to handle this expression FRBR URI? The various filters are applied to the
FRBR URI to check if it matches."""
frbr_uri = FrbrUri.parse(expression_frbr_uri)

if frbr_uri.place not in self.place_codes:
return False

if self.include_doctypes and frbr_uri.doctype not in self.include_doctypes:
return False

if self.exclude_doctypes and frbr_uri.doctype in self.exclude_doctypes:
return False

if self.include_subtypes and frbr_uri.subtype not in self.include_subtypes:
return False

if self.exclude_subtypes and frbr_uri.subtype in self.exclude_subtypes:
return False

if self.include_actors and frbr_uri.actor not in self.include_actors:
return False

if self.exclude_actors and frbr_uri.actor in self.exclude_actors:
return False

return True

def check_for_deleted(self, docs):
uris = {d["expression_frbr_uri"] for d in docs}
for doc in docs:
Expand Down Expand Up @@ -692,13 +720,15 @@ def handle_webhook(self, data):

logger.info(f"Handling webhook {data}")

if data.get("action") == "updated" and data.get("data", {}).get("url"):
update_document(self.ingestor.pk, data["data"]["url"])
url = data.get("data", {}).get("url")
expression_frbr_uri = data.get("data", {}).get("expression_frbr_uri")

if data.get("action") == "deleted" and data.get("data", {}).get(
"expression_frbr_uri"
):
delete_document(self.ingestor.pk, data["data"]["expression_frbr_uri"])
if expression_frbr_uri and self.is_responsible_for(expression_frbr_uri):
if data.get("action") == "updated" and url:
update_document(self.ingestor.pk, url)

if data.get("action") == "deleted":
delete_document(self.ingestor.pk, expression_frbr_uri)

def get_edit_url(self, document):
if self.settings.get("indigo_url"):
Expand Down
71 changes: 71 additions & 0 deletions peachjam/tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from django.test import TestCase

from peachjam.adapters import IndigoAdapter


class IndigoAdapterTest(TestCase):
def setUp(self):
self.adapter = IndigoAdapter(
None,
{"token": "XXX", "api_url": "http://example.com", "places": "za za-cpt"},
)
self.adapter.place_codes = ["za", "za-cpt"]

def test_is_responsible_for_places(self):
self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/2009/1"))
self.assertTrue(self.adapter.is_responsible_for("/akn/za-cpt/act/2009/1"))

self.assertFalse(self.adapter.is_responsible_for("/akn/bw/act/2009/1"))
self.assertFalse(self.adapter.is_responsible_for("/akn/za-xxx/act/2009/1"))

def test_is_responsible_for_include_doctypes(self):
self.adapter.include_doctypes = ["act"]

self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/2009/1"))
self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/by-law/2009/1"))
self.assertFalse(
self.adapter.is_responsible_for("/akn/za/judgment/zahc/1999/1")
)

def test_is_responsible_for_exclude_doctypes(self):
self.adapter.exclude_doctypes = ["act"]

self.assertFalse(self.adapter.is_responsible_for("/akn/za/act/2009/1"))
self.assertFalse(self.adapter.is_responsible_for("/akn/za/act/by-law/2009/1"))
self.assertTrue(self.adapter.is_responsible_for("/akn/za/judgment/zahc/1999/1"))

def test_is_responsible_for_include_subtypes(self):
self.adapter.include_subtypes = ["by-law"]

self.assertFalse(self.adapter.is_responsible_for("/akn/za/act/2009/1"))
self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/by-law/2009/1"))
self.assertFalse(self.adapter.is_responsible_for("/akn/za/act/thing/1999/1"))

def test_is_responsible_for_exclude_subtypes(self):
self.adapter.exclude_subtypes = ["by-law"]

self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/2009/1"))
self.assertFalse(self.adapter.is_responsible_for("/akn/za/act/by-law/2009/1"))
self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/thing/1999/1"))

def test_is_responsible_for_include_actors(self):
self.adapter.include_actors = ["foo"]

self.assertTrue(
self.adapter.is_responsible_for("/akn/za/act/by-law/foo/2009/1")
)
self.assertFalse(self.adapter.is_responsible_for("/akn/za/act/by-law/2009/1"))
self.assertFalse(
self.adapter.is_responsible_for("/akn/za/act/by-law/bar/1999/1")
)

def test_is_responsible_for_exclude_actors(self):
self.adapter.exclude_actors = ["foo"]

self.assertFalse(
self.adapter.is_responsible_for("/akn/za/act/by-law/foo/2009/1")
)
self.assertTrue(self.adapter.is_responsible_for("/akn/za/act/by-law/2009/1"))
self.assertTrue(
self.adapter.is_responsible_for("/akn/za/act/by-law/bar/1999/1")
)

0 comments on commit 5c68ad6

Please sign in to comment.