~homeworkprod/byceps

b28e48a8237ca1c35d4490be6c57e95689268abf — Jochen Kupperschmidt 10 months ago 9d35912
Remove email service's `find_config` in favor of `get_config`
M byceps/blueprints/admin/brand/views.py => byceps/blueprints/admin/brand/views.py +3 -12
@@ 59,7 59,7 @@ def view(brand_id):
    brand = _get_brand_or_404(brand_id)

    settings = brand_settings_service.get_settings(brand.id)
    email_config = email_service.find_config(brand.id)
    email_config = email_service.get_config(brand.id)

    return {
        'brand': brand,


@@ 152,7 152,7 @@ def email_config_update_form(brand_id, erroneous_form=None):
    """Show form to update e-mail config."""
    brand = _get_brand_or_404(brand_id)

    config = _get_email_config_or_404(brand.id)
    config = email_service.get_config(brand.id)

    form = (
        erroneous_form


@@ 177,7 177,7 @@ def email_config_update(brand_id):
    """Update e-mail config."""
    brand = _get_brand_or_404(brand_id)

    config = _get_email_config_or_404(brand.id)
    config = email_service.get_config(brand.id)

    form = EmailConfigUpdateForm(request.form)
    if not form.validate():


@@ 206,12 206,3 @@ def _get_brand_or_404(brand_id):
        abort(404)

    return brand


def _get_email_config_or_404(brand_id):
    config = email_service.find_config(brand_id)

    if config is None:
        abort(404)

    return config

M byceps/blueprints/admin/shop/email/views.py => byceps/blueprints/admin/shop/email/views.py +1 -1
@@ 37,7 37,7 @@ def view_for_shop(shop_id):
    """Show e-mail examples."""
    shop = _get_shop_or_404(shop_id)

    email_config = email_service.find_config(shop.brand_id)
    email_config = email_service.get_config(shop.brand_id)

    example_placed_order_message_text = _get_example_placed_order_message_text(
        shop.id

M byceps/services/email/service.py => byceps/services/email/service.py +2 -12
@@ 96,28 96,18 @@ def _find_db_config(brand_id: BrandID) -> Optional[DbEmailConfig]:
        .one_or_none()


def find_config(brand_id: BrandID) -> Optional[EmailConfig]:
    """Return the configuration, or `None` if not found."""
    config = _find_db_config(brand_id)

    if config is None:
        return None

    return _db_entity_to_config(config)


def get_config(brand_id: BrandID) -> EmailConfig:
    """Return the configuration, or raise an error if none is configured
    for that brand.
    """
    config = find_config(brand_id)
    config = _find_db_config(brand_id)

    if config is None:
        raise UnknownEmailConfigId(
            f'No e-mail config found for brand ID "{brand_id}"'
        )

    return config
    return _db_entity_to_config(config)


def set_config(

M byceps/services/shop/order/email/service.py => byceps/services/shop/order/email/service.py +3 -13
@@ 10,7 10,7 @@ Notification e-mails about shop orders

from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, Optional
from typing import Any, Dict

from flask import current_app
from jinja2 import FileSystemLoader


@@ 175,24 175,14 @@ def _assemble_email_to_orderer(
    recipient_address: str,
) -> Message:
    """Assemble an email message with the rendered template as its body."""
    sender = _get_sender_address(brand_id)
    config = email_service.get_config(brand_id)
    sender = config.sender
    body = _render_template(template_name, **template_context)
    recipients = [recipient_address]

    return Message(sender, recipients, subject, body)


def _get_sender_address(brand_id: BrandID) -> Optional[Sender]:
    config = email_service.find_config(brand_id)

    if not config:
        current_app.logger.warning(
            'No e-mail sender configured for brand ID "%s".', brand_id
        )

    return config.sender


def _get_snippet_body(shop_id: ShopID, name: str) -> str:
    scope = Scope('shop', str(shop_id))


M tests/integration/blueprints/admin/brand/test_views.py => tests/integration/blueprints/admin/brand/test_views.py +1 -2
@@ 43,8 43,7 @@ def test_create(brand_admin_client):
    assert brand.id == brand_id
    assert brand.title == title

    email_config = email_service.find_config(brand.id)
    assert email_config is not None
    email_config = email_service.get_config(brand.id)
    assert email_config.sender is not None
    assert email_config.sender.address == 'noreply@galant.example'
    assert email_config.sender.name == 'gaLANt'