Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PC-31951)[API] fix: Event products can only be of type SEANCE_CINE #14235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions api/src/pcapi/core/offers/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@
from . import models


EVENT_PRODUCT_SUBCATEGORIES_IDS = [subcategories.SEANCE_CINE.id]
THINGS_PRODUCT_SUBCATEGORIES_IDS = [
subcategories.LIVRE_PAPIER.id,
subcategories.SUPPORT_PHYSIQUE_MUSIQUE_CD.id,
subcategories.SUPPORT_PHYSIQUE_MUSIQUE_VINYLE.id,
]
ALL_PRODUCT_SUBCATEGORIES_IDS = EVENT_PRODUCT_SUBCATEGORIES_IDS + THINGS_PRODUCT_SUBCATEGORIES_IDS


class ProductFactory(BaseFactory):
AVAILABLE_SUBCATEGORIES = ALL_PRODUCT_SUBCATEGORIES_IDS

class Meta:
model = models.Product
exclude = ("AVAILABLE_SUBCATEGORIES",)

subcategoryId = subcategories.SUPPORT_PHYSIQUE_FILM.id
subcategoryId = subcategories.LIVRE_PAPIER.id
name = factory.Sequence("Product {}".format)
description = factory.Sequence("A passionate description of product {}".format)

Expand All @@ -37,6 +49,9 @@ def _create(
**kwargs: typing.Any,
) -> models.Product:
# Graciously provide the required idAtProviders if lastProvider is given.
if kwargs["subcategoryId"] not in cls.AVAILABLE_SUBCATEGORIES:
raise ValueError(f"Events products subcategory can only be one of {cls.AVAILABLE_SUBCATEGORIES}.")

if kwargs.get("lastProvider") and not kwargs.get("idAtProviders"):
kwargs["idAtProviders"] = uuid.uuid4()

Expand All @@ -62,11 +77,13 @@ class Meta:


class EventProductFactory(ProductFactory):
AVAILABLE_SUBCATEGORIES = EVENT_PRODUCT_SUBCATEGORIES_IDS
subcategoryId = subcategories.SEANCE_CINE.id


class ThingProductFactory(ProductFactory):
subcategoryId = subcategories.SUPPORT_PHYSIQUE_FILM.id
AVAILABLE_SUBCATEGORIES = THINGS_PRODUCT_SUBCATEGORIES_IDS
subcategoryId = subcategories.SUPPORT_PHYSIQUE_MUSIQUE_CD.id


