Skip to content

Commit

Permalink
Merge pull request #1693 from laws-africa/cross-site-popups
Browse files Browse the repository at this point in the history
cross-origin popups
  • Loading branch information
longhotsummer authored Jan 22, 2024
2 parents ce690d2 + d6b5960 commit eaac02b
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 121 deletions.
114 changes: 114 additions & 0 deletions peachjam/resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from django.conf import settings


class RedirectResolver:
RESOLVER_MAPPINGS = {
"africanlii": {
"country_code": "aa",
"domain": "africanlii.org",
},
"eswatinilii": {
"country_code": "sz",
"domain": "eswatinilii.org",
},
"ghalii": {
"country_code": "gh",
"domain": "ghalii.org",
},
"lawlibrary": {
"country_code": "za",
"domain": "lawlibrary.org.za",
},
"leslii": {
"country_code": "ls",
"domain": "lesotholii.org",
},
"malawilii": {
"country_code": "mw",
"domain": "malawilii.org",
},
"mauritiuslii": {
"country_code": "mu",
"domain": "mauritiuslii.org",
},
"namiblii": {
"country_code": "na",
"domain": "namiblii.org",
},
"nigerialii": {
"country_code": "ng",
"domain": "nigerialii.org",
},
"open by-laws": {
"place_code": [],
"domain": "openbylaws.org.za",
},
"rwandalii": {
"country_code": "rw",
"domain": "rwandalii.org",
},
"seylii": {
"country_code": "sc",
"domain": "seylii.org",
},
"sierralii": {
"country_code": "sl",
"domain": "sierralii.org",
},
"tanzlii": {
"country_code": "tz",
"domain": "tanzlii.org",
},
"tcilii": {
"country_code": "tc",
"domain": "tcilii.org",
},
"ulii": {
"country_code": "ug",
"domain": "ulii.org",
},
"zambialii": {
"country_code": "zm",
"domain": "zambialii.org",
},
"zanzibarlii": {
"place_code": "tz-znz",
"domain": "zanzibarlii.org",
},
"zimlii": {
"country_code": "zw",
"domain": "zimlii.org",
},
}

def __init__(self, app_name):
self.current_authority = self.RESOLVER_MAPPINGS.get(app_name.lower())

def get_domain_for_frbr_uri(self, parsed_frbr_uri):
best_domain = self.get_best_domain(parsed_frbr_uri)
if self.current_authority and best_domain != self.current_authority["domain"]:
return best_domain
return None

def get_url_for_frbr_uri(self, parsed_frbr_uri, raw_frbr_uri):
domain = self.get_domain_for_frbr_uri(parsed_frbr_uri)
if domain:
return f"https://{domain}{raw_frbr_uri}"

def get_best_domain(self, parsed_uri):
country_code = parsed_uri.country
place_code = parsed_uri.place

if country_code != place_code:
for key, mapping in self.RESOLVER_MAPPINGS.items():
if mapping.get("place_code") == place_code:
return mapping.get("domain")

# if no domain matching with place code is found use country code
for key, mapping in self.RESOLVER_MAPPINGS.items():
if mapping.get("country_code") == country_code:
return mapping.get("domain")
return None


resolver = RedirectResolver(settings.PEACHJAM["APP_NAME"])
6 changes: 6 additions & 0 deletions peachjam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"drf_spectacular",
"django_advanced_password_validation",
"martor",
"corsheaders",
]

MIDDLEWARE = [
Expand All @@ -87,6 +88,7 @@
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
Expand Down Expand Up @@ -607,3 +609,7 @@ def before_send(event, hint):
}
# disable the normal martor theme which pulls in another bootstrap version
MARTOR_ALTERNATIVE_CSS_FILE_THEME = "martor/css/peachjam.css"

