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-32883)[API] chore: add atomic to offer routes #14942

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion api/src/pcapi/core/offers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ def batch_delete_draft_offers(query: BaseQuery) -> None:
synchronize_session=False
)
models.Offer.query.filter(*filters).delete(synchronize_session=False)
db.session.commit()
db.session.flush()


def batch_delete_stocks(
Expand Down
2 changes: 1 addition & 1 deletion api/src/pcapi/core/offers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ def hard_delete_filtered_stocks(
subquery = get_filtered_stocks(offer=offer, venue=venue, date=date, time=time, price_category_id=price_category_id)
subquery = subquery.with_entities(models.Stock.id)
models.Stock.query.filter(models.Stock.id.in_(subquery)).delete(synchronize_session=False)
db.session.commit()
db.session.flush()


def get_paginated_stocks(
Expand Down
22 changes: 17 additions & 5 deletions api/src/pcapi/routes/pro/offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pcapi.core.offers.validation import check_product_cgu_and_offerer
from pcapi.models import api_errors
from pcapi.models import db
from pcapi.repository import atomic
from pcapi.repository import transaction
from pcapi.routes.apis import private_api
from pcapi.routes.serialization import offers_serialize
Expand All @@ -45,6 +46,7 @@
response_model=offers_serialize.ListOffersResponseModel,
api=blueprint.pro_private_schema,
)
@atomic()
def list_offers(query: offers_serialize.ListOffersQueryModel) -> offers_serialize.ListOffersResponseModel:
paginated_offers = offers_repository.get_capped_offers_for_filters(
user_id=current_user.id,
Expand All @@ -70,6 +72,7 @@ def list_offers(query: offers_serialize.ListOffersQueryModel) -> offers_serializ
response_model=offers_serialize.GetIndividualOfferWithAddressResponseModel,
api=blueprint.pro_private_schema,
)
@atomic()
def get_offer(offer_id: int) -> offers_serialize.GetIndividualOfferWithAddressResponseModel:

load_all: offers_repository.OFFER_LOAD_OPTIONS = [
Expand Down Expand Up @@ -102,6 +105,7 @@ def get_offer(offer_id: int) -> offers_serialize.GetIndividualOfferWithAddressRe
response_model=offers_serialize.GetStocksResponseModel,
api=blueprint.pro_private_schema,
)
@atomic()
def get_stocks(offer_id: int, query: offers_serialize.StocksQueryModel) -> offers_serialize.GetStocksResponseModel:
try:
offer = offers_repository.get_offer_by_id(offer_id, load_options=["offerer_address"])
Expand Down Expand Up @@ -145,6 +149,7 @@ def get_stocks(offer_id: int, query: offers_serialize.StocksQueryModel) -> offer
on_success_status=204,
api=blueprint.pro_private_schema,
)
@atomic()
def delete_stocks(offer_id: int, body: offers_serialize.DeleteStockListBody) -> None:
try:
offer = offers_repository.get_offer_by_id(offer_id)
Expand All @@ -167,6 +172,7 @@ def delete_stocks(offer_id: int, body: offers_serialize.DeleteStockListBody) ->
on_success_status=204,
api=blueprint.pro_private_schema,
)
@atomic()
def delete_all_filtered_stocks(offer_id: int, body: offers_serialize.DeleteFilteredStockListBody) -> None:
try:
offer = offers_repository.get_offer_by_id(offer_id, load_options=["offerer_address"])
Expand Down Expand Up @@ -194,6 +200,7 @@ def delete_all_filtered_stocks(offer_id: int, body: offers_serialize.DeleteFilte
response_model=offers_serialize.StockStatsResponseModel,
api=blueprint.pro_private_schema,
)
@atomic()
def get_stocks_stats(offer_id: int) -> offers_serialize.StockStatsResponseModel:
try:
offer = offers_repository.get_offer_by_id(offer_id)
Expand All @@ -220,6 +227,7 @@ def get_stocks_stats(offer_id: int) -> offers_serialize.StockStatsResponseModel:
on_success_status=204,
api=blueprint.pro_private_schema,
)
@atomic()
def delete_draft_offers(body: offers_serialize.DeleteOfferRequestBody) -> None:
if not body.ids:
raise api_errors.ApiErrors(
Expand All @@ -239,6 +247,7 @@ def delete_draft_offers(body: offers_serialize.DeleteOfferRequestBody) -> None:
on_success_status=201,
api=blueprint.pro_private_schema,
)
@atomic()
def post_draft_offer(
body: offers_schemas.PostDraftOfferBodyModel,
) -> offers_serialize.GetIndividualOfferResponseModel:
Expand All @@ -257,12 +266,11 @@ def post_draft_offer(

rest.check_user_has_access_to_offerer(current_user, venue.managingOffererId)

try:
with repository.transaction():
with atomic():
try:
Comment on lines +269 to +270
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Meewan ici aussi c'est inutile non ?

offer = offers_api.create_draft_offer(body, venue, product)
except exceptions.OfferCreationBaseException as error:
raise api_errors.ApiErrors(error.errors, status_code=400)

except exceptions.OfferCreationBaseException as error:
raise api_errors.ApiErrors(error.errors, status_code=400)
return offers_serialize.GetIndividualOfferResponseModel.from_orm(offer)


Expand Down Expand Up @@ -498,6 +506,7 @@ def delete_thumbnail(offer_id: int) -> None:
response_model=offers_serialize.CategoriesResponseModel,
api=blueprint.pro_private_schema,
)
@atomic()
def get_categories() -> offers_serialize.CategoriesResponseModel:
return offers_serialize.CategoriesResponseModel(
categories=[
Expand All @@ -516,6 +525,7 @@ def get_categories() -> offers_serialize.CategoriesResponseModel:
response_model=offers_serialize.SuggestedSubcategoriesResponseModel,
api=blueprint.pro_private_schema,
)
@atomic()
def get_suggested_subcategories(
query: offers_serialize.SuggestedSubcategoriesQueryModel,
) -> offers_serialize.SuggestedSubcategoriesResponseModel:
Expand All @@ -531,6 +541,7 @@ def get_suggested_subcategories(
response_model=offers_serialize.GetMusicTypesResponse,
api=blueprint.pro_private_schema,
)
@atomic()
def get_music_types() -> offers_serialize.GetMusicTypesResponse:
return offers_serialize.GetMusicTypesResponse(
__root__=[
Expand Down Expand Up @@ -632,6 +643,7 @@ def delete_price_category(offer_id: int, price_category_id: int) -> None:
response_model=offers_serialize.GetProductInformations,
api=blueprint.pro_private_schema,
)
@atomic()
def get_product_by_ean(ean: str, offerer_id: int) -> offers_serialize.GetProductInformations:
product = (
models.Product.query.filter(models.Product.extraData["ean"].astext == ean)
Expand Down
3 changes: 2 additions & 1 deletion api/tests/routes/pro/get_offer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Returns403Test:
# get user
# get offer
# check user_offerer exists
num_queries = 4
# rollback
num_queries = 5

def test_access_by_beneficiary(self, client):
beneficiary = users_factories.BeneficiaryGrant18Factory()
Expand Down