~homeworkprod/byceps

c1d857b6d2fa11eab373b7281f54644ef27db439 — Jochen Kupperschmidt 5 months ago 8422510
Introduce `get_badge`
M byceps/blueprints/admin/shop/order/service.py => byceps/blueprints/admin/shop/order/service.py +1 -1
@@ 122,7 122,7 @@ def _get_additional_data_for_standard_event(

def _get_additional_data_for_badge_awarded(event: OrderEvent) -> OrderEventData:
    badge_id = event.data['badge_id']
    badge = user_badge_service.find_badge(badge_id)
    badge = user_badge_service.get_badge(badge_id)

    recipient_id = event.data['recipient_id']
    recipient = user_service.get_user(recipient_id)

M byceps/services/shop/order/actions/award_badge.py => byceps/services/shop/order/actions/award_badge.py +3 -13
@@ 9,7 9,7 @@ byceps.services.shop.order.actions.award_badge
from .....typing import UserID

from ....user_badge import awarding_service, badge_service
from ....user_badge.transfer.models import BadgeAwarding, BadgeID
from ....user_badge.transfer.models import BadgeAwarding

from ...article.transfer.models import ArticleNumber



@@ 26,25 26,15 @@ def award_badge(
    parameters: Parameters,
) -> None:
    """Award badge to user."""
    badge_id = parameters['badge_id']
    badge = badge_service.get_badge(parameters['badge_id'])
    user_id = order.placed_by_id

    _verify_badge_id(badge_id)

    for _ in range(quantity):
        awarding, _ = awarding_service.award_badge_to_user(badge_id, user_id)
        awarding, _ = awarding_service.award_badge_to_user(badge.id, user_id)

        _create_order_event(order.id, awarding)


def _verify_badge_id(badge_id: BadgeID) -> None:
    """Raise exception if no badge with that ID is known."""
    badge = badge_service.find_badge(badge_id)

    if badge is None:
        raise ValueError(f'Unknown badge ID "{badge_id}"')


def _create_order_event(order_id: OrderID, awarding: BadgeAwarding) -> None:
    event_type = 'badge-awarded'
    data = {

M byceps/services/user_badge/awarding_service.py => byceps/services/user_badge/awarding_service.py +2 -5
@@ 17,7 17,7 @@ from ...typing import UserID

from ..user import event_service, service as user_service

from .badge_service import _db_entity_to_badge, find_badge, get_badges
from .badge_service import _db_entity_to_badge, get_badge, get_badges
from .dbmodels.awarding import BadgeAwarding as DbBadgeAwarding
from .dbmodels.badge import Badge as DbBadge
from .transfer.models import (


@@ 32,10 32,7 @@ def award_badge_to_user(
    badge_id: BadgeID, user_id: UserID, *, initiator_id: Optional[UserID] = None
) -> tuple[BadgeAwarding, UserBadgeAwarded]:
    """Award the badge to the user."""
    badge = find_badge(badge_id)
    if badge is None:
        raise ValueError(f'Unknown badge ID "{badge_id}"')

    badge = get_badge(badge_id)
    user = user_service.get_user(user_id)
    awarded_at = datetime.utcnow()


M byceps/services/user_badge/badge_service.py => byceps/services/user_badge/badge_service.py +10 -0
@@ 86,6 86,16 @@ def find_badge(badge_id: BadgeID) -> Optional[Badge]:
    return _db_entity_to_badge(badge)


def get_badge(badge_id: BadgeID) -> Badge:
    """Return the badge with that id, or raise an exception."""
    badge = find_badge(badge_id)

    if badge is None:
        raise ValueError(f'Unknown badge ID "{badge_id}"')

    return badge


def find_badge_by_slug(slug: str) -> Optional[Badge]:
    """Return the badge with that slug, or `None` if not found."""
    badge = DbBadge.query \