Import/startup performance improvements

This commit is contained in:
Raphael Michel
2020-06-30 11:34:34 +02:00
parent 5f50aa95eb
commit cf2af3c94d
8 changed files with 50 additions and 20 deletions

View File

@@ -629,9 +629,13 @@ class EventSettingsSerializer(serializers.Serializer):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
for fname in self.default_fields: for fname in self.default_fields:
kwargs = DEFAULTS[fname].get('serializer_kwargs', {}) kwargs = DEFAULTS[fname].get('serializer_kwargs', {})
if callable(kwargs):
kwargs = kwargs()
kwargs.setdefault('required', False) kwargs.setdefault('required', False)
kwargs.setdefault('allow_null', True) kwargs.setdefault('allow_null', True)
form_kwargs = DEFAULTS[fname].get('form_kwargs', {}) form_kwargs = DEFAULTS[fname].get('form_kwargs', {})
if callable(form_kwargs):
form_kwargs = form_kwargs()
if 'serializer_class' not in DEFAULTS[fname]: if 'serializer_class' not in DEFAULTS[fname]:
raise ValidationError('{} has no serializer class'.format(fname)) raise ValidationError('{} has no serializer class'.format(fname))
f = DEFAULTS[fname]['serializer_class']( f = DEFAULTS[fname]['serializer_class'](

View File

@@ -65,6 +65,8 @@ class SettingsForm(i18nfield.forms.I18nFormMixin, HierarkeyForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
for fname in self.auto_fields: for fname in self.auto_fields:
kwargs = DEFAULTS[fname].get('form_kwargs', {}) kwargs = DEFAULTS[fname].get('form_kwargs', {})
if callable(kwargs):
kwargs = kwargs()
kwargs.setdefault('required', False) kwargs.setdefault('required', False)
field = DEFAULTS[fname]['form_class']( field = DEFAULTS[fname]['form_class'](
**kwargs **kwargs

View File

@@ -9,10 +9,10 @@ from django.utils import timezone
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.translation import pgettext from django.utils.translation import pgettext
from django_countries.fields import CountryField
from django_scopes import ScopedManager from django_scopes import ScopedManager
from pretix.base.settings import COUNTRIES_WITH_STATE_IN_ADDRESS from pretix.base.settings import COUNTRIES_WITH_STATE_IN_ADDRESS
from pretix.helpers.countries import FastCountryField
def invoice_filename(instance, filename: str) -> str: def invoice_filename(instance, filename: str) -> str:
@@ -84,7 +84,7 @@ class Invoice(models.Model):
invoice_from_name = models.CharField(max_length=190, null=True) invoice_from_name = models.CharField(max_length=190, null=True)
invoice_from_zipcode = models.CharField(max_length=190, null=True) invoice_from_zipcode = models.CharField(max_length=190, null=True)
invoice_from_city = models.CharField(max_length=190, null=True) invoice_from_city = models.CharField(max_length=190, null=True)
invoice_from_country = CountryField(null=True) invoice_from_country = FastCountryField(null=True)
invoice_from_tax_id = models.CharField(max_length=190, null=True) invoice_from_tax_id = models.CharField(max_length=190, null=True)
invoice_from_vat_id = models.CharField(max_length=190, null=True) invoice_from_vat_id = models.CharField(max_length=190, null=True)
invoice_to = models.TextField() invoice_to = models.TextField()
@@ -94,7 +94,7 @@ class Invoice(models.Model):
invoice_to_zipcode = models.CharField(max_length=190, null=True) invoice_to_zipcode = models.CharField(max_length=190, null=True)
invoice_to_city = models.TextField(null=True) invoice_to_city = models.TextField(null=True)
invoice_to_state = models.CharField(max_length=190, null=True) invoice_to_state = models.CharField(max_length=190, null=True)
invoice_to_country = CountryField(null=True) invoice_to_country = FastCountryField(null=True)
invoice_to_vat_id = models.TextField(null=True) invoice_to_vat_id = models.TextField(null=True)
invoice_to_beneficiary = models.TextField(null=True) invoice_to_beneficiary = models.TextField(null=True)
date = models.DateField(default=today) date = models.DateField(default=today)

View File

@@ -27,7 +27,7 @@ from django.utils.formats import date_format
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.timezone import make_aware, now from django.utils.timezone import make_aware, now
from django.utils.translation import gettext_lazy as _, pgettext_lazy from django.utils.translation import gettext_lazy as _, pgettext_lazy
from django_countries.fields import Country, CountryField from django_countries.fields import Country
from django_scopes import ScopedManager, scopes_disabled from django_scopes import ScopedManager, scopes_disabled
from i18nfield.strings import LazyI18nString from i18nfield.strings import LazyI18nString
from jsonfallback.fields import FallbackJSONField from jsonfallback.fields import FallbackJSONField
@@ -44,7 +44,7 @@ from pretix.base.services.locking import NoLockManager
from pretix.base.settings import PERSON_NAME_SCHEMES from pretix.base.settings import PERSON_NAME_SCHEMES
from pretix.base.signals import order_gracefully_delete from pretix.base.signals import order_gracefully_delete
from ...helpers.countries import CachedCountries from ...helpers.countries import CachedCountries, FastCountryField
from .base import LockModel, LoggedModel from .base import LockModel, LoggedModel
from .event import Event, SubEvent from .event import Event, SubEvent
from .items import Item, ItemVariation, Question, QuestionOption, Quota from .items import Item, ItemVariation, Question, QuestionOption, Quota
@@ -1076,7 +1076,7 @@ class AbstractPosition(models.Model):
street = models.TextField(verbose_name=_('Address'), blank=True, null=True) street = models.TextField(verbose_name=_('Address'), blank=True, null=True)
zipcode = models.CharField(max_length=30, verbose_name=_('ZIP code'), blank=True, null=True) zipcode = models.CharField(max_length=30, verbose_name=_('ZIP code'), blank=True, null=True)
city = models.CharField(max_length=255, verbose_name=_('City'), blank=True, null=True) city = models.CharField(max_length=255, verbose_name=_('City'), blank=True, null=True)
country = CountryField(verbose_name=_('Country'), blank=True, blank_label=_('Select country'), null=True) country = FastCountryField(verbose_name=_('Country'), blank=True, blank_label=_('Select country'), null=True)
state = models.CharField(max_length=255, verbose_name=pgettext_lazy('address', 'State'), blank=True, null=True) state = models.CharField(max_length=255, verbose_name=pgettext_lazy('address', 'State'), blank=True, null=True)
class Meta: class Meta:
@@ -2150,8 +2150,8 @@ class InvoiceAddress(models.Model):
zipcode = models.CharField(max_length=30, verbose_name=_('ZIP code'), blank=False) zipcode = models.CharField(max_length=30, verbose_name=_('ZIP code'), blank=False)
city = models.CharField(max_length=255, verbose_name=_('City'), blank=False) city = models.CharField(max_length=255, verbose_name=_('City'), blank=False)
country_old = models.CharField(max_length=255, verbose_name=_('Country'), blank=False) country_old = models.CharField(max_length=255, verbose_name=_('Country'), blank=False)
country = CountryField(verbose_name=_('Country'), blank=False, blank_label=_('Select country'), country = FastCountryField(verbose_name=_('Country'), blank=False, blank_label=_('Select country'),
countries=CachedCountries) countries=CachedCountries)
state = models.CharField(max_length=255, verbose_name=pgettext_lazy('address', 'State'), blank=True) state = models.CharField(max_length=255, verbose_name=pgettext_lazy('address', 'State'), blank=True)
vat_id = models.CharField(max_length=255, blank=True, verbose_name=_('VAT ID'), vat_id = models.CharField(max_length=255, blank=True, verbose_name=_('VAT ID'),
help_text=_('Only for business customers within the EU.')) help_text=_('Only for business customers within the EU.'))

View File

@@ -5,12 +5,12 @@ from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.utils.formats import localize from django.utils.formats import localize
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from i18nfield.fields import I18nCharField from i18nfield.fields import I18nCharField
from pretix.base.decimal import round_decimal from pretix.base.decimal import round_decimal
from pretix.base.models.base import LoggedModel from pretix.base.models.base import LoggedModel
from pretix.base.templatetags.money import money_filter from pretix.base.templatetags.money import money_filter
from pretix.helpers.countries import FastCountryField
class TaxedPrice: class TaxedPrice:
@@ -116,7 +116,7 @@ class TaxRule(LoggedModel):
"ID. Only enable this option after consulting a tax counsel. No warranty given for correct tax " "ID. Only enable this option after consulting a tax counsel. No warranty given for correct tax "
"calculation. USE AT YOUR OWN RISK.") "calculation. USE AT YOUR OWN RISK.")
) )
home_country = CountryField( home_country = FastCountryField(
verbose_name=_('Merchant country'), verbose_name=_('Merchant country'),
blank=True, blank=True,
help_text=_('Your country of residence. This is the country the EU reverse charge rule will not apply in, ' help_text=_('Your country of residence. This is the country the EU reverse charge rule will not apply in, '

View File

@@ -13,7 +13,6 @@ from django.db.models import Model
from django.utils.translation import ( from django.utils.translation import (
gettext_lazy as _, gettext_noop, pgettext, pgettext_lazy, gettext_lazy as _, gettext_noop, pgettext, pgettext_lazy,
) )
from django_countries import countries
from hierarkey.models import GlobalSettingsBase, Hierarkey from hierarkey.models import GlobalSettingsBase, Hierarkey
from i18nfield.forms import I18nFormField, I18nTextarea, I18nTextInput from i18nfield.forms import I18nFormField, I18nTextarea, I18nTextInput
from i18nfield.strings import LazyI18nString from i18nfield.strings import LazyI18nString
@@ -27,9 +26,15 @@ from pretix.base.reldate import (
SerializerRelativeDateField, SerializerRelativeDateTimeField, SerializerRelativeDateField, SerializerRelativeDateTimeField,
) )
from pretix.control.forms import MultipleLanguagesWidget, SingleLanguageWidget from pretix.control.forms import MultipleLanguagesWidget, SingleLanguageWidget
from pretix.helpers.countries import CachedCountries
allcountries = list(countries)
allcountries.insert(0, ('', _('Select country'))) def country_choice_kwargs():
allcountries = list(CachedCountries())
allcountries.insert(0, ('', _('Select country')))
return {
'choices': allcountries
}
DEFAULTS = { DEFAULTS = {
@@ -570,13 +575,8 @@ DEFAULTS = {
'type': str, 'type': str,
'form_class': forms.ChoiceField, 'form_class': forms.ChoiceField,
'serializer_class': serializers.ChoiceField, 'serializer_class': serializers.ChoiceField,
'serializer_kwargs': dict( 'serializer_kwargs': country_choice_kwargs,
choices=allcountries, 'form_kwargs': country_choice_kwargs,
),
'form_kwargs': dict(
choices=allcountries,
label=_("Country"),
)
}, },
'invoice_address_from_tax_id': { 'invoice_address_from_tax_id': {
'default': '', 'default': '',

View File

@@ -1,6 +1,7 @@
from django.core.cache import cache from django.core.cache import cache
from django.utils.translation import get_language from django.utils.translation import get_language
from django_countries import Countries from django_countries import Countries
from django_countries.fields import CountryField
class CachedCountries(Countries): class CachedCountries(Countries):
@@ -27,3 +28,24 @@ class CachedCountries(Countries):
self._cached_lists[cache_key] = val self._cached_lists[cache_key] = val
cache.set(cache_key, val, 3600 * 24 * 30) cache.set(cache_key, val, 3600 * 24 * 30)
yield from val yield from val
class FastCountryField(CountryField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("countries", CachedCountries)
super().__init__(*args, **kwargs)
def check(self, **kwargs):
# Disable _check_choices since it would require sorting all country names at every import of this field,
# which taskes 1-2 seconds
return [
*self._check_field_name(),
# *self._check_choices(),
*self._check_db_index(),
*self._check_null_allowed_for_primary_keys(),
*self._check_backend_specific_checks(**kwargs),
*self._check_validators(),
*self._check_deprecation_details(),
*self._check_multiple(),
*self._check_max_length_attribute(**kwargs),
]

View File

@@ -719,3 +719,5 @@ OAUTH2_PROVIDER = {
'ROTATE_REFRESH_TOKEN': False, 'ROTATE_REFRESH_TOKEN': False,
} }
TEMPLATES[0]['DIRS'].insert(0, '../pretixeu/pretixeu/pretixeu/templates')
TEMPLATES[0]['OPTIONS']['context_processors'].append('pretixeu.billing.context.contextprocessor')