mirror of
https://github.com/pretix/pretix.git
synced 2026-05-09 15:54:03 +00:00
Remove re-implementations if i18nfield API integration
This commit is contained in:
@@ -19,57 +19,8 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
|
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
|
||||||
# <https://www.gnu.org/licenses/>.
|
# <https://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
from django.conf import settings
|
|
||||||
from django.core.validators import URLValidator
|
from django.core.validators import URLValidator
|
||||||
from i18nfield.fields import I18nCharField, I18nTextField
|
from i18nfield.rest_framework import I18nAwareModelSerializer, I18nField
|
||||||
from i18nfield.strings import LazyI18nString
|
|
||||||
from rest_framework.exceptions import ValidationError
|
|
||||||
from rest_framework.fields import Field
|
|
||||||
from rest_framework.serializers import ModelSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class I18nField(Field):
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.allow_blank = kwargs.pop('allow_blank', False)
|
|
||||||
self.trim_whitespace = kwargs.pop('trim_whitespace', True)
|
|
||||||
self.max_length = kwargs.pop('max_length', None)
|
|
||||||
self.min_length = kwargs.pop('min_length', None)
|
|
||||||
super().__init__(**kwargs)
|
|
||||||
|
|
||||||
def to_representation(self, value):
|
|
||||||
if hasattr(value, 'data'):
|
|
||||||
if isinstance(value.data, dict):
|
|
||||||
return value.data
|
|
||||||
elif value.data is None:
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return {
|
|
||||||
settings.LANGUAGE_CODE: str(value.data)
|
|
||||||
}
|
|
||||||
elif value is None:
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return {
|
|
||||||
settings.LANGUAGE_CODE: str(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
|
||||||
if isinstance(data, str):
|
|
||||||
return LazyI18nString(data)
|
|
||||||
elif isinstance(data, dict):
|
|
||||||
if any([k not in dict(settings.LANGUAGES) for k in data.keys()]):
|
|
||||||
raise ValidationError('Invalid languages included.')
|
|
||||||
return LazyI18nString(data)
|
|
||||||
else:
|
|
||||||
raise ValidationError('Invalid data type.')
|
|
||||||
|
|
||||||
|
|
||||||
class I18nAwareModelSerializer(ModelSerializer):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
I18nAwareModelSerializer.serializer_field_mapping[I18nCharField] = I18nField
|
|
||||||
I18nAwareModelSerializer.serializer_field_mapping[I18nTextField] = I18nField
|
|
||||||
|
|
||||||
|
|
||||||
class I18nURLField(I18nField):
|
class I18nURLField(I18nField):
|
||||||
@@ -84,3 +35,10 @@ class I18nURLField(I18nField):
|
|||||||
else:
|
else:
|
||||||
URLValidator()(value.data)
|
URLValidator()(value.data)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"I18nAwareModelSerializer", # for backwards compatibility
|
||||||
|
"I18nField", # for backwards compatibility
|
||||||
|
"I18nURLField",
|
||||||
|
]
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ from django.utils.translation import (
|
|||||||
from django_countries.fields import Country
|
from django_countries.fields import Country
|
||||||
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.rest_framework import I18nField
|
||||||
from i18nfield.strings import LazyI18nString
|
from i18nfield.strings import LazyI18nString
|
||||||
from phonenumbers import PhoneNumber, parse
|
from phonenumbers import PhoneNumber, parse
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
@@ -63,7 +64,7 @@ from rest_framework import serializers
|
|||||||
from pretix.api.serializers.fields import (
|
from pretix.api.serializers.fields import (
|
||||||
ListMultipleChoiceField, UploadedFileField,
|
ListMultipleChoiceField, UploadedFileField,
|
||||||
)
|
)
|
||||||
from pretix.api.serializers.i18n import I18nField, I18nURLField
|
from pretix.api.serializers.i18n import I18nURLField
|
||||||
from pretix.base.forms import I18nMarkdownTextarea, I18nURLFormField
|
from pretix.base.forms import I18nMarkdownTextarea, I18nURLFormField
|
||||||
from pretix.base.models.tax import VAT_ID_COUNTRIES, TaxRule
|
from pretix.base.models.tax import VAT_ID_COUNTRIES, TaxRule
|
||||||
from pretix.base.reldate import (
|
from pretix.base.reldate import (
|
||||||
|
|||||||
Reference in New Issue
Block a user