Updated I18n file fields validation
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 13:39:06 +0200
changeset 35 357ee895fd64
parent 34 bcd60d1cc40d
child 36 f4164500d545
Updated I18n file fields validation
src/pyams_i18n/schema.py
--- a/src/pyams_i18n/schema.py	Wed Jul 12 14:19:16 2017 +0200
+++ b/src/pyams_i18n/schema.py	Mon Sep 11 13:39:06 2017 +0200
@@ -16,6 +16,7 @@
 # import standard library
 
 # import interfaces
+from pyams_file.interfaces import DELETED_FILE
 from pyams_i18n.interfaces.schema import II18nField, II18nTextLineField, II18nTextField, II18nFileField, II18nImageField, \
     II18nThumbnailImageField, II18nHTMLField
 from zope.schema.interfaces import RequiredMissing
@@ -128,6 +129,18 @@
                                                                  required=False),
                                             **kwargs)
 
+    def _validate(self, value):
+        super(I18nField, self)._validate(value)
+        if self.required:
+            if self.default:
+                return
+            if not value:
+                raise RequiredMissing
+            for lang_value in value.values():
+                if lang_value and (lang_value is not DELETED_FILE):
+                    return
+            raise RequiredMissing
+
 
 @implementer(II18nImageField)
 class I18nImageField(I18nField):
@@ -139,6 +152,18 @@
                                                                    required=False),
                                              **kwargs)
 
+    def _validate(self, value):
+        super(I18nField, self)._validate(value)
+        if self.required:
+            if self.default:
+                return
+            if not value:
+                raise RequiredMissing
+            for lang_value in value.values():
+                if lang_value and (lang_value is not DELETED_FILE):
+                    return
+            raise RequiredMissing
+
 
 @implementer(II18nThumbnailImageField)
 class I18nThumbnailImageField(I18nField):
@@ -149,3 +174,15 @@
                                                                                      max_length=value_max_length,
                                                                                      required=False),
                                                       **kwargs)
+
+    def _validate(self, value):
+        super(I18nField, self)._validate(value)
+        if self.required:
+            if self.default:
+                return
+            if not value:
+                raise RequiredMissing
+            for lang_value in value.values():
+                if lang_value and (lang_value is not DELETED_FILE):
+                    return
+            raise RequiredMissing