-
Notifications
You must be signed in to change notification settings - Fork 10
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
fa398d3
commit b2e586e
Showing
11 changed files
with
247 additions
and
1 deletion.
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
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
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
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,63 @@ | ||
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 objects.token.models import TokenAuth | ||
from objects.utils import build_absolute_url | ||
|
||
|
||
class DemoUserStep(BaseConfigurationStep): | ||
""" | ||
Create demo user to request Objects API | ||
**NOTE** For now demo user has all permissions. | ||
""" | ||
|
||
verbose_name = "Demo User Configuration" | ||
required_settings = [ | ||
"DEMO_TOKEN", | ||
"DEMO_PERSON", | ||
"DEMO_EMAIL", | ||
] | ||
enable_setting = "DEMO_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
return TokenAuth.objects.filter(token=settings.DEMO_TOKEN).exists() | ||
|
||
def configure(self): | ||
token_auth, created = TokenAuth.objects.get_or_create( | ||
token=settings.DEMO_TOKEN, | ||
defaults={ | ||
"contact_person": settings.DEMO_PERSON, | ||
"email": settings.DEMO_EMAIL, | ||
"is_superuser": True, | ||
}, | ||
) | ||
if ( | ||
token_auth.contact_person != settings.DEMO_PERSON | ||
or token_auth.email != settings.DEMO_EMAIL | ||
): | ||
token_auth.contact_person = settings.DEMO_PERSON | ||
token_auth.email = settings.DEMO_EMAIL | ||
token_auth.save(update_fields=["contact_person", "email"]) | ||
|
||
def test_configuration(self): | ||
endpoint = reverse("v2:object-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 objects 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,58 @@ | ||
from django.conf import settings | ||
|
||
import requests | ||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
from zds_client.client import ClientError | ||
from zgw_consumers.constants import APITypes, AuthTypes | ||
from zgw_consumers.models import Service | ||
|
||
|
||
class ObjecttypesStep(BaseConfigurationStep): | ||
""" | ||
Configure credentials for Objects API to request Objecttypes API | ||
Normal mode doesn't change the token after its initial creation. | ||
If the token is changed, run this command with 'overwrite' flag | ||
""" | ||
|
||
verbose_name = "Objecttypes Configuration" | ||
required_settings = [ | ||
"OBJECTTYPES_API_ROOT", | ||
"OBJECTS_OBJECTTYPES_TOKEN", | ||
] | ||
enable_setting = "OBJECTS_OBJECTTYPES_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
return Service.objects.filter(api_root=settings.OBJECTTYPES_API_ROOT).exists() | ||
|
||
def configure(self) -> None: | ||
service, created = Service.objects.update_or_create( | ||
api_root=settings.OBJECTTYPES_API_ROOT, | ||
defaults={ | ||
"label": "Objecttypes API", | ||
"api_type": APITypes.orc, | ||
"oas": settings.OBJECTTYPES_API_OAS, | ||
"auth_type": AuthTypes.api_key, | ||
"header_key": "Authorization", | ||
"header_value": f"Token {settings.OBJECTS_OBJECTTYPES_TOKEN}", | ||
}, | ||
) | ||
if not created: | ||
service.oas = settings.OBJECTTYPES_API_OAS | ||
service.header_value = f"Token {settings.OBJECTS_OBJECTTYPES_TOKEN}" | ||
service.save(update_fields=["oas", "header_value"]) | ||
|
||
def test_configuration(self) -> None: | ||
""" | ||
This check depends on the configuration in Objecttypes | ||
""" | ||
client = Service.objects.get( | ||
api_root=settings.OBJECTTYPES_API_ROOT | ||
).build_client() | ||
try: | ||
client.list("objecttype") | ||
except (requests.RequestException, ClientError) as exc: | ||
raise SelfTestFailed( | ||
"Could not Could not retrieve list of objecttypes from Objecttypes API." | ||
) 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 objects.utils import build_absolute_url | ||
|
||
|
||
class SiteConfigurationStep(BaseConfigurationStep): | ||
""" | ||
Configure the application site/domain. | ||
""" | ||
|
||
verbose_name = "Site Configuration" | ||
required_settings = ["OBJECTS_DOMAIN", "OBJECTS_ORGANIZATION"] | ||
enable_setting = "SITES_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
site = Site.objects.get_current() | ||
return site.domain == settings.OBJECTS_DOMAIN | ||
|
||
def configure(self): | ||
site = Site.objects.get_current() | ||
site.domain = settings.OBJECTS_DOMAIN | ||
site.name = f"Objects {settings.OBJECTS_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 according to settings or configuration. | ||
""" | ||
from django.contrib.sites.models import Site | ||
|
||
if settings.OBJECTS_DOMAIN: | ||
return settings.OBJECTS_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 |