forked from CGM_Public/pretix_original
Revert 36d6b6f9, pass language to async tasks
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import json
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@@ -48,27 +47,14 @@ def language(lng):
|
|||||||
|
|
||||||
|
|
||||||
class LazyLocaleException(Exception):
|
class LazyLocaleException(Exception):
|
||||||
def __init__(self, msg, msgargs=None):
|
def __init__(self, *args):
|
||||||
self.msg = msg
|
self.msg = args[0]
|
||||||
|
self.msgargs = args[1] if len(args) > 1 else None
|
||||||
if isinstance(msgargs, list) or isinstance(msgargs, tuple) or isinstance(msgargs, dict):
|
self.args = args
|
||||||
msgargs = json.dumps(msgargs, cls=I18nJSONEncoder)
|
super().__init__(self.msg, self.msgargs)
|
||||||
|
|
||||||
self.msgargs = msgargs
|
|
||||||
super().__init__(msg, self.msgargs)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if self.msgargs:
|
if self.msgargs:
|
||||||
data = json.loads(self.msgargs)
|
return ugettext(self.msg) % self.msgargs
|
||||||
if isinstance(data, dict):
|
|
||||||
for k, v in data.items():
|
|
||||||
if isinstance(v, dict):
|
|
||||||
data[k] = LazyI18nString(v)
|
|
||||||
elif isinstance(data, list):
|
|
||||||
for i, v in enumerate(data):
|
|
||||||
if isinstance(v, dict):
|
|
||||||
data[i] = LazyI18nString(v)
|
|
||||||
|
|
||||||
return ugettext(self.msg) % data
|
|
||||||
else:
|
else:
|
||||||
return ugettext(self.msg)
|
return ugettext(self.msg)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ from pretix.celery_app import app
|
|||||||
|
|
||||||
|
|
||||||
class ProfiledTask(app.Task):
|
class ProfiledTask(app.Task):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|
||||||
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
|
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from collections import Counter, namedtuple
|
from collections import Counter, namedtuple
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from celery.exceptions import MaxRetriesExceededError
|
from celery.exceptions import MaxRetriesExceededError
|
||||||
@@ -10,7 +11,7 @@ from django.utils.timezone import now
|
|||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
|
||||||
from pretix.base.decimal import round_decimal
|
from pretix.base.decimal import round_decimal
|
||||||
from pretix.base.i18n import LazyLocaleException
|
from pretix.base.i18n import LazyLocaleException, language
|
||||||
from pretix.base.models import (
|
from pretix.base.models import (
|
||||||
CartPosition, Event, Item, ItemVariation, Voucher,
|
CartPosition, Event, Item, ItemVariation, Voucher,
|
||||||
)
|
)
|
||||||
@@ -116,7 +117,7 @@ class CartManager:
|
|||||||
cartsize -= len([1 for op in self._operations if isinstance(op, self.RemoveOperation)])
|
cartsize -= len([1 for op in self._operations if isinstance(op, self.RemoveOperation)])
|
||||||
if cartsize > int(self.event.settings.max_items_per_order):
|
if cartsize > int(self.event.settings.max_items_per_order):
|
||||||
# TODO: i18n plurals
|
# TODO: i18n plurals
|
||||||
raise CartError(error_messages['max_items'], (self.event.settings.max_items_per_order,))
|
raise CartError(_(error_messages['max_items']) % (self.event.settings.max_items_per_order,))
|
||||||
|
|
||||||
def _check_item_constraints(self, op):
|
def _check_item_constraints(self, op):
|
||||||
if isinstance(op, self.AddOperation) or isinstance(op, self.ExtendOperation):
|
if isinstance(op, self.AddOperation) or isinstance(op, self.ExtendOperation):
|
||||||
@@ -144,8 +145,12 @@ class CartManager:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if new_total > op.item.max_per_order:
|
if new_total > op.item.max_per_order:
|
||||||
raise CartError(error_messages['max_items_per_product'], {'max': op.item.max_per_order,
|
raise CartError(
|
||||||
'product': op.item.name})
|
_(error_messages['max_items_per_product']) % {
|
||||||
|
'max': op.item.max_per_order,
|
||||||
|
'product': op.item.name
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def _get_price(self, item: Item, variation: Optional[ItemVariation],
|
def _get_price(self, item: Item, variation: Optional[ItemVariation],
|
||||||
voucher: Optional[Voucher], custom_price: Optional[Decimal]):
|
voucher: Optional[Voucher], custom_price: Optional[Decimal]):
|
||||||
@@ -369,7 +374,7 @@ class CartManager:
|
|||||||
|
|
||||||
|
|
||||||
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(CartError,))
|
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(CartError,))
|
||||||
def add_items_to_cart(self, event: int, items: List[dict], cart_id: str=None) -> None:
|
def add_items_to_cart(self, event: int, items: List[dict], cart_id: str=None, locale='en') -> None:
|
||||||
"""
|
"""
|
||||||
Adds a list of items to a user's cart.
|
Adds a list of items to a user's cart.
|
||||||
:param event: The event ID in question
|
:param event: The event ID in question
|
||||||
@@ -378,6 +383,7 @@ def add_items_to_cart(self, event: int, items: List[dict], cart_id: str=None) ->
|
|||||||
:param coupon: A coupon that should also be reeemed
|
:param coupon: A coupon that should also be reeemed
|
||||||
:raises CartError: On any error that occured
|
:raises CartError: On any error that occured
|
||||||
"""
|
"""
|
||||||
|
with language(locale):
|
||||||
event = Event.objects.get(id=event)
|
event = Event.objects.get(id=event)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
@@ -391,13 +397,14 @@ def add_items_to_cart(self, event: int, items: List[dict], cart_id: str=None) ->
|
|||||||
|
|
||||||
|
|
||||||
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(CartError,))
|
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(CartError,))
|
||||||
def remove_items_from_cart(self, event: int, items: List[dict], cart_id: str=None) -> None:
|
def remove_items_from_cart(self, event: int, items: List[dict], cart_id: str=None, locale='en') -> None:
|
||||||
"""
|
"""
|
||||||
Removes a list of items from a user's cart.
|
Removes a list of items from a user's cart.
|
||||||
:param event: The event ID in question
|
:param event: The event ID in question
|
||||||
:param items: A list of tuple of the form (item id, variation id or None, number)
|
:param items: A list of tuple of the form (item id, variation id or None, number)
|
||||||
:param session: Session ID of a guest
|
:param session: Session ID of a guest
|
||||||
"""
|
"""
|
||||||
|
with language(locale):
|
||||||
event = Event.objects.get(id=event)
|
event = Event.objects.get(id=event)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -390,7 +390,6 @@ def _perform_order(event: str, payment_provider: str, position_ids: List[str],
|
|||||||
if not order.invoices.exists():
|
if not order.invoices.exists():
|
||||||
generate_invoice(order)
|
generate_invoice(order)
|
||||||
|
|
||||||
with language(order.locale):
|
|
||||||
if order.total == Decimal('0.00'):
|
if order.total == Decimal('0.00'):
|
||||||
mailtext = event.settings.mail_text_order_free
|
mailtext = event.settings.mail_text_order_free
|
||||||
else:
|
else:
|
||||||
@@ -671,6 +670,7 @@ class OrderChangeManager:
|
|||||||
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(OrderError,))
|
@app.task(base=ProfiledTask, bind=True, max_retries=5, default_retry_delay=1, throws=(OrderError,))
|
||||||
def perform_order(self, event: str, payment_provider: str, positions: List[str],
|
def perform_order(self, event: str, payment_provider: str, positions: List[str],
|
||||||
email: str=None, locale: str=None, address: int=None, meta_info: dict=None):
|
email: str=None, locale: str=None, address: int=None, meta_info: dict=None):
|
||||||
|
with language(locale):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return _perform_order(event, payment_provider, positions, email, locale, address, meta_info)
|
return _perform_order(event, payment_provider, positions, email, locale, address, meta_info)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from django.contrib import messages
|
|||||||
from django.db.models import Count, Q
|
from django.db.models import Count, Q
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
from django.utils import translation
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.views.generic import TemplateView, View
|
from django.views.generic import TemplateView, View
|
||||||
@@ -116,7 +117,7 @@ class CartRemove(EventViewMixin, CartActionMixin, AsyncAction, View):
|
|||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
items = self._items_from_post_data()
|
items = self._items_from_post_data()
|
||||||
if items:
|
if items:
|
||||||
return self.do(self.request.event.id, items, self.request.session.session_key)
|
return self.do(self.request.event.id, items, self.request.session.session_key, translation.get_language())
|
||||||
else:
|
else:
|
||||||
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
|
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
@@ -136,7 +137,7 @@ class CartAdd(EventViewMixin, CartActionMixin, AsyncAction, View):
|
|||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
items = self._items_from_post_data()
|
items = self._items_from_post_data()
|
||||||
if items:
|
if items:
|
||||||
return self.do(self.request.event.id, items, self.request.session.session_key)
|
return self.do(self.request.event.id, items, self.request.session.session_key, translation.get_language())
|
||||||
else:
|
else:
|
||||||
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
|
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
|
|||||||
Reference in New Issue
Block a user