hotpocket/services/backend/hotpocket_backend/apps/ui/services/associations.py
Tomek Wójcik b4338e2769
Some checks failed
CI / Checks (push) Failing after 13m2s
Release v1.0.0
2025-08-20 21:00:50 +02:00

69 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import annotations
import logging
import uuid
from django.core.exceptions import PermissionDenied
from django.http import Http404
from hotpocket_soa.dto.associations import (
AssociationOut,
AssociationWithTargetOut,
)
from hotpocket_soa.services import AssociationsService
LOGGER = logging.getLogger(__name__)
class UIAssociationsService:
def __init__(self):
self.associations_service = AssociationsService()
def get_or_404(self,
*,
account_uuid: uuid.UUID | None,
pk: uuid.UUID,
with_target: bool = False,
allow_archived: bool = False,
) -> AssociationOut | AssociationWithTargetOut:
try:
return AssociationsService().get(
account_uuid=account_uuid,
pk=pk,
with_target=True,
allow_archived=allow_archived,
)
except AssociationsService.AssociationNotFound as exception:
LOGGER.error(
'Association not found: account_uuid=`%s` pk=`%s`',
account_uuid,
pk,
exc_info=exception,
)
raise Http404()
except AssociationsService.AssociationAccessDenied as exception:
LOGGER.error(
'Association access denied: account_uuid=`%s` pk=`%s`',
account_uuid,
pk,
exc_info=exception,
)
raise PermissionDenied()
def star_on_unstar(self,
*,
association: AssociationOut,
star: bool,
) -> AssociationWithTargetOut:
if star is True:
_ = AssociationsService().star(association=association)
else:
_ = AssociationsService().unstar(association=association)
return AssociationsService().get( # type: ignore[return-value]
account_uuid=association.account_uuid,
pk=association.pk,
with_target=True,
)