Skip to content

Commit

Permalink
[#485] Create Step and Model
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Dec 16, 2024
1 parent 50a1ef4 commit 1a3f4e2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/objects/setup_configuration/models/token_auth.py
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]
72 changes: 72 additions & 0 deletions src/objects/setup_configuration/steps/token_auth.py
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}")

0 comments on commit 1a3f4e2

Please sign in to comment.