Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def positions(self):
return CartPosition.objects.filter(
Q(cart_id=self.cart_id) & Q(event=self.event)
).select_related('item', 'subevent')
def _ajax_response_data(self):
cart_id = get_or_create_cart_id(self.request)
return {
'cart_id': cart_id,
'has_cart': CartPosition.objects.filter(cart_id=cart_id, event=self.request.event).exists()
}
def count_in_cart(self, now_dt: datetime=None) -> int:
from pretix.base.models import CartPosition
now_dt = now_dt or now()
return CartPosition.objects.filter(
Q(event=self.event) & Q(subevent=self.subevent) &
Q(expires__gte=now_dt) &
Q(
Q(voucher__isnull=True)
| Q(voucher__block_quota=False)
| Q(voucher__valid_until__lt=now_dt)
) &
self._position_lookup
).count()
if "cart_id" not in request.GET:
cached_data = cache.get(cache_key)
if cached_data:
return self.response(cached_data)
data = {
'currency': request.event.currency,
'display_net_prices': request.event.settings.display_net_prices,
'show_variations_expanded': request.event.settings.show_variations_expanded,
'waiting_list_enabled': request.event.settings.waiting_list_enabled,
'voucher_explanation_text': str(request.event.settings.voucher_explanation_text),
'error': None,
'cart_exists': False
}
if 'cart_id' in request.GET and CartPosition.objects.filter(event=request.event, cart_id=request.GET.get('cart_id')).exists():
data['cart_exists'] = True
ev = self.subevent or request.event
data['name'] = str(ev.name)
fail = False
if not ev.presale_is_running:
if ev.presale_has_ended:
if request.event.settings.presale_has_ended_text:
data['error'] = str(request.event.settings.presale_has_ended_text)
else:
data['error'] = ugettext('The presale period for this event is over.')
elif request.event.settings.presale_start_show_date:
data['error'] = ugettext('The presale for this event will start on %(date)s at %(time)s.') % {
'date': date_format(ev.presale_start.astimezone(request.event.timezone), "SHORT_DATE_FORMAT"),
'time': date_format(ev.presale_start.astimezone(request.event.timezone), "TIME_FORMAT"),
def get_cart(request):
from pretix.presale.views.cart import get_or_create_cart_id
if not hasattr(request, '_cart_cache'):
cart_id = get_or_create_cart_id(request, create=False)
if not cart_id:
request._cart_cache = CartPosition.objects.none()
else:
request._cart_cache = CartPosition.objects.filter(
cart_id=cart_id, event=request.event
).order_by(
'item', 'variation'
).select_related(
'item', 'variation', 'subevent', 'subevent__event', 'subevent__event__organizer',
'item__tax_rule'
)
for cp in request._cart_cache:
cp.event = request.event # Populate field with known value to save queries
return request._cart_cache
This method migrates legacy sessions created before the upgrade to 1.8.0 on a best-effort basis,
meaning that the migration does not respect plugin-specific data and works best if the user only
used the session for one event at the time of migration.
If ``create`` is ``False`` and no session currently exists, ``None`` will be returned.
"""
session_keyname = 'current_cart_event_{}'.format(request.event.pk)
prefix = ''
if request.resolver_match and request.resolver_match.kwargs.get('cart_namespace'):
session_keyname += '_' + request.resolver_match.kwargs.get('cart_namespace')
prefix = request.resolver_match.kwargs.get('cart_namespace')
current_id = orig_current_id = request.session.get(session_keyname)
if prefix and 'take_cart_id' in request.GET:
pos = CartPosition.objects.filter(event=request.event, cart_id=request.GET.get('take_cart_id'))
if request.method == "POST" or pos.exists():
current_id = request.GET.get('take_cart_id')
if current_id and current_id in request.session.get('carts', {}):
if current_id != orig_current_id:
request.session[session_keyname] = current_id
return current_id
else:
cart_data = {}
if prefix and 'take_cart_id' in request.GET and current_id:
new_id = current_id
cached_widget_data = widget_data_cache.get('widget_data_{}'.format(current_id))
if cached_widget_data:
cart_data['widget_data'] = cached_widget_data
else:
if not create:
def _get_voucher_availability(self):
vouchers_ok = {}
self._voucher_depend_on_cart = set()
for voucher, count in self._voucher_use_diff.items():
voucher.refresh_from_db()
if voucher.valid_until is not None and voucher.valid_until < self.now_dt:
raise CartError(error_messages['voucher_expired'])
redeemed_in_carts = CartPosition.objects.filter(
Q(voucher=voucher) & Q(event=self.event) &
Q(expires__gte=self.now_dt)
).exclude(pk__in=[
op.position.id for op in self._operations if isinstance(op, self.ExtendOperation)
])
cart_count = redeemed_in_carts.count()
v_avail = voucher.max_usages - voucher.redeemed - cart_count
if cart_count > 0:
self._voucher_depend_on_cart.add(voucher)
vouchers_ok[voucher] = v_avail
return vouchers_ok
def delete(self, request, *args, **kwargs):
success_url = self.get_success_url()
o = self.get_object()
if o.allow_delete():
CartPosition.objects.filter(addon_to__item=self.get_object()).delete()
self.get_object().cartposition_set.all().delete()
self.get_object().log_action('pretix.event.item.deleted', user=self.request.user)
self.get_object().delete()
messages.success(request, _('The selected product has been deleted.'))
return HttpResponseRedirect(success_url)
else:
o = self.get_object()
o.active = False
o.save()
o.log_action('pretix.event.item.changed', user=self.request.user, data={
'active': False
})
messages.success(request, _('The selected product has been deactivated.'))
return HttpResponseRedirect(success_url)