changeset 3257:704dada41df0

plugin identity: fixed update with `get_all` + better new data detection for avatar: - when metadata doesn't exist, update with `get_all` set doesn't crash anymore. - fixed avatarUpdateIsNewData
author Goffi <goffi@goffi.org>
date Sun, 19 Apr 2020 16:38:46 +0200
parents df26f1a9020a
children 7aa01e262e05
files sat/plugins/plugin_misc_identity.py
diffstat 1 files changed, 12 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_identity.py	Tue Apr 14 21:01:13 2020 +0200
+++ b/sat/plugins/plugin_misc_identity.py	Sun Apr 19 16:38:46 2020 +0200
@@ -355,15 +355,16 @@
             except KeyError:
                 update_is_new_data = self.defaultUpdateIsNewData
 
-            if not update_is_new_data(client, entity, cached_data, data):
+            if data is None:
                 if cached_data is None:
                     log.debug(
                         f"{metadata_name} for {entity} is already disabled, nothing to "
                         f"do")
-                else:
-                    log.debug(
-                        f"{metadata_name} for {entity} is already in cache, nothing to "
-                        f"do")
+                    return
+            elif not update_is_new_data(client, entity, cached_data, data):
+                log.debug(
+                    f"{metadata_name} for {entity} is already in cache, nothing to "
+                    f"do")
                 return
 
         # we can't use the cache, so we do the update
@@ -373,7 +374,10 @@
         if metadata.get('get_all', False):
             # get_all is set, meaning that we have to check all plugins
             # so we first delete current cache
-            self.host.memory.delEntityDatum(client, entity, metadata_name)
+            try:
+                self.host.memory.delEntityDatum(client, entity, metadata_name)
+            except KeyError:
+                pass
             # then fill it again by calling get, which will retrieve all values
             await self.get(client, metadata_name, entity)
             return
@@ -452,19 +456,8 @@
                 "cache_uid": cache_uid,
             }
 
-    def avatarUpdateIsNewData(self, client, entity, cached_data, file_path):
-        if cached_data is None:
-            return file_path is not None
-
-        if file_path is not None and file_path == cached_data['path']:
-            if file_path is None:
-                log.debug(
-                    f"Avatar is already disabled for {entity}, nothing to do")
-            else:
-                log.debug(
-                    f"Avatar at {file_path} is already used by {entity}, nothing "
-                    f"to do")
-            return
+    def avatarUpdateIsNewData(self, client, entity, cached_data, new_data):
+        return new_data['path'] != cached_data['path']
 
     async def avatarUpdateDataFilter(self, client, entity, data):
         if not isinstance(data, dict):