mirror of
https://github.com/pretix/pretix.git
synced 2026-05-03 14:54:04 +00:00
Reduce functional complexity (McCabe max 18 → max 12)
This commit is contained in:
@@ -50,24 +50,14 @@ class LocaleMiddleware(BaseLocaleMiddleware):
|
||||
return response
|
||||
|
||||
|
||||
def get_language_from_request(request) -> str:
|
||||
"""
|
||||
Analyzes the request to find what language the user wants the system to
|
||||
show. Only languages listed in settings.LANGUAGES are taken into account.
|
||||
If the user requests a sublanguage where we have a main language, we send
|
||||
out the main language.
|
||||
"""
|
||||
global _supported
|
||||
if _supported is None:
|
||||
_supported = OrderedDict(settings.LANGUAGES)
|
||||
|
||||
# Priority 1: User settings
|
||||
def get_language_from_user_settings(request) -> str:
|
||||
if request.user.is_authenticated():
|
||||
lang_code = request.user.locale
|
||||
if lang_code in _supported and lang_code is not None and check_for_language(lang_code):
|
||||
return lang_code
|
||||
|
||||
# Priority 2: Anonymous user settings (session, cookie)
|
||||
|
||||
def get_language_from_session_or_cookie(request) -> str:
|
||||
if hasattr(request, 'session'):
|
||||
lang_code = request.session.get(LANGUAGE_SESSION_KEY)
|
||||
if lang_code in _supported and lang_code is not None and check_for_language(lang_code):
|
||||
@@ -79,7 +69,8 @@ def get_language_from_request(request) -> str:
|
||||
except LookupError:
|
||||
pass
|
||||
|
||||
# Priority 3: Event default
|
||||
|
||||
def get_language_from_event(request) -> str:
|
||||
if hasattr(request, 'event'):
|
||||
lang_code = request.event.locale
|
||||
try:
|
||||
@@ -87,7 +78,8 @@ def get_language_from_request(request) -> str:
|
||||
except LookupError:
|
||||
pass
|
||||
|
||||
# Priority 4: Browser default
|
||||
|
||||
def get_language_from_browser(request) -> str:
|
||||
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
|
||||
for accept_lang, unused in parse_accept_lang_header(accept):
|
||||
if accept_lang == '*':
|
||||
@@ -101,7 +93,29 @@ def get_language_from_request(request) -> str:
|
||||
except LookupError:
|
||||
continue
|
||||
|
||||
|
||||
def get_default_language():
|
||||
try:
|
||||
return get_supported_language_variant(settings.LANGUAGE_CODE)
|
||||
except LookupError:
|
||||
return settings.LANGUAGE_CODE
|
||||
|
||||
|
||||
def get_language_from_request(request) -> str:
|
||||
"""
|
||||
Analyzes the request to find what language the user wants the system to
|
||||
show. Only languages listed in settings.LANGUAGES are taken into account.
|
||||
If the user requests a sublanguage where we have a main language, we send
|
||||
out the main language.
|
||||
"""
|
||||
global _supported
|
||||
if _supported is None:
|
||||
_supported = OrderedDict(settings.LANGUAGES)
|
||||
|
||||
return (
|
||||
get_language_from_user_settings(request)
|
||||
or get_language_from_session_or_cookie(request)
|
||||
or get_language_from_event(request)
|
||||
or get_language_from_browser(request)
|
||||
or get_default_language()
|
||||
)
|
||||
|
||||
@@ -960,6 +960,20 @@ class ItemVariation(Versionable):
|
||||
price = response[0]['price']
|
||||
return price
|
||||
|
||||
def add_values_from_string(self, pk):
|
||||
"""
|
||||
Add values to this ItemVariation using a serialized string of the form
|
||||
``property-id:value-id,ṗroperty-id:value-id``
|
||||
"""
|
||||
for pair in pk.split(","):
|
||||
prop, value = pair.split(":")
|
||||
self.values.add(
|
||||
PropertyValue.objects.current.get(
|
||||
identity=value,
|
||||
prop_id=prop
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class VariationsField(VersionedManyToManyField):
|
||||
"""
|
||||
@@ -1332,7 +1346,27 @@ class QuestionAnswer(Versionable):
|
||||
answer = models.TextField()
|
||||
|
||||
|
||||
class OrderPosition(Versionable):
|
||||
class ObjectWithAnswers:
|
||||
|
||||
def cache_answers(self):
|
||||
"""
|
||||
Creates two properties on the object.
|
||||
(1) answ: a dictionary of question.id → answer string
|
||||
(2) questions: a list of Question objects, extended by an 'answer' property
|
||||
"""
|
||||
self.answ = {}
|
||||
for a in self.answers.all():
|
||||
self.answ[a.question_id] = a.answer
|
||||
self.questions = []
|
||||
for q in self.item.questions.all():
|
||||
if q.identity in self.answ:
|
||||
q.answer = self.answ[q.identity]
|
||||
else:
|
||||
q.answer = ""
|
||||
self.questions.append(q)
|
||||
|
||||
|
||||
class OrderPosition(ObjectWithAnswers, Versionable):
|
||||
"""
|
||||
An OrderPosition is one line of an order, representing one ordered items
|
||||
of a specified type (or variation).
|
||||
@@ -1368,8 +1402,25 @@ class OrderPosition(Versionable):
|
||||
verbose_name = _("Order position")
|
||||
verbose_name_plural = _("Order positions")
|
||||
|
||||
@classmethod
|
||||
def transform_cart_positions(cls, cp: list, order) -> list:
|
||||
ops = []
|
||||
for cartpos in cp:
|
||||
op = OrderPosition(
|
||||
order=order, item=cartpos.item, variation=cartpos.variation,
|
||||
price=cartpos.price, attendee_name=cartpos.attendee_name
|
||||
)
|
||||
for answ in cartpos.answers.all():
|
||||
answ = answ.clone()
|
||||
answ.orderposition = op
|
||||
answ.cartposition = None
|
||||
answ.save()
|
||||
op.save()
|
||||
cartpos.delete()
|
||||
ops.append(op)
|
||||
|
||||
class CartPosition(Versionable):
|
||||
|
||||
class CartPosition(ObjectWithAnswers, Versionable):
|
||||
"""
|
||||
A cart position is similar to a order line, except that it is not
|
||||
yet part of a binding order but just placed by some user in his or
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from functools import partial
|
||||
from itertools import product
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
@@ -9,7 +10,7 @@ from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from pretix.base.forms import VersionedModelForm
|
||||
|
||||
from pretix.base.models import ItemVariation, PropertyValue, Item
|
||||
from pretix.base.models import ItemVariation, Item
|
||||
|
||||
|
||||
class TolerantFormsetModelForm(VersionedModelForm):
|
||||
@@ -106,6 +107,23 @@ class RestrictionInlineFormset(forms.BaseInlineFormSet):
|
||||
exclude = ['item']
|
||||
|
||||
|
||||
def selector(values, prop):
|
||||
# Given an iterable of PropertyValue objects, this will return a
|
||||
# list of their primary keys, ordered by the primary keys of the
|
||||
# properties they belong to EXCEPT the value for the property prop2.
|
||||
# We'll see later why we need this.
|
||||
return [
|
||||
v.identity for v in sorted(values, key=lambda v: v.prop.identity)
|
||||
if v.prop.identity != prop.identity
|
||||
]
|
||||
|
||||
|
||||
def sort(v, prop):
|
||||
# Given a list of variations, this will sort them by their position
|
||||
# on the x-axis
|
||||
return v[prop.identity].sortkey
|
||||
|
||||
|
||||
class VariationsFieldRenderer(forms.widgets.CheckboxFieldRenderer):
|
||||
"""
|
||||
This is the default renderer for a VariationsField. Based on the choice input class
|
||||
@@ -142,80 +160,9 @@ class VariationsFieldRenderer(forms.widgets.CheckboxFieldRenderer):
|
||||
if dimension == 0:
|
||||
output.append(format_html('<em>{0}</em>', _("not applicable")))
|
||||
elif dimension == 1:
|
||||
output.append('<ul>')
|
||||
for i, variation in enumerate(variations):
|
||||
final_attrs = dict(
|
||||
self.attrs.copy(), type=self.choice_input_class.input_type,
|
||||
name=self.name, value=variation['key']
|
||||
)
|
||||
if variation['key'] in self.value:
|
||||
final_attrs['checked'] = 'checked'
|
||||
w = self.choice_input_class(
|
||||
self.name, self.value, self.attrs.copy(),
|
||||
(variation['key'], variation[properties[0].identity].value),
|
||||
i
|
||||
)
|
||||
output.append(format_html('<li>{0}</li>', force_text(w)))
|
||||
output.append('</ul>')
|
||||
|
||||
elif dimension >= 2:
|
||||
# prop1 is the property on all the grid's y-axes
|
||||
prop1 = properties[0]
|
||||
prop1v = list(prop1.values.current.all())
|
||||
# prop2 is the property on all the grid's x-axes
|
||||
prop2 = properties[1]
|
||||
prop2v = list(prop2.values.current.all())
|
||||
|
||||
def selector(values):
|
||||
# Given an iterable of PropertyValue objects, this will return a
|
||||
# list of their primary keys, ordered by the primary keys of the
|
||||
# properties they belong to EXCEPT the value for the property prop2.
|
||||
# We'll see later why we need this.
|
||||
return [
|
||||
v.identity for v in sorted(values, key=lambda v: v.prop.identity)
|
||||
if v.prop.identity != prop2.identity
|
||||
]
|
||||
|
||||
def sort(v):
|
||||
# Given a list of variations, this will sort them by their position
|
||||
# on the x-axis
|
||||
return v[prop2.identity].sortkey
|
||||
|
||||
# We now iterate over the cartesian product of all the other
|
||||
# properties which are NOT on the axes of the grid because we
|
||||
# create one grid for any combination of them.
|
||||
for gridrow in product(*[prop.values.current.all() for prop in properties[2:]]):
|
||||
if len(gridrow) > 0:
|
||||
output.append('<strong>')
|
||||
output.append(", ".join([value.value for value in gridrow]))
|
||||
output.append('</strong>')
|
||||
output.append('<table class="table"><thead><tr><th></th>')
|
||||
for val2 in prop2v:
|
||||
output.append(format_html('<th>{0}</th>', val2.value))
|
||||
output.append('</thead><tbody>')
|
||||
for val1 in prop1v:
|
||||
output.append(format_html('<tr><th>{0}</th>', val1.value))
|
||||
# We are now inside one of the rows of the grid and have to
|
||||
# select the variations to display in this row. In order to
|
||||
# achieve this, we use the 'selector' lambda defined above.
|
||||
# It gives us a normalized, comparable version of a set of
|
||||
# PropertyValue objects. In this case, we compute the
|
||||
# selector of our row as the selector of the sum of the
|
||||
# values defining our grind and the value defining our row.
|
||||
selection = selector(gridrow + (val1,))
|
||||
# We now iterate over all variations who generate the same
|
||||
# selector as 'selection'.
|
||||
filtered = [v for v in variations if selector(v.relevant_values()) == selection]
|
||||
for variation in sorted(filtered, key=sort):
|
||||
final_attrs = dict(
|
||||
self.attrs.copy(), type=self.choice_input_class.input_type,
|
||||
name=self.name, value=variation['key']
|
||||
)
|
||||
if variation['key'] in self.value:
|
||||
final_attrs['checked'] = 'checked'
|
||||
output.append(format_html('<td><label><input{0} /></label></td>', flatatt(final_attrs)))
|
||||
output.append('</td>')
|
||||
output.append('</tbody></table>')
|
||||
output = self.render_1d(output, variations, properties)
|
||||
else:
|
||||
output = self.render_nd(output, variations, properties)
|
||||
output.append(
|
||||
('<div class="help-block"><a href="#" class="variations-select-all">{0}</a> · '
|
||||
'<a href="#" class="variations-select-none">{1}</a></div></div>').format(
|
||||
@@ -225,6 +172,67 @@ class VariationsFieldRenderer(forms.widgets.CheckboxFieldRenderer):
|
||||
)
|
||||
return mark_safe('\n'.join(output))
|
||||
|
||||
def render_1d(self, output, variations, properties):
|
||||
output.append('<ul>')
|
||||
for i, variation in enumerate(variations):
|
||||
final_attrs = dict(
|
||||
self.attrs.copy(), type=self.choice_input_class.input_type,
|
||||
name=self.name, value=variation['key']
|
||||
)
|
||||
if variation['key'] in self.value:
|
||||
final_attrs['checked'] = 'checked'
|
||||
w = self.choice_input_class(
|
||||
self.name, self.value, self.attrs.copy(),
|
||||
(variation['key'], variation[properties[0].identity].value),
|
||||
i
|
||||
)
|
||||
output.append(format_html('<li>{0}</li>', force_text(w)))
|
||||
output.append('</ul>')
|
||||
return output
|
||||
|
||||
def render_bd(self, output, variations, properties):
|
||||
# prop1 is the property on all the grid's y-axes
|
||||
prop1 = properties[0]
|
||||
prop1v = list(prop1.values.current.all())
|
||||
# prop2 is the property on all the grid's x-axes
|
||||
prop2 = properties[1]
|
||||
prop2v = list(prop2.values.current.all())
|
||||
|
||||
# We now iterate over the cartesian product of all the other
|
||||
# properties which are NOT on the axes of the grid because we
|
||||
# create one grid for any combination of them.
|
||||
for gridrow in product(*[prop.values.current.all() for prop in properties[2:]]):
|
||||
if len(gridrow) > 0:
|
||||
output.append('<strong>')
|
||||
output.append(", ".join([value.value for value in gridrow]))
|
||||
output.append('</strong>')
|
||||
output.append('<table class="table"><thead><tr><th></th>')
|
||||
output.append(*[format_html('<th>{0}</th>', val2.value) for val2 in prop2v])
|
||||
output.append('</thead><tbody>')
|
||||
for val1 in prop1v:
|
||||
output.append(format_html('<tr><th>{0}</th>', val1.value))
|
||||
# We are now inside one of the rows of the grid and have to
|
||||
# select the variations to display in this row. In order to
|
||||
# achieve this, we use the 'selector' lambda defined above.
|
||||
# It gives us a normalized, comparable version of a set of
|
||||
# PropertyValue objects. In this case, we compute the
|
||||
# selector of our row as the selector of the sum of the
|
||||
# values defining our grind and the value defining our row.
|
||||
selection = selector(gridrow + (val1,), prop2)
|
||||
# We now iterate over all variations who generate the same
|
||||
# selector as 'selection'.
|
||||
filtered = [v for v in variations if selector(v.relevant_values(), prop2) == selection]
|
||||
for variation in sorted(filtered, key=partial(sort, prop=prop2)):
|
||||
final_attrs = dict(
|
||||
self.attrs.copy(), type=self.choice_input_class.input_type,
|
||||
name=self.name, value=variation['key']
|
||||
)
|
||||
if variation['key'] in self.value:
|
||||
final_attrs['checked'] = 'checked'
|
||||
output.append(format_html('<td><label><input{0} /></label></td>', flatatt(final_attrs)))
|
||||
output.append('</td>')
|
||||
output.append('</tbody></table>')
|
||||
|
||||
|
||||
class VariationsCheckboxRenderer(VariationsFieldRenderer):
|
||||
"""
|
||||
@@ -312,13 +320,9 @@ class VariationsField(forms.ModelMultipleChoiceField):
|
||||
# For implementation details, see ItemVariation.get_all_variations()
|
||||
# which uses a very similar method
|
||||
all_variations = self.item.variations.all().prefetch_related("values")
|
||||
variations_cache = {}
|
||||
for var in all_variations:
|
||||
key = []
|
||||
for v in var.values.all():
|
||||
key.append((v.prop_id, v.identity))
|
||||
key = tuple(sorted(key))
|
||||
variations_cache[key] = var.identity
|
||||
variations_cache = {
|
||||
var.to_variation_dict().identify(): var.identity for var in all_variations
|
||||
}
|
||||
|
||||
cleaned_value = []
|
||||
|
||||
@@ -330,10 +334,7 @@ class VariationsField(forms.ModelMultipleChoiceField):
|
||||
# A combination of PropertyValues was given
|
||||
|
||||
# Hash the combination in the same way as in our cache above
|
||||
key = []
|
||||
for pair in pk.split(","):
|
||||
key.append(tuple([i for i in pair.split(":")]))
|
||||
key = tuple(sorted(key))
|
||||
key = ",".join([pair.split(":")[1] for pair in sorted(pk.split(","))])
|
||||
|
||||
if key in variations_cache:
|
||||
# An ItemVariation object already exists for this variation,
|
||||
@@ -348,21 +349,14 @@ class VariationsField(forms.ModelMultipleChoiceField):
|
||||
var.item_id = self.item.identity
|
||||
var.save()
|
||||
# Add the values to the ItemVariation object
|
||||
for pair in pk.split(","):
|
||||
prop, value = pair.split(":")
|
||||
try:
|
||||
var.values.add(
|
||||
PropertyValue.objects.current.get(
|
||||
identity=value,
|
||||
prop_id=prop
|
||||
)
|
||||
)
|
||||
except PropertyValue.DoesNotExist:
|
||||
raise ValidationError(
|
||||
self.error_messages['invalid_pk_value'],
|
||||
code='invalid_pk_value',
|
||||
params={'pk': value},
|
||||
)
|
||||
try:
|
||||
var.add_values_from_string(pk)
|
||||
except:
|
||||
raise ValidationError(
|
||||
self.error_messages['invalid_pk_value'],
|
||||
code='invalid_pk_value',
|
||||
params={'pk': value},
|
||||
)
|
||||
variations_cache[key] = var.identity
|
||||
cleaned_value.append(str(var.identity))
|
||||
else:
|
||||
|
||||
@@ -11,6 +11,16 @@ from pretix.control.signals import restriction_formset
|
||||
from .models import TimeRestriction
|
||||
|
||||
|
||||
# The maximum validity of our cached values is the next date, one of our
|
||||
# timeframe_from or tiemframe_to actions happens
|
||||
def timediff(restrictions):
|
||||
for r in restrictions:
|
||||
if r.timeframe_from >= now():
|
||||
yield (r.timeframe_from - now()).total_seconds()
|
||||
if r.timeframe_to >= now():
|
||||
yield (r.timeframe_to - now()).total_seconds()
|
||||
|
||||
|
||||
@receiver(determine_availability)
|
||||
def availability_handler(sender, **kwargs):
|
||||
# Handle the signal's input arguments
|
||||
@@ -34,15 +44,6 @@ def availability_handler(sender, **kwargs):
|
||||
# interfere with other plugins.
|
||||
variations = [d.copy() for d in variations]
|
||||
|
||||
# The maximum validity of our cached values is the next date, one of our
|
||||
# timeframe_from or tiemframe_to actions happens
|
||||
def timediff(restrictions):
|
||||
for r in restrictions:
|
||||
if r.timeframe_from >= now():
|
||||
yield (r.timeframe_from - now()).total_seconds()
|
||||
if r.timeframe_to >= now():
|
||||
yield (r.timeframe_to - now()).total_seconds()
|
||||
|
||||
try:
|
||||
cache_validity = min(timediff(restrictions))
|
||||
except ValueError:
|
||||
|
||||
@@ -52,10 +52,9 @@ class CartDisplayMixin:
|
||||
))
|
||||
|
||||
def get_cart(self, answers=False, queryset=None, payment_fee=None):
|
||||
if queryset is None:
|
||||
queryset = CartPosition.objects.current.filter(
|
||||
Q(user=self.request.user) & Q(event=self.request.event)
|
||||
)
|
||||
queryset = queryset or CartPosition.objects.current.filter(
|
||||
Q(user=self.request.user) & Q(event=self.request.event)
|
||||
)
|
||||
|
||||
prefetch = ['variation__values', 'variation__values__prop']
|
||||
if answers:
|
||||
@@ -87,33 +86,18 @@ class CartDisplayMixin:
|
||||
group.total = group.count * group.price
|
||||
group.has_questions = answers and k[0] != ""
|
||||
if answers:
|
||||
group.answ = {}
|
||||
for a in group.answers.all():
|
||||
group.answ[a.question_id] = a.answer
|
||||
group.questions = []
|
||||
for q in group.item.questions.all():
|
||||
if q.identity in group.answ:
|
||||
q.answer = group.answ[q.identity]
|
||||
else:
|
||||
q.answer = ""
|
||||
group.questions.append(q)
|
||||
group.cache_answers()
|
||||
positions.append(group)
|
||||
|
||||
total = sum(p.total for p in positions)
|
||||
|
||||
if payment_fee is None:
|
||||
payment_fee = 0
|
||||
if 'payment' in self.request.session:
|
||||
responses = register_payment_providers.send(self.request.event)
|
||||
for receiver, response in responses:
|
||||
provider = response(self.request.event)
|
||||
if provider.identifier == self.request.session['payment']:
|
||||
payment_fee = provider.calculate_fee(total)
|
||||
payment_fee = payment_fee or self.get_payment_fee(total)
|
||||
|
||||
try:
|
||||
minutes_left = max(min(p.expires for p in positions) - now(), timedelta()).seconds // 60 if positions else 0
|
||||
except AttributeError:
|
||||
minutes_left = None
|
||||
|
||||
return {
|
||||
'positions': positions,
|
||||
'raw': cartpos,
|
||||
@@ -123,6 +107,16 @@ class CartDisplayMixin:
|
||||
'minutes_left': minutes_left,
|
||||
}
|
||||
|
||||
def get_payment_fee(self, total):
|
||||
payment_fee = 0
|
||||
if 'payment' in self.request.session:
|
||||
responses = register_payment_providers.send(self.request.event)
|
||||
for receiver, response in responses:
|
||||
provider = response(self.request.event)
|
||||
if provider.identifier == self.request.session['payment']:
|
||||
payment_fee = provider.calculate_fee(total)
|
||||
return payment_fee
|
||||
|
||||
|
||||
class EventViewMixin:
|
||||
|
||||
|
||||
@@ -59,14 +59,6 @@ class CartActionMixin:
|
||||
return []
|
||||
return items
|
||||
|
||||
def _re_add_position(self, items, position):
|
||||
for i, tup in enumerate(items):
|
||||
if tup[0] == position.item_id and tup[1] == position.variation_id:
|
||||
items[i] = (tup[0], tup[1], tup[2] + 1)
|
||||
return items
|
||||
items.append((position.item_id, position.variation_id, 1))
|
||||
return items
|
||||
|
||||
|
||||
class CartRemove(EventViewMixin, CartActionMixin, EventLoginRequiredMixin, View):
|
||||
|
||||
@@ -107,27 +99,43 @@ class CartAdd(EventViewMixin, CartActionMixin, View):
|
||||
self.msg_some_unavailable = False
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
items = self._items_from_post_data()
|
||||
self.items = self._items_from_post_data()
|
||||
|
||||
# We do not use EventLoginRequiredMixin here, as we want to store stuff into the
|
||||
# session beforehand
|
||||
if not request.user.is_authenticated() or \
|
||||
(request.user.event is not None and request.user.event != request.event):
|
||||
request.session['cart_tmp'] = json.dumps(items)
|
||||
request.session['cart_tmp'] = json.dumps(self.items)
|
||||
return redirect_to_login(
|
||||
self.get_success_url(), reverse('presale:event.checkout.login', kwargs={
|
||||
'organizer': request.event.organizer.slug,
|
||||
'event': request.event.slug,
|
||||
}), 'next'
|
||||
)
|
||||
return self.process(items)
|
||||
return self.process()
|
||||
|
||||
def error_message(self, msg, important=False):
|
||||
if not self.msg_some_unavailable or important:
|
||||
self.msg_some_unavailable = True
|
||||
messages.error(self.request, msg)
|
||||
|
||||
def process(self, items):
|
||||
def _re_add_position(self, position):
|
||||
for i, tup in enumerate(self.items):
|
||||
if tup[0] == position.item_id and tup[1] == position.variation_id:
|
||||
self.items[i] = (tup[0], tup[1], tup[2] + 1)
|
||||
return self.items
|
||||
self.items.append((position.item_id, position.variation_id, 1))
|
||||
|
||||
def _expired_positions(self):
|
||||
# For items that are already expired, we have to delete and re-add them, as they might
|
||||
# be no longer available or prices might have changed. Sorry!
|
||||
for cp in CartPosition.objects.current.filter(
|
||||
Q(user=self.request.user) & Q(event=self.request.event) & Q(expires__lte=now())
|
||||
):
|
||||
self._re_add_position(cp)
|
||||
cp.delete()
|
||||
|
||||
def process(self):
|
||||
# Extend this user's cart session to 30 minutes from now to ensure all items in the
|
||||
# cart expire at the same time
|
||||
# We can extend the reservation of items which are not yet expired without risk
|
||||
@@ -135,18 +143,13 @@ class CartAdd(EventViewMixin, CartActionMixin, View):
|
||||
Q(user=self.request.user) & Q(event=self.request.event) & Q(expires__gt=now())
|
||||
).update(expires=now() + timedelta(minutes=30))
|
||||
|
||||
# For items that are already expired, we have to delete and re-add them, as they might
|
||||
# be no longer available or prices might have changed. Sorry!
|
||||
for cp in CartPosition.objects.current.filter(
|
||||
Q(user=self.request.user) & Q(event=self.request.event) & Q(expires__lte=now())):
|
||||
items = self._re_add_position(items, cp)
|
||||
cp.delete()
|
||||
self._expired_positions()
|
||||
|
||||
if not items:
|
||||
if not self.items:
|
||||
return redirect(self.get_failure_url())
|
||||
|
||||
existing = CartPosition.objects.current.filter(user=self.request.user, event=self.request.event).count()
|
||||
if sum(i[2] for i in items) + existing > int(self.request.event.settings.max_items_per_order):
|
||||
if sum(i[2] for i in self.items) + existing > int(self.request.event.settings.max_items_per_order):
|
||||
# TODO: i18n plurals
|
||||
self.error_message(self.error_messages['max_items'] % self.request.event.settings.max_items_per_order)
|
||||
return redirect(self.get_failure_url())
|
||||
@@ -156,19 +159,19 @@ class CartAdd(EventViewMixin, CartActionMixin, View):
|
||||
i.identity: i for i
|
||||
in Item.objects.current.filter(
|
||||
event=self.request.event,
|
||||
identity__in=[i[0] for i in items]
|
||||
identity__in=[i[0] for i in self.items]
|
||||
).prefetch_related("quotas")
|
||||
}
|
||||
variations_cache = {
|
||||
v.identity: v for v
|
||||
in ItemVariation.objects.current.filter(
|
||||
item__event=self.request.event,
|
||||
identity__in=[i[1] for i in items if i[1] is not None]
|
||||
identity__in=[i[1] for i in self.items if i[1] is not None]
|
||||
).select_related("item", "item__event").prefetch_related("quotas", "values", "values__prop")
|
||||
}
|
||||
|
||||
# Process the request itself
|
||||
for i in items:
|
||||
for i in self.items:
|
||||
# Check whether the specified items are part of what we just fetched from the database
|
||||
# If they are not, the user supplied item IDs which either do not exist or belong to
|
||||
# a different event
|
||||
@@ -183,13 +186,11 @@ class CartAdd(EventViewMixin, CartActionMixin, View):
|
||||
# (b) make the item/variation unavailable. If neither is the case, check_restriction
|
||||
# will correctly return the default price
|
||||
price = item.check_restrictions() if variation is None else variation.check_restrictions()
|
||||
if price is False:
|
||||
self.error_message(self.error_messages['unavailable'])
|
||||
continue
|
||||
|
||||
# Fetch all quotas. If there are no quotas, this item is not allowed to be sold.
|
||||
quotas = list(item.quotas.all()) if variation is None else list(variation.quotas.all())
|
||||
if len(quotas) == 0:
|
||||
|
||||
if price is False or len(quotas) == 0:
|
||||
self.error_message(self.error_messages['unavailable'])
|
||||
continue
|
||||
|
||||
@@ -201,15 +202,14 @@ class CartAdd(EventViewMixin, CartActionMixin, View):
|
||||
# quota while we're doing so.
|
||||
quota.lock()
|
||||
avail = quota.availability()
|
||||
if avail[0] != Quota.AVAILABILITY_OK:
|
||||
# This quota is sold out/currently unavailable, so do not sell this at all
|
||||
self.error_message(self.error_messages['unavailable'])
|
||||
quota_ok = 0
|
||||
break
|
||||
elif avail[1] < i[2]:
|
||||
# This quota is available, but with less than i[2] items left, so we have to
|
||||
if avail[1] < i[2]:
|
||||
# This quota is not available or less than i[2] items are left, so we have to
|
||||
# reduce the number of bought items
|
||||
self.error_message(self.error_messages['in_part'])
|
||||
self.error_message(
|
||||
self.error_messages['unavailable']
|
||||
if avail[0] != Quota.AVAILABILITY_OK
|
||||
else self.error_messages['in_part']
|
||||
)
|
||||
quota_ok = min(quota_ok, avail[1])
|
||||
|
||||
# Create a CartPosition for as much items as we can
|
||||
|
||||
@@ -284,10 +284,7 @@ class OrderConfirm(EventViewMixin, CartDisplayMixin, EventLoginRequiredMixin, Ch
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.request = request
|
||||
check = self.check_process(request)
|
||||
if check:
|
||||
return check
|
||||
return super().get(request, *args, **kwargs)
|
||||
return self.check_process(request) or super().get(request, *args, **kwargs)
|
||||
|
||||
def error_message(self, msg, important=False):
|
||||
if not self.msg_some_unavailable or important:
|
||||
@@ -296,10 +293,9 @@ class OrderConfirm(EventViewMixin, CartDisplayMixin, EventLoginRequiredMixin, Ch
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.request = request
|
||||
check = self.check_process(request)
|
||||
if check:
|
||||
return
|
||||
return self.check_process(request) or self.perform_order(request)
|
||||
|
||||
def perform_order(self, request):
|
||||
dt = now()
|
||||
quotas_locked = set()
|
||||
|
||||
@@ -309,10 +305,7 @@ class OrderConfirm(EventViewMixin, CartDisplayMixin, EventLoginRequiredMixin, Ch
|
||||
quotas = list(cp.item.quotas.all()) if cp.variation is None else list(cp.variation.quotas.all())
|
||||
if cp.expires < dt:
|
||||
price = cp.item.check_restrictions() if cp.variation is None else cp.variation.check_restrictions()
|
||||
if price is False:
|
||||
self.error_message(self.error_messages['unavailable'])
|
||||
continue
|
||||
if len(quotas) == 0:
|
||||
if price is False or len(quotas) == 0:
|
||||
self.error_message(self.error_messages['unavailable'])
|
||||
continue
|
||||
if price != cp.price:
|
||||
@@ -341,40 +334,10 @@ class OrderConfirm(EventViewMixin, CartDisplayMixin, EventLoginRequiredMixin, Ch
|
||||
cp.expires = now() + timedelta(minutes=self.request.event.settings.get('reservation_time', as_type=int))
|
||||
cp.save()
|
||||
if not self.msg_some_unavailable: # Everything went well
|
||||
with transaction.atomic():
|
||||
total = sum([c.price for c in cartpos])
|
||||
payment_fee = self.payment_provider.calculate_fee(total)
|
||||
total += payment_fee
|
||||
expires = [dt + timedelta(days=request.event.payment_term_days)]
|
||||
if request.event.payment_term_last:
|
||||
expires.append(request.event.payment_term_last)
|
||||
order = Order.objects.create(
|
||||
status=Order.STATUS_PENDING,
|
||||
event=request.event,
|
||||
user=request.user,
|
||||
datetime=dt,
|
||||
expires=min(expires),
|
||||
total=total,
|
||||
payment_fee=payment_fee,
|
||||
payment_provider=self.payment_provider.identifier,
|
||||
)
|
||||
for cp in cartpos:
|
||||
op = OrderPosition.objects.create(
|
||||
order=order, item=cp.item, variation=cp.variation,
|
||||
price=cp.price, attendee_name=cp.attendee_name
|
||||
)
|
||||
for answ in cp.answers.all():
|
||||
answ = answ.clone()
|
||||
answ.orderposition = op
|
||||
answ.cartposition = None
|
||||
answ.save()
|
||||
cp.delete()
|
||||
messages.success(request, _('Your order has been placed.'))
|
||||
order = self._place_order(cartpos, dt)
|
||||
messages.success(request, _('Your order has been placed.'))
|
||||
resp = self.payment_provider.checkout_perform(request, order)
|
||||
if isinstance(resp, str):
|
||||
return redirect(str)
|
||||
else:
|
||||
return redirect(self.get_order_url(order))
|
||||
return redirect(resp or self.get_order_url(order))
|
||||
|
||||
except Quota.LockTimeoutException:
|
||||
# Is raised when there are too many threads asking for quota locks and we were
|
||||
@@ -386,3 +349,24 @@ class OrderConfirm(EventViewMixin, CartDisplayMixin, EventLoginRequiredMixin, Ch
|
||||
quota.release()
|
||||
|
||||
return redirect(self.get_confirm_url())
|
||||
|
||||
@transaction.atomic()
|
||||
def _place_order(self, cartpos, dt):
|
||||
total = sum([c.price for c in cartpos])
|
||||
payment_fee = self.payment_provider.calculate_fee(total)
|
||||
total += payment_fee
|
||||
expires = [dt + timedelta(days=self.request.event.payment_term_days)]
|
||||
if self.request.event.payment_term_last:
|
||||
expires.append(self.request.event.payment_term_last)
|
||||
order = Order.objects.create(
|
||||
status=Order.STATUS_PENDING,
|
||||
event=self.request.event,
|
||||
user=self.request.user,
|
||||
datetime=dt,
|
||||
expires=min(expires),
|
||||
total=total,
|
||||
payment_fee=payment_fee,
|
||||
payment_provider=self.payment_provider.identifier,
|
||||
)
|
||||
OrderPosition.transform_cart_positions(cartpos, order)
|
||||
return order
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
ignore = N802,W503
|
||||
max-line-length = 160
|
||||
exclude = migrations,.ropeproject,static
|
||||
max-complexity = 16
|
||||
max-complexity = 12
|
||||
|
||||
Reference in New Issue
Block a user