def build_extra_data_from_subcategory(
Expand Down
10 changes: 2 additions & 8 deletions api/src/pcapi/sandboxes/scripts/creators/industrial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_eac_data import create_eac_data
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_event_occurrences import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_event_offers import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_event_products import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_event_stocks import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_incidents import create_industrial_incidents
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_individual_offerers import (
Expand All @@ -41,7 +40,6 @@
create_industrial_search_indexed_objects,
)
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_thing_offers import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_thing_products import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_thing_stocks import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_venues import *
from pcapi.sandboxes.scripts.creators.industrial.create_industrial_venues_with_timezone import (
Expand Down Expand Up @@ -79,13 +77,9 @@ def save_industrial_sandbox() -> None:

venues_by_name = create_industrial_venues(offerers_by_name)

event_products_by_name = create_industrial_event_products()
event_offers_by_name = create_industrial_event_offers(offerers_by_name)

thing_products_by_name = create_industrial_thing_products()

event_offers_by_name = create_industrial_event_offers(event_products_by_name, offerers_by_name)

thing_offers_by_name = create_industrial_thing_offers(thing_products_by_name, offerers_by_name, venues_by_name)
thing_offers_by_name = create_industrial_thing_offers(offerers_by_name, venues_by_name)

create_industrial_draft_offers(offerers_by_name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_industrial_event_occurrences(
price_categories: dict[decimal.Decimal, offers_models.PriceCategory] = {}
for index, beginning_datetime in enumerate(EVENT_OCCURRENCE_BEGINNING_DATETIMES, start=1):
name = "{} / {} / {} ".format(
event_offer_with_occurrences.product.name if event_offer_with_occurrences.product else "",
event_offer_with_occurrences.name,
event_offer_with_occurrences.venue.name,
beginning_datetime.strftime(date_utils.DATE_ISO_FORMAT),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging

from pcapi.core.categories import subcategories_v2
import pcapi.core.offerers.models as offerers_models
import pcapi.core.offers.factories as offers_factories
import pcapi.core.offers.models as offers_models
from pcapi.repository import repository
from pcapi.sandboxes.scripts.mocks.event_mocks import MOCK_NAMES


logger = logging.getLogger(__name__)
Expand All @@ -14,7 +16,6 @@


def create_industrial_event_offers(
events_by_name: dict[str, offers_models.Product],
offerers_by_name: dict[str, offerers_models.Offerer],
) -> dict[str, offers_models.Offer]:
logger.info("create_industrial_event_offers")
Expand All @@ -24,7 +25,7 @@ def create_industrial_event_offers(
event_index = 0
offer_index = 0

event_items = list(events_by_name.items())
event_subcategories = [s for s in subcategories_v2.ALL_SUBCATEGORIES if s.is_event and s.is_offline_only]

for offerer in offerers_by_name.values():
event_venues = [venue for venue in offerer.managedVenues if not venue.isVirtual]
Expand All @@ -35,9 +36,10 @@ def create_industrial_event_offers(
event_venue = event_venues[0]

for venue_event_index in range(0, EVENTS_PER_OFFERER_WITH_PHYSICAL_VENUE):
rest_event_index = (venue_event_index + event_index) % len(event_items)

(event_name, event) = event_items[rest_event_index]
event_subcategory_index = (venue_event_index + event_index) % len(event_subcategories)
event_subcategory = event_subcategories[event_subcategory_index]
mock_index = (venue_event_index + event_index) % len(MOCK_NAMES)
event_name = MOCK_NAMES[mock_index]

name = "{} / {}".format(event_name, event_venue.name)
if offer_index % DEACTIVATED_OFFERS_PICK_MODULO == 0:
Expand All @@ -50,8 +52,10 @@ def create_industrial_event_offers(
is_duo = True
event_offers_by_name[name] = offers_factories.OfferFactory(
venue=event_venue,
product=event,
extraData=event.extraData,
subcategoryId=event_subcategory.id,
extraData=offers_factories.build_extra_data_from_subcategory(
event_subcategory.id, set_all_fields=False
),
isActive=is_active,
isDuo=is_duo,
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging

from pcapi.core.categories import subcategories_v2
import pcapi.core.offerers.models as offerers_models
import pcapi.core.offers.factories as offers_factories
import pcapi.core.offers.models as offers_models
from pcapi.repository import repository
from pcapi.sandboxes.scripts.mocks.thing_mocks import MOCK_NAMES


logger = logging.getLogger(__name__)
Expand All @@ -14,18 +16,18 @@


def create_industrial_thing_offers(
thing_products_by_name: dict[str, offers_models.Product],
offerers_by_name: dict[str, offerers_models.Offerer],
venues_by_name: dict[str, offerers_models.Venue],
) -> dict[str, offers_models.Offer]:
logger.info("create_industrial_thing_offers")

thing_offers_by_name: dict[str, offers_models.Offer] = {}

thing_subcategories = [s for s in subcategories_v2.ALL_SUBCATEGORIES if not s.is_event]

id_at_provider = 1234
thing_index = 0
offer_index = 0
thing_items = list(thing_products_by_name.items())
for offerer in offerers_by_name.values():
virtual_venue = [venue for venue in offerer.managedVenues if venue.isVirtual][0]

Expand All @@ -34,17 +36,20 @@ def create_industrial_thing_offers(

for venue_thing_index in range(0, THINGS_PER_OFFERER):
thing_venue = None
while thing_venue is None:
rest_thing_index = (venue_thing_index + thing_index) % len(thing_items)
(thing_name, thing_product) = thing_items[rest_thing_index]
if thing_product.subcategory.is_offline_only:
thing_venue = physical_venue
elif thing_product.subcategory.is_online_only:
thing_venue = virtual_venue
else:
thing_venue = physical_venue

thing_index += 1
subcategory_index = (venue_thing_index + thing_index) % len(thing_subcategories)
subcategory = thing_subcategories[subcategory_index]
thing_name_index = (venue_thing_index + thing_index) % len(MOCK_NAMES)
thing_name = MOCK_NAMES[thing_name_index]

if subcategory.is_offline_only:
thing_venue = physical_venue
elif subcategory.is_online_only:
thing_venue = virtual_venue
else:
thing_venue = physical_venue

if thing_venue is None:
continue

name = "{} / {}".format(thing_name, thing_venue.name)
if offer_index % DEACTIVATED_OFFERS_PICK_MODULO == 0:
Expand All @@ -53,11 +58,11 @@ def create_industrial_thing_offers(
is_active = True
thing_offers_by_name[name] = offers_factories.OfferFactory(
venue=thing_venue,
subcategoryId=thing_product.subcategoryId,
subcategoryId=subcategory.id,
isActive=is_active,
url="http://example.com" if thing_product.subcategory.is_online_only else None,
url="http://example.com" if subcategory.is_online_only else None,
idAtProvider=str(id_at_provider),
extraData=thing_product.extraData,
extraData=offers_factories.build_extra_data_from_subcategory(subcategory.id, set_all_fields=False),
)
offer_index += 1
id_at_provider += 1
Expand Down
Loading
Loading