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