add typing

This commit is contained in:
Mira Weller
2024-07-15 09:40:53 +02:00
parent 8cfb69c265
commit 3fe6919bef
3 changed files with 11 additions and 12 deletions

View File

@@ -42,7 +42,7 @@ from collections import Counter, OrderedDict, defaultdict
from datetime import date, datetime, time, timedelta from datetime import date, datetime, time, timedelta
from decimal import Decimal, DecimalException from decimal import Decimal, DecimalException
from itertools import groupby from itertools import groupby
from typing import Optional, Tuple from typing import List, Optional, Tuple
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import dateutil.parser import dateutil.parser
@@ -64,14 +64,13 @@ from django_countries.fields import Country
from django_scopes import ScopedManager from django_scopes import ScopedManager
from i18nfield.fields import I18nCharField, I18nTextField from i18nfield.fields import I18nCharField, I18nTextField
from pretix.base.media import MEDIA_TYPES
from pretix.base.models import CartPosition, Event, SalesChannel, SubEvent
from pretix.base.models.base import LoggedModel from pretix.base.models.base import LoggedModel
from pretix.base.models.fields import MultiStringField from pretix.base.models.fields import MultiStringField
from pretix.base.models.tax import TaxedPrice from pretix.base.models.tax import TaxedPrice
from pretix.base.timemachine import time_machine_now from pretix.base.timemachine import time_machine_now
from pretix.helpers.images import ImageSizeValidator
from ...helpers.images import ImageSizeValidator
from ..media import MEDIA_TYPES
from .event import Event, SubEvent
class ItemCategory(LoggedModel): class ItemCategory(LoggedModel):
@@ -145,7 +144,7 @@ class ItemCategory(LoggedModel):
verbose_name_plural = _("Product categories") verbose_name_plural = _("Product categories")
ordering = ('position', 'id') ordering = ('position', 'id')
def cross_sell_visible(self, cart, sales_channel): def cross_sell_visible(self, cartpositions: List[CartPosition], sales_channel: SalesChannel):
""" """
If this category should be visible in the cross-selling step for a given cart and sales_channel, this method If this category should be visible in the cross-selling step for a given cart and sales_channel, this method
returns a queryset of the items that should be displayed, as well as a dict giving additional information on them. returns a queryset of the items that should be displayed, as well as a dict giving additional information on them.
@@ -160,7 +159,7 @@ class ItemCategory(LoggedModel):
return self.items.all(), {} return self.items.all(), {}
if self.cross_selling_condition == 'products': if self.cross_selling_condition == 'products':
match = set(match.pk for match in self.cross_selling_match_products.only('pk')) # TODO prefetch this match = set(match.pk for match in self.cross_selling_match_products.only('pk')) # TODO prefetch this
return (self.items.all(), {}) if any(pos.item.pk in match for pos in cart) else (None, {}) return (self.items.all(), {}) if any(pos.item.pk in match for pos in cartpositions) else (None, {})
if self.cross_selling_condition == 'discounts': if self.cross_selling_condition == 'discounts':
if not hasattr(self.event, '_potential_discounts_by_item_for_current_cart'): if not hasattr(self.event, '_potential_discounts_by_item_for_current_cart'):
potential_discounts_by_cartpos = defaultdict(list) potential_discounts_by_cartpos = defaultdict(list)
@@ -172,7 +171,7 @@ class ItemCategory(LoggedModel):
[ [
(cp.item_id, cp.subevent_id, cp.line_price_gross, bool(cp.addon_to), cp.is_bundled, (cp.item_id, cp.subevent_id, cp.line_price_gross, bool(cp.addon_to), cp.is_bundled,
cp.listed_price - cp.price_after_voucher) cp.listed_price - cp.price_after_voucher)
for cp in cart for cp in cartpositions
], ],
collect_potential_discounts=potential_discounts_by_cartpos collect_potential_discounts=potential_discounts_by_cartpos
) )

View File

@@ -156,7 +156,7 @@ def get_line_price(price_after_voucher: Decimal, custom_price_input: Decimal, cu
return price return price
def apply_discounts(event: Event, sales_channel: str, def apply_discounts(event: Event, sales_channel: str | SalesChannel,
positions: List[Tuple[int, Optional[int], Decimal, bool, bool, Decimal]], positions: List[Tuple[int, Optional[int], Decimal, bool, bool, Decimal]],
collect_potential_discounts=None) -> List[Decimal]: collect_potential_discounts=None) -> List[Decimal]:
""" """

View File

@@ -57,7 +57,7 @@ from django.views.generic.base import TemplateResponseMixin
from django_scopes import scopes_disabled from django_scopes import scopes_disabled
from pretix.base.models import Customer, Membership, Order from pretix.base.models import Customer, Membership, Order
from pretix.base.models.items import Question, ItemCategory from pretix.base.models.items import ItemCategory, Question
from pretix.base.models.orders import ( from pretix.base.models.orders import (
InvoiceAddress, OrderPayment, QuestionAnswer, InvoiceAddress, OrderPayment, QuestionAnswer,
) )
@@ -632,11 +632,11 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep):
@property @property
def cross_selling_applicable_categories(self): def cross_selling_applicable_categories(self):
cart = self.positions cartpositions = self.positions
return [ return [
(c, products_qs, discount_info) for (c, products_qs, discount_info) in (c, products_qs, discount_info) for (c, products_qs, discount_info) in
( (
(c, *c.cross_sell_visible(cart, self.request.sales_channel.identifier)) (c, *c.cross_sell_visible(cartpositions, self.request.sales_channel.identifier))
for c in self.request.event.categories.filter(cross_selling_mode__isnull=False) for c in self.request.event.categories.filter(cross_selling_mode__isnull=False)
) )
if products_qs is not None if products_qs is not None