-
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.
- Loading branch information
1 parent
50a1ef4
commit 1a3f4e2
Showing
2 changed files
with
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django_setup_configuration.models import ConfigurationModel | ||
from pydantic import Field | ||
|
||
from objects.token.models import TokenAuth | ||
|
||
|
||
class TokenAuthConfigurationModel(ConfigurationModel): | ||
class Meta: | ||
django_model_refs = { | ||
TokenAuth: ( | ||
"identifier", | ||
"token", | ||
"contact_person", | ||
"email", | ||
"organization", | ||
"application", | ||
"administration", | ||
"is_superuser", | ||
) | ||
} | ||
|
||
|
||
class TokenAuthGroupConfigurationModel(ConfigurationModel): | ||
items: list[TokenAuthConfigurationModel] |
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,72 @@ | ||
import logging | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.db import IntegrityError | ||
|
||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import ConfigurationRunFailed | ||
|
||
from objects.setup_configuration.models.token_auth import ( | ||
TokenAuthGroupConfigurationModel, | ||
) | ||
from objects.token.models import TokenAuth | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TokenAuthConfigurationStep( | ||
BaseConfigurationStep[TokenAuthGroupConfigurationModel] | ||
): | ||
""" | ||
Configure tokens for other applications to access Objects API | ||
""" | ||
|
||
namespace = "token_tokenauth" | ||
enable_setting = "token_tokenauth_config_enable" | ||
|
||
verbose_name = "Configuration to set up authentication tokens for objects" | ||
config_model = TokenAuthGroupConfigurationModel | ||
|
||
def execute(self, model: TokenAuthGroupConfigurationModel) -> None: | ||
for item in model.items: | ||
logger.info(f"Configuring {item.identifier}") | ||
|
||
model_kwargs = { | ||
"identifier": item.identifier, | ||
"token": item.token, | ||
"contact_person": item.contact_person, | ||
"email": item.email, | ||
"organization": item.organization, | ||
"application": item.application, | ||
"administration": item.administration, | ||
"is_superuser": item.is_superuser, | ||
} | ||
|
||
token_instance = TokenAuth(**model_kwargs) | ||
|
||
try: | ||
token_instance.full_clean(exclude=("id",), validate_unique=False) | ||
except ValidationError as exception: | ||
exception_message = ( | ||
f"Validation error(s) occured for {item.identifier}." | ||
) | ||
raise ConfigurationRunFailed(exception_message) from exception | ||
|
||
logger.debug(f"No validation errors found for {item.identifier}") | ||
|
||
try: | ||
logger.debug(f"Saving {item.identifier}") | ||
|
||
TokenAuth.objects.update_or_create( | ||
identifier=item.identifier, | ||
defaults={ | ||
key: value | ||
for key, value in model_kwargs.items() | ||
if key != "identifier" | ||
}, | ||
) | ||
except IntegrityError as exception: | ||
exception_message = f"Failed configuring token {item.identifier}." | ||
raise ConfigurationRunFailed(exception_message) from exception | ||
|
||
logger.info(f"Configured {item.identifier}") |