# CORS
# disable regex matches, we do matching using signals
CORS_URLS_REGEX = r"^$"
14 changes: 11 additions & 3 deletions peachjam/templates/peachjam/document_popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</la-akoma-ntoso>
{% endblock %}
{% else %}
<div class="mb-2">
<div>
<div>
{% block title %}
<b>{{ document.title }}</b>
Expand All @@ -19,13 +19,21 @@
{% endblock %}
</div>
{% block citation %}
{% if document.citation %}
{% if document.citation and document.citation != document.title %}
<div>
<i>{{ document.citation }}</i>
</div>
{% endif %}
{% endblock %}
{% block date %}<div>{{ document.date }}</div>{% endblock %}
</div>
{% block date %}<div>{{ document.date }}</div>{% endblock %}
{% block offsite_request %}
{% if offsite_request %}
<div class="mt-2">
<a href="https://{{ request.get_host }}{{ document.expression_frbr_uri }}"
target="_blank">{{ APP_NAME }}</a>
</div>
{% endif %}
{% endblock %}
{% endif %}
</div>
2 changes: 1 addition & 1 deletion peachjam/tests/test_resolver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cobalt import FrbrUri
from django.test import TestCase

from peachjam.views.documents import RedirectResolver
from peachjam.resolver import RedirectResolver

urls = [
"/akn/zm/judgment/zmsc/2021/7/eng@2021-01-19",
Expand Down
113 changes: 3 additions & 110 deletions peachjam/views/documents.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from cobalt import FrbrUri
from django.conf import settings
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect, reverse
from django.utils.decorators import method_decorator
Expand All @@ -9,111 +8,7 @@
from peachjam.helpers import add_slash, add_slash_to_frbr_uri
from peachjam.models import CoreDocument
from peachjam.registry import registry


class RedirectResolver:
RESOLVER_MAPPINGS = {
"africanlii": {
"country_code": "aa",
"domain": "africanlii.org",
},
"eswatinilii": {
"country_code": "sz",
"domain": "eswatinilii.org",
},
"ghalii": {
"country_code": "gh",
"domain": "ghalii.org",
},
"lawlibrary": {
"country_code": "za",
"domain": "lawlibrary.org.za",
},
"leslii": {
"country_code": "ls",
"domain": "lesotholii.org",
},
"malawilii": {
"country_code": "mw",
"domain": "malawilii.org",
},
"mauritiuslii": {
"country_code": "mu",
"domain": "mauritiuslii.org",
},
"namiblii": {
"country_code": "na",
"domain": "namiblii.org",
},
"nigerialii": {
"country_code": "ng",
"domain": "nigerialii.org",
},
"open by-laws": {
"place_code": [],
"domain": "openbylaws.org.za",
},
"rwandalii": {
"country_code": "rw",
"domain": "rwandalii.org",
},
"seylii": {
"country_code": "sc",
"domain": "seylii.org",
},
"sierralii": {
"country_code": "sl",
"domain": "sierralii.org",
},
"tanzlii": {
"country_code": "tz",
"domain": "tanzlii.org",
},
"tcilii": {
"country_code": "tc",
"domain": "tcilii.org",
},
"ulii": {
"country_code": "ug",
"domain": "ulii.org",
},
"zambialii": {
"country_code": "zm",
"domain": "zambialii.org",
},
"zanzibarlii": {
"place_code": "tz-znz",
"domain": "zanzibarlii.org",
},
"zimlii": {
"country_code": "zw",
"domain": "zimlii.org",
},
}

def __init__(self, app_name):
self.current_authority = self.RESOLVER_MAPPINGS[app_name.lower()]

def get_domain_for_frbr_uri(self, parsed_uri):
best_domain = self.get_best_domain(parsed_uri)
if best_domain != self.current_authority["domain"]:
return best_domain
return None

def get_best_domain(self, parsed_uri):
country_code = parsed_uri.country
place_code = parsed_uri.place

if country_code != place_code:
for key, mapping in self.RESOLVER_MAPPINGS.items():
if mapping.get("place_code") == place_code:
return mapping.get("domain")

# if no domain matching with place code is found use country code
for key, mapping in self.RESOLVER_MAPPINGS.items():
if mapping.get("country_code") == country_code:
return mapping.get("domain")
return None
from peachjam.resolver import resolver


class DocumentDetailViewResolver(View):
Expand Down Expand Up @@ -145,10 +40,8 @@ def dispatch(self, request, *args, **kwargs):
)

if not obj:
resolver = RedirectResolver(settings.PEACHJAM["APP_NAME"])
domain = resolver.get_domain_for_frbr_uri(parsed_frbr_uri)
if domain:
url = f"https://{domain}{frbr_uri}"
url = resolver.get_url_for_frbr_uri(parsed_frbr_uri, frbr_uri)
if url:
return redirect(url)
raise Http404()

Expand Down
Loading

0 comments on commit eaac02b

Please sign in to comment.