Fix #1378 -- API: Allow to access and modify (some) event setti… (#1569)

* API: Allow to access event settings

* Convert most "general" settings

* Smaller fixes

* Add more settings

* Relative dates, nulling

* Fix a test failure

* Fix wrong attribute access
This commit is contained in:
Raphael Michel
2020-02-04 17:06:23 +01:00
committed by GitHub
parent 87b10ef055
commit fd1c964c92
16 changed files with 1357 additions and 585 deletions

View File

@@ -1,5 +1,4 @@
from django.apps import AppConfig
from django.conf import settings
class PretixBaseConfig(AppConfig):
@@ -14,6 +13,7 @@ class PretixBaseConfig(AppConfig):
from . import notifications # NOQA
from . import email # NOQA
from .services import auth, checkin, export, mail, tickets, cart, orderimport, orders, invoices, cleanup, update_check, quotas, notifications, vouchers # NOQA
from django.conf import settings
try:
from .celery_app import app as celery_app # NOQA

View File

@@ -8,7 +8,6 @@ from django.utils.crypto import get_random_string
from formtools.wizard.views import SessionWizardView
from hierarkey.forms import HierarkeyForm
from pretix.base.models import Event
from pretix.base.reldate import RelativeDateField, RelativeDateTimeField
from .validators import PlaceholderValidator # NOQA
@@ -51,19 +50,33 @@ class I18nInlineFormSet(i18nfield.forms.I18nInlineFormSet):
class SettingsForm(i18nfield.forms.I18nFormMixin, HierarkeyForm):
auto_fields = []
def __init__(self, *args, **kwargs):
from pretix.base.settings import DEFAULTS
self.obj = kwargs.get('obj', None)
self.locales = self.obj.settings.get('locales') if self.obj else kwargs.pop('locales', None)
kwargs['attribute_name'] = 'settings'
kwargs['locales'] = self.locales
kwargs['initial'] = self.obj.settings.freeze()
super().__init__(*args, **kwargs)
for fname in self.auto_fields:
kwargs = DEFAULTS[fname].get('form_kwargs', {})
kwargs.setdefault('required', False)
field = DEFAULTS[fname]['form_class'](
**kwargs
)
if isinstance(field, i18nfield.forms.I18nFormField):
field.widget.enabled_locales = self.locales
self.fields[fname] = field
for k, f in self.fields.items():
if isinstance(f, (RelativeDateTimeField, RelativeDateField)):
f.set_event(self.obj)
def get_new_filename(self, name: str) -> str:
from pretix.base.models import Event
nonce = get_random_string(length=8)
if isinstance(self.obj, Event):
fname = '%s/%s/%s.%s.%s' % (

View File

@@ -6,9 +6,6 @@ from django.utils.functional import lazy
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from pretix.base.models import OrderPosition
from pretix.multidomain.urlreverse import eventreverse
class DatePickerWidget(forms.DateInput):
def __init__(self, attrs=None, date_format=None):
@@ -71,6 +68,9 @@ class UploadedFileWidget(forms.ClearableFileInput):
@property
def url(self):
from pretix.base.models import OrderPosition
from pretix.multidomain.urlreverse import eventreverse
if isinstance(self.position, OrderPosition):
return eventreverse(self.event, 'presale:event.order.download.answer', kwargs={
'order': self.position.order.code,

View File

@@ -8,6 +8,7 @@ from django import forms
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
BASE_CHOICES = (
('date_from', _('Event start')),
@@ -115,6 +116,8 @@ class RelativeDateWrapper:
base_date_name=parts[3],
time=time
)
if data.base_date_name not in [k[0] for k in BASE_CHOICES]:
raise ValueError('{} is not a valid base date'.format(data.base_date_name))
else:
data = parser.parse(input)
return RelativeDateWrapper(data)
@@ -330,3 +333,39 @@ class ModelRelativeDateTimeField(models.CharField):
defaults = {'form_class': self.form_class}
defaults.update(kwargs)
return super().formfield(**defaults)
class SerializerRelativeDateField(serializers.CharField):
def to_internal_value(self, data):
if data is None:
return None
try:
r = RelativeDateWrapper.from_string(data)
if isinstance(r.data, RelativeDate):
if r.data.time is not None:
raise ValidationError("Do not specify a time for a date field")
return r
except:
raise ValidationError("Invalid relative date")
def to_representation(self, value: RelativeDateWrapper):
if value is None:
return None
return value.to_string()
class SerializerRelativeDateTimeField(serializers.CharField):
def to_internal_value(self, data):
if data is None:
return None
try:
return RelativeDateWrapper.from_string(data)
except:
raise ValidationError("Invalid relative date")
def to_representation(self, value: RelativeDateWrapper):
if value is None:
return None
return value.to_string()

File diff suppressed because it is too large Load Diff

View File

@@ -641,3 +641,27 @@ to define additional columns that can be read during import. You are expected to
As with all event-plugin signals, the ``sender`` keyword argument will contain the event.
"""
validate_event_settings = EventPluginSignal(
providing_args=["settings_dict"]
)
"""
This signal is sent out if the user performs an update of event settings through the API or web interface.
You are passed a ``settings_dict`` dictionary with the new state of the event settings object and are expected
to raise a ``django.core.exceptions.ValidationError`` if the new state is not valid.
You can not modify the dictionary. This is only recommended to use if you have multiple settings
that can only be validated together. To validate individual settings, pass a validator to the
serializer field instead.
As with all event-plugin signals, the ``sender`` keyword argument will contain the event.
"""
api_event_settings_fields = EventPluginSignal(
providing_args=[]
)
"""
This signal is sent out to collect serializable settings fields for the API. You are expected to
return a dictionary mapping names of attributes in the settings store to DRF serializer field instances.
As with all event-plugin signals, the ``sender`` keyword argument will contain the event.
"""