Skip to content

Commit d6eaa77

Browse files
committed
Update seeding for Catalog
1 parent edf2b00 commit d6eaa77

39 files changed

+804
-1175
lines changed

mpt_api_client/resources/catalog/products.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class ProductsServiceConfig:
6565

6666

6767
class ProductsService(
68-
CreateFileMixin[Model],
69-
UpdateFileMixin[Model],
70-
PublishableMixin[Model],
68+
CreateFileMixin[Product],
69+
UpdateFileMixin[Product],
70+
PublishableMixin[Product],
7171
GetMixin[Product],
7272
DeleteMixin,
7373
CollectionMixin[Product],
@@ -128,9 +128,9 @@ def update_settings(self, product_id: str, settings: ResourceData) -> Product:
128128

129129

130130
class AsyncProductsService(
131-
AsyncCreateFileMixin[Model],
132-
AsyncUpdateFileMixin[Model],
133-
AsyncPublishableMixin[Model],
131+
AsyncCreateFileMixin[Product],
132+
AsyncUpdateFileMixin[Product],
133+
AsyncPublishableMixin[Product],
134134
AsyncGetMixin[Product],
135135
AsyncDeleteMixin,
136136
AsyncCollectionMixin[Product],

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ per-file-ignores = [
131131
"tests/e2e/accounts/*.py: WPS430 WPS202",
132132
"tests/e2e/catalog/*.py: WPS202 WPS421",
133133
"tests/e2e/catalog/items/*.py: WPS110 WPS202",
134+
"tests/seed/catalog/test_product.py: WPS202 WPS204 WPS219",
134135
"tests/*: WPS432 WPS202",
135-
"seed/accounts/*.py: WPS453",
136+
"seed/*: WPS404",
137+
"seed/accounts/*.py: WPS204 WPS404 WPS453",
138+
"seed/catalog/product.py: WPS202 WPS204 WPS217 WPS201 WPS213 WPS404"
136139
]
137140

138141
[tool.ruff]

seed/accounts/__init__.py

Whitespace-only changes.

seed/accounts/api_tokens.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import logging
22
import os
33

4-
from dependency_injector.wiring import inject
4+
from dependency_injector.wiring import Provide, inject
55

66
from mpt_api_client import AsyncMPTClient
77
from mpt_api_client.resources.accounts.api_tokens import ApiToken
8+
from seed.container import Container
89
from seed.context import Context
9-
from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS
1010

1111
logger = logging.getLogger(__name__)
1212

1313

1414
@inject
1515
async def get_api_token(
16-
context: Context = DEFAULT_CONTEXT,
17-
mpt_ops: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
16+
context: Context = Provide[Container.context],
17+
mpt_ops: AsyncMPTClient = Provide[Container.mpt_operations],
1818
) -> ApiToken | None:
1919
"""Get API token from context or fetch from API."""
2020
api_token_id = context.get_string("accounts.api_token.id")
@@ -34,7 +34,7 @@ async def get_api_token(
3434

3535
@inject
3636
def build_api_token_data(
37-
context: Context = DEFAULT_CONTEXT,
37+
context: Context = Provide[Container.context],
3838
) -> dict[str, object]:
3939
"""Get API token data dictionary for creation."""
4040
account_id = os.getenv("CLIENT_ACCOUNT_ID")
@@ -50,8 +50,8 @@ def build_api_token_data(
5050

5151
@inject
5252
async def init_api_token(
53-
context: Context = DEFAULT_CONTEXT,
54-
mpt_ops: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
53+
context: Context = Provide[Container.context],
54+
mpt_ops: AsyncMPTClient = Provide[Container.mpt_operations],
5555
) -> ApiToken:
5656
"""Get or create API token."""
5757
api_token = await get_api_token(context=context, mpt_ops=mpt_ops)

seed/accounts/buyer.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import logging
22
import os
3-
import pathlib
43

5-
from dependency_injector.wiring import inject
4+
from dependency_injector.wiring import Provide, inject
65

76
from mpt_api_client import AsyncMPTClient
87
from mpt_api_client.resources.accounts.buyers import Buyer
8+
from seed.container import Container
99
from seed.context import Context
10-
from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS
10+
from seed.static.static import ICON
1111

1212
logger = logging.getLogger(__name__)
1313

14-
icon = pathlib.Path("seed/data/logo.png").resolve()
15-
1614

1715
@inject
1816
async def get_buyer(
19-
context: Context = DEFAULT_CONTEXT,
20-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
17+
context: Context = Provide[Container.context],
18+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
2119
) -> Buyer | None:
2220
"""Get buyer from context or fetch from API."""
2321
buyer_id = context.get_string("accounts.buyer.id")
@@ -36,7 +34,7 @@ async def get_buyer(
3634

3735

3836
@inject
39-
def build_buyer_data(context: Context = DEFAULT_CONTEXT) -> dict[str, object]:
37+
def build_buyer_data(context: Context = Provide[Container.context]) -> dict[str, object]:
4038
"""Build buyer data dictionary for creation."""
4139
buyer_account_id = os.getenv("CLIENT_ACCOUNT_ID")
4240
if not buyer_account_id:
@@ -65,16 +63,16 @@ def build_buyer_data(context: Context = DEFAULT_CONTEXT) -> dict[str, object]:
6563

6664
@inject
6765
async def init_buyer(
68-
context: Context = DEFAULT_CONTEXT,
69-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
66+
context: Context = Provide[Container.context],
67+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
7068
) -> Buyer:
7169
"""Get or create buyer."""
7270
buyer = await get_buyer(context=context, mpt_operations=mpt_operations)
7371
if buyer is None:
7472
buyer_data = build_buyer_data(context=context)
7573
logger.debug("Creating buyer ...")
76-
with open(str(icon), "rb") as icon_file: # noqa: PTH123
77-
created = await mpt_operations.accounts.buyers.create(buyer_data, file=icon_file)
74+
with ICON.open("rb") as icon_fd:
75+
created = await mpt_operations.accounts.buyers.create(buyer_data, file=icon_fd)
7876
if isinstance(created, Buyer):
7977
context.set_resource("accounts.buyer", created)
8078
context["accounts.buyer.id"] = created.id

seed/accounts/licensee.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import logging
22
import os
3-
import pathlib
43

5-
from dependency_injector.wiring import inject
4+
from dependency_injector.wiring import Provide, inject
65

76
from mpt_api_client import AsyncMPTClient
87
from mpt_api_client.resources.accounts.licensees import Licensee
8+
from seed.container import Container
99
from seed.context import Context
10-
from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_CLIENT
10+
from seed.static.static import ICON
1111

1212
logger = logging.getLogger(__name__)
1313

14-
icon = pathlib.Path("seed/data/logo.png").resolve()
15-
1614

1715
@inject
1816
async def get_licensee(
19-
context: Context = DEFAULT_CONTEXT,
20-
mpt_client: AsyncMPTClient = DEFAULT_MPT_CLIENT,
17+
context: Context = Provide[Container.context],
18+
mpt_client: AsyncMPTClient = Provide[Container.mpt_client],
2119
) -> Licensee | None:
2220
"""Get licensee from context or fetch from API."""
2321
licensee_id = context.get_string("accounts.licensee.id")
@@ -37,7 +35,7 @@ async def get_licensee(
3735

3836
@inject
3937
def build_licensee_data( # noqa: WPS238
40-
context: Context = DEFAULT_CONTEXT,
38+
context: Context = Provide[Container.context],
4139
) -> dict[str, object]:
4240
"""Get licensee data dictionary for creation."""
4341
account_id = os.getenv("CLIENT_ACCOUNT_ID")
@@ -76,15 +74,15 @@ def build_licensee_data( # noqa: WPS238
7674

7775
@inject
7876
async def init_licensee(
79-
context: Context = DEFAULT_CONTEXT,
80-
mpt_client: AsyncMPTClient = DEFAULT_MPT_CLIENT,
77+
context: Context = Provide[Container.context],
78+
mpt_client: AsyncMPTClient = Provide[Container.mpt_client],
8179
) -> Licensee:
8280
"""Get or create licensee."""
8381
licensee = await get_licensee(context=context, mpt_client=mpt_client)
8482
if licensee is None:
8583
licensee_data = build_licensee_data(context=context)
8684
logger.debug("Creating licensee ...")
87-
with open(str(icon), "rb") as icon_file: # noqa: PTH123
85+
with ICON.open("rb") as icon_file:
8886
created = await mpt_client.accounts.licensees.create(licensee_data, file=icon_file)
8987
if isinstance(created, Licensee):
9088
context.set_resource("accounts.licensee", created)

seed/accounts/module.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import logging
22

3-
from dependency_injector.wiring import inject
3+
from dependency_injector.wiring import Provide, inject
44

55
from mpt_api_client import AsyncMPTClient
66
from mpt_api_client.resources.accounts.modules import Module
77
from mpt_api_client.rql.query_builder import RQLQuery
8+
from seed.container import Container
89
from seed.context import Context
9-
from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS
1010

1111
logger = logging.getLogger(__name__)
1212

1313

1414
@inject
1515
async def get_module(
16-
context: Context = DEFAULT_CONTEXT,
17-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
16+
context: Context = Provide[Container.context],
17+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
1818
) -> Module | None:
1919
"""Get module from context or fetch from API."""
2020
module_id = context.get_string("accounts.module.id")
@@ -34,8 +34,8 @@ async def get_module(
3434

3535
@inject
3636
async def refresh_module(
37-
context: Context = DEFAULT_CONTEXT,
38-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
37+
context: Context = Provide[Container.context],
38+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
3939
) -> Module | None:
4040
"""Refresh module in context (always fetch)."""
4141
module = await get_module(context=context, mpt_operations=mpt_operations)

seed/accounts/seller.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# mypy: disable-error-code=unreachable
21
import logging
32
import uuid
43

5-
from dependency_injector.wiring import inject
4+
from dependency_injector.wiring import Provide, inject
65

76
from mpt_api_client import AsyncMPTClient
87
from mpt_api_client.resources.accounts.sellers import Seller
8+
from seed.container import Container
99
from seed.context import Context
10-
from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS
1110

1211
logger = logging.getLogger(__name__)
1312

1413

1514
@inject
1615
async def get_seller(
17-
context: Context = DEFAULT_CONTEXT,
18-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
16+
context: Context = Provide[Container.context],
17+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
1918
) -> Seller | None:
2019
"""Get seller from context or fetch from API."""
2120
seller_id = context.get_string("accounts.seller.id")
@@ -33,7 +32,6 @@ async def get_seller(
3332
return seller
3433

3534

36-
@inject
3735
def build_seller_data(external_id: str | None = None) -> dict[str, object]:
3836
"""Get seller data dictionary for creation."""
3937
if external_id is None:
@@ -54,11 +52,11 @@ def build_seller_data(external_id: str | None = None) -> dict[str, object]:
5452

5553
@inject
5654
async def init_seller(
57-
context: Context = DEFAULT_CONTEXT,
58-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
55+
context: Context = Provide[Container.context],
56+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
5957
) -> Seller | None:
6058
"""Get or create seller. Returns Seller if successful, None otherwise."""
61-
seller = await get_seller(context=context, mpt_operations=mpt_operations)
59+
seller = await get_seller()
6260
if seller is None:
6361
logger.debug("Creating seller ...")
6462
seller_data = build_seller_data()
@@ -68,13 +66,12 @@ async def init_seller(
6866
context["accounts.seller.id"] = created.id
6967
logger.info("Seller created: %s", created.id)
7068
return created
71-
logger.warning("Seller creation failed")
69+
logger.warning("Seller creation failed") # type: ignore[unreachable]
7270
return None
7371
logger.info("Seller already exists: %s", seller.id)
7472
return seller
7573

7674

77-
@inject
7875
async def seed_seller() -> None:
7976
"""Seed seller."""
8077
logger.debug("Seeding seller ...")

seed/accounts/user_group.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
import logging
33
import os
44

5-
from dependency_injector.wiring import inject
5+
from dependency_injector.wiring import Provide, inject
66

77
from mpt_api_client import AsyncMPTClient
88
from mpt_api_client.resources.accounts.user_groups import UserGroup
9+
from seed.container import Container
910
from seed.context import Context
10-
from seed.defaults import DEFAULT_CONTEXT, DEFAULT_MPT_OPERATIONS
1111

1212
logger = logging.getLogger(__name__)
1313

1414

1515
@inject
1616
async def get_user_group(
17-
context: Context = DEFAULT_CONTEXT,
18-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
17+
context: Context = Provide[Container.context],
18+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
1919
) -> UserGroup | None:
2020
"""Get user group from context or fetch from API."""
2121
user_group_id = context.get_string("accounts.user_group.id")
@@ -35,7 +35,7 @@ async def get_user_group(
3535

3636
@inject
3737
def build_user_group_data(
38-
context: Context = DEFAULT_CONTEXT,
38+
context: Context = Provide[Container.context],
3939
) -> dict[str, object]:
4040
"""Get user group data dictionary for creation."""
4141
account_id = os.getenv("CLIENT_ACCOUNT_ID")
@@ -54,8 +54,8 @@ def build_user_group_data(
5454

5555
@inject
5656
async def init_user_group(
57-
context: Context = DEFAULT_CONTEXT,
58-
mpt_operations: AsyncMPTClient = DEFAULT_MPT_OPERATIONS,
57+
context: Context = Provide[Container.context],
58+
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
5959
) -> UserGroup | None:
6060
"""Get or create user group."""
6161
user_group = await get_user_group(context=context, mpt_operations=mpt_operations)

seed/catalog/authorization.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import uuid
2+
3+
from dependency_injector.wiring import Provide, inject
4+
5+
from mpt_api_client import AsyncMPTClient
6+
from mpt_api_client.resources.catalog.authorizations import Authorization
7+
from seed.container import Container
8+
from seed.context import Context
9+
from seed.helper import init_resource, require_context_id
10+
11+
12+
async def seed_authorization() -> None:
13+
"""Seed authorization."""
14+
await init_resource("catalog.authorization.id", create_authorization)
15+
16+
17+
@inject
18+
async def create_authorization(
19+
operations: AsyncMPTClient = Provide[Container.mpt_operations],
20+
context: Context = Provide[Container.context],
21+
) -> Authorization:
22+
"""Creates an authorization."""
23+
product_id = require_context_id(context, "catalog.product.id", "Create authorization")
24+
seller_id = require_context_id(context, "accounts.seller.id", "Create authorization")
25+
account_id = require_context_id(context, "accounts.account.id", "Create authorization")
26+
short_uuid = uuid.uuid4().hex[:8]
27+
28+
authorization_data = {
29+
"externalIds": {"operations": f"e2e-seeded-{short_uuid}"},
30+
"product": {"id": product_id},
31+
"owner": {"id": seller_id},
32+
"journal": {"firstInvoiceDate": "2025-12-01", "frequency": "1m"},
33+
"eligibility": {"client": True, "partner": True},
34+
"currency": "USD",
35+
"notes": "E2E Seeded",
36+
"name": "E2E Seeded",
37+
"vendor": {"id": account_id},
38+
}
39+
return await operations.catalog.authorizations.create(authorization_data)

0 commit comments

Comments
 (0)