Updated media file fields interfaces
authorThierry Florac <thierry.florac@onf.fr>
Fri, 13 Oct 2017 09:06:29 +0200
changeset 38 cd56db960a5d
parent 37 4deaa649b742
child 39 07dc3a8e34f7
Updated media file fields interfaces
src/pyams_i18n/interfaces/schema.py
src/pyams_i18n/schema.py
--- a/src/pyams_i18n/interfaces/schema.py	Mon Sep 11 15:02:22 2017 +0200
+++ b/src/pyams_i18n/interfaces/schema.py	Fri Oct 13 09:06:29 2017 +0200
@@ -41,9 +41,21 @@
     """I18n file field marker interface"""
 
 
+class II18nMediaField(II18nFileField):
+    """I18n media field marker interface"""
+
+
 class II18nImageField(II18nFileField):
     """I18n image field marker interface"""
 
 
 class II18nThumbnailImageField(II18nImageField):
     """I18n image field with thumbnail marker interface"""
+
+
+class II18nVideoField(II18nMediaField):
+    """I18n video field marker interface"""
+
+
+class II18nAudioField(II18nMediaField):
+    """I18n audio field marker interface"""
--- a/src/pyams_i18n/schema.py	Mon Sep 11 15:02:22 2017 +0200
+++ b/src/pyams_i18n/schema.py	Fri Oct 13 09:06:29 2017 +0200
@@ -17,13 +17,14 @@
 
 # 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
+from pyams_i18n.interfaces.schema import II18nField, II18nTextLineField, II18nTextField, II18nHTMLField, \
+    II18nFileField, II18nMediaField, II18nImageField, II18nThumbnailImageField, II18nVideoField, II18nAudioField
+from z3c.form.interfaces import NOT_CHANGED
+from zope.schema.interfaces import RequiredMissing, WrongType
 
 # import packages
 from persistent.mapping import PersistentMapping
-from pyams_file.schema import FileField, ImageField, ThumbnailImageField
+from pyams_file.schema import FileField, MediaField, ImageField, ThumbnailImageField, VideoField, AudioField
 from pyams_utils.schema import HTMLField
 from zope.interface import implementer
 from zope.schema import Dict, TextLine, Text
@@ -130,7 +131,6 @@
                                             **kwargs)
 
     def _validate(self, value):
-        super(I18nField, self)._validate(value)
         if self.required:
             if self.default:
                 return
@@ -138,51 +138,74 @@
                 raise RequiredMissing
             for lang_value in value.values():
                 if lang_value and (lang_value is not DELETED_FILE):
-                    return
-            raise RequiredMissing
+                    break
+                raise RequiredMissing
+        for lang_value in value.values():
+            if lang_value is NOT_CHANGED:
+                return
+            elif isinstance(lang_value, tuple):
+                try:
+                    filename, stream = lang_value
+                    if not isinstance(filename, str):
+                        raise WrongType(filename, str, '{0}.filename'.format(self.__name__))
+                    if not hasattr(stream, 'read'):
+                        raise WrongType(stream, '<file-like object>', self.__name__)
+                except ValueError:
+                    raise WrongType(lang_value, tuple, self.__name__)
+            elif not self.value_type.schema.providedBy(lang_value):
+                raise WrongType(lang_value, self.value_type.schema, self.__name__)
+
+
+@implementer(II18nMediaField)
+class I18nMediaField(I18nFileField):
+    """I18n media field"""
+
+    def __init__(self, key_type=None, value_type=None, value_min_length=None, value_max_length=None, **kwargs):
+        super(I18nFileField, self).__init__(value_type=MediaField(min_length=value_min_length,
+                                                                  max_length=value_max_length,
+                                                                  required=False),
+                                            **kwargs)
 
 
 @implementer(II18nImageField)
-class I18nImageField(I18nField):
+class I18nImageField(I18nMediaField):
     """I18n image field"""
 
     def __init__(self, key_type=None, value_type=None, value_min_length=None, value_max_length=None, **kwargs):
-        super(I18nImageField, self).__init__(value_type=ImageField(min_length=value_min_length,
-                                                                   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
+        super(I18nFileField, self).__init__(value_type=ImageField(min_length=value_min_length,
+                                                                  max_length=value_max_length,
+                                                                  required=False),
+                                            **kwargs)
 
 
 @implementer(II18nThumbnailImageField)
-class I18nThumbnailImageField(I18nField):
+class I18nThumbnailImageField(I18nImageField):
     """I18n thumbnail image field"""
 
     def __init__(self, key_type=None, value_type=None, value_min_length=None, value_max_length=None, **kwargs):
-        super(I18nThumbnailImageField, self).__init__(value_type=ThumbnailImageField(min_length=value_min_length,
-                                                                                     max_length=value_max_length,
-                                                                                     required=False),
-                                                      **kwargs)
+        super(I18nFileField, self).__init__(value_type=ThumbnailImageField(min_length=value_min_length,
+                                                                           max_length=value_max_length,
+                                                                           required=False),
+                                            **kwargs)
+
+
+@implementer(II18nVideoField)
+class I18nVideoField(I18nMediaField):
+    """I18n video field"""
 
-    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
+    def __init__(self, key_type=None, value_type=None, value_min_length=None, value_max_length=None, **kwargs):
+        super(I18nFileField, self).__init__(value_type=VideoField(min_length=value_min_length,
+                                                                  max_length=value_max_length,
+                                                                  required=False),
+                                            **kwargs)
+
+
+@implementer(II18nAudioField)
+class I18nAudioField(I18nMediaField):
+    """I18n audio field"""
+
+    def __init__(self, key_type=None, value_type=None, value_min_length=None, value_max_length=None, **kwargs):
+        super(I18nFileField, self).__init__(value_type=AudioField(min_length=value_min_length,
+                                                                  max_length=value_max_length,
+                                                                  required=False),
+                                            **kwargs)