-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [maykinmedia/django-setup-configuration#1] add configuration steps
- Loading branch information
1 parent
08b9b9b
commit 0de103d
Showing
6 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from django.conf import settings | ||
from django.urls import reverse | ||
|
||
import requests | ||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
|
||
from objecttypes.token.models import TokenAuth | ||
from objecttypes.utils import build_absolute_url | ||
|
||
|
||
class ObjectsAuthStep(BaseConfigurationStep): | ||
""" | ||
Configure credentials for Objects API to request Objecttypes API | ||
""" | ||
|
||
verbose_name = "Objects API Authentication Configuration" | ||
required_settings = [ | ||
"OBJECTS_OBJECTTYPES_TOKEN", | ||
"OBJECTS_OBJECTTYPES_PERSON", | ||
"OBJECTS_OBJECTTYPES_EMAIL", | ||
] | ||
enable_setting = "OBJECTS_OBJECTTYPES_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
return TokenAuth.objects.filter( | ||
token=settings.OBJECTS_OBJECTTYPES_TOKEN | ||
).exists() | ||
|
||
def configure(self): | ||
token_auth, created = TokenAuth.objects.get_or_create( | ||
token=settings.OBJECTS_OBJECTTYPES_TOKEN, | ||
defaults={ | ||
"contact_person": settings.OBJECTS_OBJECTTYPES_PERSON, | ||
"email": settings.OBJECTS_OBJECTTYPES_EMAIL, | ||
}, | ||
) | ||
if ( | ||
token_auth.contact_person != settings.OBJECTS_OBJECTTYPES_PERSON | ||
or token_auth.email != settings.OBJECTS_OBJECTTYPES_EMAIL | ||
): | ||
token_auth.contact_person = settings.OBJECTS_OBJECTTYPES_PERSON | ||
token_auth.email = settings.OBJECTS_OBJECTTYPES_EMAIL | ||
token_auth.save(update_fields=["contact_person", "email"]) | ||
|
||
def test_configuration(self): | ||
""" | ||
This check depends on the configuration of permissions in Open Zaak | ||
""" | ||
endpoint = reverse("v2:objecttype-list") | ||
full_url = build_absolute_url(endpoint, request=None) | ||
|
||
try: | ||
response = requests.get( | ||
full_url, | ||
headers={ | ||
"HTTP_AUTHORIZATION": f"Token {settings.OBJECTS_OBJECTTYPES_TOKEN}", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
raise SelfTestFailed( | ||
"Could not list objecttypes for the configured token" | ||
) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.conf import settings | ||
from django.contrib.sites.models import Site | ||
from django.urls import reverse | ||
|
||
import requests | ||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
|
||
from objecttypes.utils import build_absolute_url | ||
|
||
|
||
class SiteConfigurationStep(BaseConfigurationStep): | ||
""" | ||
Configure the application site/domain. | ||
""" | ||
|
||
verbose_name = "Site Configuration" | ||
required_settings = ["OBJECTTYPES_DOMAIN", "OBJECTTYPES_ORGANIZATION"] | ||
enable_setting = "SITES_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
site = Site.objects.get_current() | ||
return site.domain == settings.OBJECTTYPES_DOMAIN | ||
|
||
def configure(self): | ||
site = Site.objects.get_current() | ||
site.domain = settings.OBJECTTYPES_DOMAIN | ||
site.name = f"Objecttypes {settings.OBJECTTYPES_ORGANIZATION}".strip() | ||
site.save() | ||
|
||
def test_configuration(self): | ||
full_url = build_absolute_url(reverse("home")) | ||
try: | ||
response = requests.get(full_url) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
raise SelfTestFailed(f"Could not access home page at '{full_url}'") from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django.conf import settings | ||
from django.http import HttpRequest | ||
|
||
from furl import furl | ||
|
||
|
||
def get_domain() -> str: | ||
""" | ||
Obtain the domain/netloc of Open Notificaties according to settings or configuration. | ||
""" | ||
from django.contrib.sites.models import Site | ||
|
||
if settings.OBJECTTYPES_DOMAIN: | ||
return settings.OBJECTTYPES_DOMAIN | ||
|
||
return Site.objects.get_current().domain | ||
|
||
|
||
def build_absolute_url(path: str, request: HttpRequest | None = None) -> str: | ||
if request is not None: | ||
return request.build_absolute_uri(path) | ||
|
||
domain = get_domain() | ||
_furl = furl( | ||
scheme="https" if settings.IS_HTTPS else "http", | ||
netloc=domain, | ||
path=path, | ||
) | ||
return _furl.url |