Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:03:53 +0200
changeset 98 7244519886cc
parent 97 13cd689be9a3
child 99 bf6607bda112
Updated annotations adapters
src/pyams_file/property.py
src/pyams_file/thumbnail.py
--- a/src/pyams_file/property.py	Mon May 28 09:56:31 2018 +0200
+++ b/src/pyams_file/property.py	Mon May 28 14:03:53 2018 +0200
@@ -18,11 +18,11 @@
 # import interfaces
 from pyams_file.interfaces import IFile, IFileInfo, IFileFieldContainer, DELETED_FILE
 from z3c.form.interfaces import NOT_CHANGED
-from zope.annotation.interfaces import IAnnotations
 from zope.schema.interfaces import IField
 
 # import packages
 from pyams_file.file import FileFactory
+from pyams_utils.adapter import get_annotation_adapter
 from pyramid.threadlocal import get_current_registry
 from zope.interface import alsoProvides
 from zope.lifecycleevent import ObjectCreatedEvent, ObjectRemovedEvent, ObjectAddedEvent
@@ -102,9 +102,7 @@
                 # store file attributes of instance
                 if not IFileFieldContainer.providedBy(instance):
                     alsoProvides(instance, IFileFieldContainer)
-                annotations = IAnnotations(instance)
-                attributes = annotations.get(FILE_CONTAINER_ATTRIBUTES)
-                if attributes is None:
-                    attributes = annotations[FILE_CONTAINER_ATTRIBUTES] = set()
+                attributes = get_annotation_adapter(instance, FILE_CONTAINER_ATTRIBUTES, set,
+                                                    notify=False, locate=False)
                 attributes.add(self.__name)
                 registry.notify(ObjectAddedEvent(value, instance, name))
--- a/src/pyams_file/thumbnail.py	Mon May 28 09:56:31 2018 +0200
+++ b/src/pyams_file/thumbnail.py	Mon May 28 14:03:53 2018 +0200
@@ -20,13 +20,12 @@
 # import interfaces
 from pyams_file.interfaces import IImage, IThumbnail, IThumbnailer, IFileModifiedEvent, IWatermarker, IThumbnailFile
 from pyams_utils.interfaces.tales import ITALESExtension
-from zope.annotation.interfaces import IAnnotations
 from zope.traversing.interfaces import ITraversable
 
 # import packages
 from persistent.dict import PersistentDict
 from pyams_file.file import FileFactory
-from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config
+from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config, get_annotation_adapter
 from pyams_utils.registry import query_utility
 from pyams_utils.request import check_request
 from pyramid.events import subscriber
@@ -50,11 +49,8 @@
 
     def __init__(self, image):
         self.image = image
-        annotations = IAnnotations(image)
-        thumbnails = annotations.get(THUMBNAIL_ANNOTATIONS_KEY)
-        if thumbnails is None:
-            thumbnails = annotations[THUMBNAIL_ANNOTATIONS_KEY] = PersistentDict()
-        self.thumbnails = thumbnails
+        self.thumbnails = get_annotation_adapter(image, THUMBNAIL_ANNOTATIONS_KEY, PersistentDict,
+                                                 notify=False, locate=False)
 
     def get_image_size(self):
         return self.image.get_image_size()
@@ -89,8 +85,7 @@
             return None
 
     def get_geometry(self, selection_name):
-        annotations = IAnnotations(self.image)
-        geometries = annotations.get(THUMBNAIL_GEOMETRY_KEY, {})
+        geometries = get_annotation_adapter(self.image, THUMBNAIL_GEOMETRY_KEY, default={})
         # get default geometry for custom thumbnails
         if ':' in selection_name:
             selection_name, options = selection_name.split(':', 1)
@@ -105,10 +100,8 @@
             return thumbnailer.get_default_geometry(options)
 
     def set_geometry(self, selection_name, geometry):
-        annotations = IAnnotations(self.image)
-        geometries = annotations.get(THUMBNAIL_GEOMETRY_KEY)
-        if geometries is None:
-            geometries = annotations[THUMBNAIL_GEOMETRY_KEY] = PersistentDict()
+        geometries = get_annotation_adapter(self.image, THUMBNAIL_GEOMETRY_KEY, PersistentDict,
+                                            notify=False, locate=False)
         geometries[selection_name] = geometry
         for current_thumbnail_name in self.thumbnails.copy():
             if (current_thumbnail_name == selection_name) or \
@@ -116,8 +109,7 @@
                 self.delete_thumbnail(current_thumbnail_name)
 
     def clear_geometries(self):
-        annotations = IAnnotations(self.image)
-        geometries = annotations.get(THUMBNAIL_GEOMETRY_KEY)
+        geometries = get_annotation_adapter(self.image, THUMBNAIL_GEOMETRY_KEY)
         if geometries is not None:
             for geometry_name in geometries.copy():
                 del geometries[geometry_name]