~homeworkprod/byceps

531a863e5df70027fb872afbd56df7d5350f65d4 — Jochen Kupperschmidt 1 year, 1 month ago e5cccf3
Move shop order sequence service function frontends to new service in order package
M byceps/blueprints/admin/shop/storefront/views.py => byceps/blueprints/admin/shop/storefront/views.py +17 -12
@@ 9,6 9,7 @@ byceps.blueprints.admin.shop.storefront.views
from flask import abort, request

from .....services.shop.catalog import service as catalog_service
from .....services.shop.order import sequence_service as order_sequence_service
from .....services.shop.sequence import service as sequence_service
from .....services.shop.shop import service as shop_service
from .....services.shop.storefront import service as storefront_service


@@ 49,7 50,9 @@ def index_for_shop(shop_id):

def _get_order_number_prefixes_by_sequence_id(storefronts, shop_id):
    sequence_ids = {sf.order_number_sequence_id for sf in storefronts}
    sequences = sequence_service.find_order_number_sequences_for_shop(shop_id)
    sequences = order_sequence_service.find_order_number_sequences_for_shop(
        shop_id
    )
    return {seq.id: seq.prefix for seq in sequences}




@@ 62,7 65,7 @@ def view(storefront_id):

    shop = shop_service.get_shop(storefront.shop_id)

    order_number_sequence = sequence_service.find_order_number_sequence(
    order_number_sequence = order_sequence_service.find_order_number_sequence(
        storefront.order_number_sequence_id
    )
    order_number_prefix = order_number_sequence.prefix


@@ 82,8 85,8 @@ def create_form(shop_id, erroneous_form=None):
    shop = _get_shop_or_404(shop_id)

    catalogs = catalog_service.get_all_catalogs()
    order_number_sequences = sequence_service.find_order_number_sequences_for_shop(
        shop.id
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(shop.id)
    )
    order_number_sequence_available = bool(order_number_sequences)



@@ 108,8 111,8 @@ def create(shop_id):

    catalogs = catalog_service.get_all_catalogs()

    order_number_sequences = sequence_service.find_order_number_sequences_for_shop(
        shop.id
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(shop.id)
    )
    if not order_number_sequences:
        flash_error(


@@ 130,7 133,7 @@ def create(shop_id):
        flash_error('Es wurde keine gültige Bestellnummer-Sequenz angegeben.')
        return create_form(shop_id, form)

    order_number_sequence = sequence_service.find_order_number_sequence(
    order_number_sequence = order_sequence_service.find_order_number_sequence(
        order_number_sequence_id
    )
    if (order_number_sequence is None) or (


@@ 140,7 143,7 @@ def create(shop_id):
        return create_form(shop_id, form)

    try:
        item_number = sequence_service.generate_order_number(
        item_number = order_sequence_service.generate_order_number(
            order_number_sequence.id
        )
    except sequence_service.NumberGenerationFailed as e:


@@ 168,8 171,8 @@ def update_form(storefront_id, erroneous_form=None):
    shop = shop_service.get_shop(storefront.shop_id)

    catalogs = catalog_service.get_all_catalogs()
    order_number_sequences = sequence_service.find_order_number_sequences_for_shop(
        shop.id
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(shop.id)
    )

    form = (


@@ 194,8 197,10 @@ def update(storefront_id):
    storefront = _get_storefront_or_404(storefront_id)

    catalogs = catalog_service.get_all_catalogs()
    order_number_sequences = sequence_service.find_order_number_sequences_for_shop(
        storefront.shop_id
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(
            storefront.shop_id
        )
    )

    form = StorefrontUpdateForm(request.form)

A byceps/services/shop/order/sequence_service.py => byceps/services/shop/order/sequence_service.py +55 -0
@@ 0,0 1,55 @@
"""
byceps.services.shop.order.sequence_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from typing import List, Optional

from ..sequence import service as sequence_service
from ..sequence.transfer.models import NumberSequence, NumberSequenceID, Purpose
from ..shop.transfer.models import ShopID

from .transfer.models import OrderNumber


def create_order_number_sequence(
    shop_id: ShopID, prefix: str, *, value: Optional[int] = None
) -> NumberSequenceID:
    """Create an order number sequence."""
    return sequence_service.create_sequence(
        shop_id, Purpose.order, prefix, value=value
    )


def delete_order_number_sequence(sequence_id: NumberSequenceID) -> None:
    """Delete the order number sequence."""
    sequence_service.delete_sequence(sequence_id)


def find_order_number_sequence(
    sequence_id: NumberSequenceID,
) -> Optional[NumberSequence]:
    """Return the order number sequence, or `None` if the sequence ID
    is unknown or if the sequence's purpose is not order numbers.
    """
    return sequence_service._find_sequence(sequence_id, Purpose.order)


def find_order_number_sequences_for_shop(
    shop_id: ShopID,
) -> List[NumberSequence]:
    """Return the order number sequences defined for that shop."""
    return sequence_service._find_number_sequences(shop_id, Purpose.order)


def generate_order_number(sequence_id: NumberSequenceID) -> OrderNumber:
    """Generate and reserve an unused, unique order number from this
    sequence.
    """
    sequence = sequence_service._get_next_sequence_step(
        sequence_id, Purpose.order
    )
    return OrderNumber(f'{sequence.prefix}{sequence.value:05d}')

M byceps/services/shop/order/service.py => byceps/services/shop/order/service.py +1 -2
@@ 20,7 20,6 @@ from ...user import service as user_service

from ..article import service as article_service
from ..cart.models import Cart
from ..sequence import service as sequence_service
from ..shop.models import Shop
from ..shop import service as shop_service
from ..shop.transfer.models import ShopID


@@ 31,7 30,7 @@ from .models.order import Order as DbOrder
from .models.order_event import OrderEvent as DbOrderEvent
from .models.order_item import OrderItem as DbOrderItem
from .models.orderer import Orderer
from . import action_service
from . import action_service, sequence_service
from .transfer.models import (
    Order,
    OrderID,

M byceps/services/shop/sequence/service.py => byceps/services/shop/sequence/service.py +0 -37
@@ 10,8 10,6 @@ from typing import List, Optional

from ....database import db

from ..order.transfer.models import OrderNumber

from ..shop.transfer.models import ShopID

from .models import NumberSequence as DbNumberSequence


@@ 34,12 32,6 @@ def create_sequence(
    return sequence.id


def create_order_number_sequence(
    shop_id: ShopID, prefix: str, *, value: Optional[int] = None
) -> NumberSequenceID:
    return create_sequence(shop_id, Purpose.order, prefix, value=value)


def delete_sequence(sequence_id: NumberSequenceID) -> None:
    """Delete the sequence."""
    db.session.query(DbNumberSequence) \


@@ 49,20 41,6 @@ def delete_sequence(sequence_id: NumberSequenceID) -> None:
    db.session.commit()


def delete_order_number_sequence(sequence_id: NumberSequenceID) -> None:
    """Delete the order sequence."""
    delete_sequence(sequence_id)


def find_order_number_sequence(
    sequence_id: NumberSequenceID,
) -> Optional[NumberSequence]:
    """Return the order number sequence, or `None` if the sequence ID
    is unknown or if the sequence's purpose is not order numbers.
    """
    return _find_sequence(sequence_id, Purpose.order)


def _find_sequence(
    sequence_id: NumberSequenceID, purpose: Purpose
) -> Optional[NumberSequence]:


@@ 82,14 60,6 @@ class NumberGenerationFailed(Exception):
        self.message = message


def generate_order_number(sequence_id: NumberSequenceID) -> OrderNumber:
    """Generate and reserve an unused, unique order number from this
    sequence.
    """
    sequence = _get_next_sequence_step(sequence_id, Purpose.order)
    return OrderNumber(f'{sequence.prefix}{sequence.value:05d}')


def _get_next_sequence_step(
    sequence_id: NumberSequenceID, purpose: Purpose
) -> NumberSequence:


@@ 116,13 86,6 @@ def _get_next_sequence_step(
    return sequence


def find_order_number_sequences_for_shop(
    shop_id: ShopID,
) -> List[NumberSequence]:
    """Return the order number sequences defined for that shop."""
    return _find_number_sequences(shop_id, Purpose.order)


def _find_number_sequences(
    shop_id: ShopID, purpose: Purpose
) -> List[NumberSequence]:

M scripts/create_shop.py => scripts/create_shop.py +2 -2
@@ 11,7 11,7 @@ import click
from byceps.services.shop.article import (
    sequence_service as article_sequence_service,
)
from byceps.services.shop.sequence import service as sequence_service
import byceps.services.shop.order.sequence_service as order_sequence_service
from byceps.services.shop.shop import service as shop_service
from byceps.util.system import get_config_filename_from_env_or_exit



@@ 30,7 30,7 @@ def execute(shop_id, title, email_config_id, article_prefix, order_prefix):
    article_sequence_service.create_article_number_sequence(
        shop.id, article_prefix
    )
    sequence_service.create_order_number_sequence(shop.id, order_prefix)
    order_sequence_service.create_order_number_sequence(shop.id, order_prefix)

    click.secho('Done.', fg='green')


M tests/integration/announce/irc/test_shop_order.py => tests/integration/announce/irc/test_shop_order.py +9 -5
@@ 8,9 8,11 @@ import pytest
from byceps.announce.irc import shop_order  # Load signal handlers.
from byceps.events.shop import ShopOrderCanceled, ShopOrderPaid, ShopOrderPlaced
from byceps.services.shop.cart.models import Cart
from byceps.services.shop.order import service as order_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.order.transfer.models import PaymentMethod
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.shop import service as shop_service
from byceps.services.shop.storefront import service as storefront_service
from byceps.signals import shop as shop_signals


@@ 47,7 49,9 @@ def test_shop_order_placed_announced(app, placed_order, orderer_user):
    assert_submitted_data(mock, expected_channel, expected_text)


def test_shop_order_canceled_announced(app, canceled_order, orderer_user, shop_admin):
def test_shop_order_canceled_announced(
    app, canceled_order, orderer_user, shop_admin
):
    expected_channel = CHANNEL_ORGA_LOG
    expected_text = (
        'ShoppingSheriff hat Bestellung ORDER-00002 von Ken_von_Kaufkraft '


@@ 127,13 131,13 @@ def shop(app, email_config):

@pytest.fixture(scope='module')
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'ORDER-'
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture(scope='module')

M tests/integration/blueprints/admin/shop/conftest.py => tests/integration/blueprints/admin/shop/conftest.py +5 -3
@@ 5,7 5,9 @@

import pytest

from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
)
from byceps.services.shop.shop import service as shop_service
from byceps.services.shop.storefront import service as storefront_service



@@ 21,13 23,13 @@ def shop(email_config):

@pytest.fixture(scope='module')
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'ORDER-'
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture(scope='module')

M tests/integration/blueprints/admin/shop/order/test_order_export.py => tests/integration/blueprints/admin/shop/order/test_order_export.py +6 -4
@@ 12,8 12,10 @@ import pytest
from byceps.services.shop.article import service as article_service
from byceps.services.shop.cart.models import Cart
from byceps.services.shop.order.models.orderer import Orderer
from byceps.services.shop.order import service as order_service
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.storefront import service as storefront_service

from tests.helpers import http_client, login_user


@@ 106,13 108,13 @@ def orderer(make_user):

@pytest.fixture
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'LR-08-B', value=26
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/blueprints/site/shop/order/test_views.py => tests/integration/blueprints/site/shop/order/test_views.py +6 -4
@@ 10,8 10,10 @@ import pytest
from byceps.events.shop import ShopOrderPlaced
from byceps.services.shop.article import service as article_service
from byceps.services.shop.order.models.order import Order
from byceps.services.shop.order import service as order_service
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.shop import service as shop_service
from byceps.services.shop.storefront import service as storefront_service
from byceps.services.site import service as site_service


@@ 50,13 52,13 @@ def shop(email_config, admin_user):

@pytest.fixture
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'AEC-01-B', value=4
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/blueprints/site/shop/orders/test_views.py => tests/integration/blueprints/site/shop/orders/test_views.py +8 -6
@@ 7,8 7,10 @@ import pytest

from byceps.services.shop.cart.models import Cart
from byceps.services.shop.order.models.orderer import Orderer
from byceps.services.shop.order import service as order_service
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.shop import service as shop_service
from byceps.services.shop.storefront import service as storefront_service
from byceps.services.site import service as site_service


@@ 45,24 47,24 @@ def shop2(admin_app, email_config):

@pytest.fixture
def order_number_sequence_id1(shop1) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop1.id, 'LF-02-B'
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture
def order_number_sequence_id2(shop2) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop2.id, 'SHOP-02-B'
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/services/shop/conftest.py => tests/integration/services/shop/conftest.py +5 -3
@@ 6,7 6,9 @@
import pytest

from byceps.services.shop.cart.models import Cart
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
)
from byceps.services.shop.shop import service as shop_service
from byceps.services.shop.storefront import service as storefront_service



@@ 20,13 22,13 @@ def shop(email_config):

@pytest.fixture
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'order-'
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/services/shop/order/email/test_email_on_order_canceled.py => tests/integration/services/shop/order/email/test_email_on_order_canceled.py +6 -4
@@ 9,8 9,10 @@ from unittest.mock import patch
import pytest

from byceps.services.shop.order.email import service as order_email_service
from byceps.services.shop.order import service as order_service
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.storefront import service as storefront_service
from byceps.services.snippet import service as snippet_service



@@ 34,13 36,13 @@ def customer(make_user_with_detail):

@pytest.fixture
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'AC-14-B', value=16
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/services/shop/order/email/test_email_on_order_paid.py => tests/integration/services/shop/order/email/test_email_on_order_paid.py +6 -4
@@ 9,9 9,11 @@ from unittest.mock import patch
import pytest

from byceps.services.shop.order.email import service as order_email_service
from byceps.services.shop.order import service as order_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.order.transfer.models import PaymentMethod
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.storefront import service as storefront_service
from byceps.services.snippet import service as snippet_service



@@ 35,13 37,13 @@ def customer(make_user_with_detail):

@pytest.fixture
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'AC-14-B', value=21
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/services/shop/order/email/test_email_on_order_placed.py => tests/integration/services/shop/order/email/test_email_on_order_placed.py +6 -4
@@ 11,8 11,10 @@ import pytest

from byceps.services.shop.article import service as article_service
from byceps.services.shop.order.email import service as order_email_service
from byceps.services.shop.order import service as order_service
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.storefront import service as storefront_service
from byceps.services.snippet import service as snippet_service



@@ 39,13 41,13 @@ def customer(make_user_with_detail):

@pytest.fixture
def order_number_sequence_id(shop) -> None:
    sequence_id = sequence_service.create_order_number_sequence(
    sequence_id = order_sequence_service.create_order_number_sequence(
        shop.id, 'AC-14-B', value=252
    )

    yield sequence_id

    sequence_service.delete_order_number_sequence(sequence_id)
    order_sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/services/shop/order/test_service_orders_placed_by_user_for_shop.py => tests/integration/services/shop/order/test_service_orders_placed_by_user_for_shop.py +14 -8
@@ 6,8 6,10 @@
import pytest

from byceps.services.shop.cart.models import Cart
from byceps.services.shop.order import service as order_service
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
    service as order_service,
)
from byceps.services.shop.shop import service as shop_service
from byceps.services.shop.storefront import service as storefront_service



@@ 19,8 21,8 @@ from tests.integration.services.shop.helpers import create_shop
@pytest.fixture
def storefront1(email_config):
    shop = create_shop('first-nice-shop')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LF-02-B'
    order_number_sequence_id = (
        order_sequence_service.create_order_number_sequence(shop.id, 'LF-02-B')
    )
    storefront = storefront_service.create_storefront(
        f'{shop.id}-storefront',


@@ 32,15 34,17 @@ def storefront1(email_config):
    yield storefront

    storefront_service.delete_storefront(storefront.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    order_sequence_service.delete_order_number_sequence(
        order_number_sequence_id
    )
    shop_service.delete_shop(shop.id)


@pytest.fixture
def storefront2(email_config):
    shop = create_shop('second-nice-shop')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LF-03-B'
    order_number_sequence_id = (
        order_sequence_service.create_order_number_sequence(shop.id, 'LF-03-B')
    )
    storefront = storefront_service.create_storefront(
        f'{shop.id}-storefront',


@@ 52,7 56,9 @@ def storefront2(email_config):
    yield storefront

    storefront_service.delete_storefront(storefront.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    order_sequence_service.delete_order_number_sequence(
        order_number_sequence_id
    )
    shop_service.delete_shop(shop.id)



M tests/integration/services/shop/sequence/test_service_sequence_number_generation.py => tests/integration/services/shop/sequence/test_service_sequence_number_generation.py +21 -9
@@ 8,7 8,9 @@ import pytest
from byceps.services.shop.article import (
    sequence_service as article_sequence_service,
)
from byceps.services.shop.sequence import service as sequence_service
from byceps.services.shop.order import (
    sequence_service as order_sequence_service,
)
from byceps.services.shop.shop import service as shop_service




@@ 71,15 73,19 @@ def test_generate_article_number_custom(admin_app, shop2):
def test_generate_order_number_default(admin_app, shop1):
    shop = shop1

    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'AEC-01-B'
    order_number_sequence_id = (
        order_sequence_service.create_order_number_sequence(shop.id, 'AEC-01-B')
    )

    actual = sequence_service.generate_order_number(order_number_sequence_id)
    actual = order_sequence_service.generate_order_number(
        order_number_sequence_id
    )

    assert actual == 'AEC-01-B00001'

    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    order_sequence_service.delete_order_number_sequence(
        order_number_sequence_id
    )


def test_generate_order_number_custom(admin_app, shop2):


@@ 87,12 93,18 @@ def test_generate_order_number_custom(admin_app, shop2):

    last_assigned_order_sequence_number = 206

    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LOL-03-B', value=last_assigned_order_sequence_number
    order_number_sequence_id = (
        order_sequence_service.create_order_number_sequence(
            shop.id, 'LOL-03-B', value=last_assigned_order_sequence_number
        )
    )

    actual = sequence_service.generate_order_number(order_number_sequence_id)
    actual = order_sequence_service.generate_order_number(
        order_number_sequence_id
    )

    assert actual == 'LOL-03-B00207'

    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    order_sequence_service.delete_order_number_sequence(
        order_number_sequence_id
    )