mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-23 17:18:44 +02:00
locale: add lazy translations to missing ValidationErrors
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from drf_spectacular.types import OpenApiTypes
|
from drf_spectacular.types import OpenApiTypes
|
||||||
from drf_spectacular.utils import extend_schema_field
|
from drf_spectacular.utils import extend_schema_field
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from apps.transactions.models import (
|
from apps.transactions.models import (
|
||||||
TransactionCategory,
|
TransactionCategory,
|
||||||
@@ -25,13 +26,13 @@ class TransactionCategoryField(serializers.Field):
|
|||||||
return TransactionCategory.objects.get(pk=data)
|
return TransactionCategory.objects.get(pk=data)
|
||||||
except TransactionCategory.DoesNotExist:
|
except TransactionCategory.DoesNotExist:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
"Category with this ID does not exist."
|
_("Category with this ID does not exist.")
|
||||||
)
|
)
|
||||||
elif isinstance(data, str):
|
elif isinstance(data, str):
|
||||||
category, created = TransactionCategory.objects.get_or_create(name=data)
|
category, created = TransactionCategory.objects.get_or_create(name=data)
|
||||||
return category
|
return category
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
"Invalid category data. Provide an ID or name."
|
_("Invalid category data. Provide an ID or name.")
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -61,13 +62,13 @@ class TransactionTagField(serializers.Field):
|
|||||||
tag = TransactionTag.objects.get(pk=item)
|
tag = TransactionTag.objects.get(pk=item)
|
||||||
except TransactionTag.DoesNotExist:
|
except TransactionTag.DoesNotExist:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
f"Tag with ID {item} does not exist."
|
_("Tag with this ID does not exist.")
|
||||||
)
|
)
|
||||||
elif isinstance(item, str):
|
elif isinstance(item, str):
|
||||||
tag, created = TransactionTag.objects.get_or_create(name=item)
|
tag, created = TransactionTag.objects.get_or_create(name=item)
|
||||||
else:
|
else:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
"Invalid tag data. Provide an ID or name."
|
_("Invalid tag data. Provide an ID or name.")
|
||||||
)
|
)
|
||||||
tags.append(tag)
|
tags.append(tag)
|
||||||
return tags
|
return tags
|
||||||
@@ -85,13 +86,13 @@ class TransactionEntityField(serializers.Field):
|
|||||||
entity = TransactionEntity.objects.get(pk=item)
|
entity = TransactionEntity.objects.get(pk=item)
|
||||||
except TransactionTag.DoesNotExist:
|
except TransactionTag.DoesNotExist:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
f"Entity with ID {item} does not exist."
|
_("Entity with this ID does not exist.")
|
||||||
)
|
)
|
||||||
elif isinstance(item, str):
|
elif isinstance(item, str):
|
||||||
entity, created = TransactionEntity.objects.get_or_create(name=item)
|
entity, created = TransactionEntity.objects.get_or_create(name=item)
|
||||||
else:
|
else:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
"Invalid entity data. Provide an ID or name."
|
_("Invalid entity data. Provide an ID or name.")
|
||||||
)
|
)
|
||||||
entities.append(entity)
|
entities.append(entity)
|
||||||
return entities
|
return entities
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from apps.common.widgets.tom_select import TomSelect, TomSelectMultiple
|
from apps.common.widgets.tom_select import TomSelect, TomSelectMultiple
|
||||||
|
|
||||||
@@ -124,7 +125,7 @@ class DynamicModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
|||||||
)
|
)
|
||||||
return instance
|
return instance
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValidationError(f"Error creating new instance: {str(e)}")
|
raise ValidationError(_("Error creating new instance"))
|
||||||
|
|
||||||
def clean(self, value):
|
def clean(self, value):
|
||||||
"""
|
"""
|
||||||
@@ -160,6 +161,6 @@ class DynamicModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
|||||||
try:
|
try:
|
||||||
new_objects.append(self._create_new_instance(new_value))
|
new_objects.append(self._create_new_instance(new_value))
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
raise ValidationError(f"Error creating '{new_value}': {str(e)}")
|
raise ValidationError(_("Error creating new instance"))
|
||||||
|
|
||||||
return existing_objects + new_objects
|
return existing_objects + new_objects
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class MonthYearModelField(models.DateField):
|
|||||||
# Set the day to 1
|
# Set the day to 1
|
||||||
return date.replace(day=1).date()
|
return date.replace(day=1).date()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValidationError("Invalid date format. Use YYYY-MM.")
|
raise ValidationError(_("Invalid date format. Use YYYY-MM."))
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
kwargs["widget"] = MonthYearWidget
|
kwargs["widget"] = MonthYearWidget
|
||||||
|
|||||||
@@ -327,7 +327,7 @@ class TransferForm(forms.Form):
|
|||||||
to_account = cleaned_data.get("to_account")
|
to_account = cleaned_data.get("to_account")
|
||||||
|
|
||||||
if from_account == to_account:
|
if from_account == to_account:
|
||||||
raise forms.ValidationError("From and To accounts must be different.")
|
raise forms.ValidationError(_("From and To accounts must be different."))
|
||||||
|
|
||||||
return cleaned_data
|
return cleaned_data
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user