more robust code for creating tags
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 3 Feb 2011 15:04:09 +0000 (16:04 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 3 Feb 2011 15:04:09 +0000 (16:04 +0100)
wrap in try/except, enforce roles when set and ['admin'] otherwise, ignore min_role_id

db-config.d/000-functions

index d648263..28468b1 100644 (file)
@@ -25,17 +25,37 @@ g_known_tag_types = [tag_type['tagname'] for tag_type in GetTagTypes()]
 g_known_tag_types.sort()
 
 def SetTagType(tag_type):
+    tagname=tag_type['tagname']
     global g_known_tag_types
+    # handle 'roles' field differently
+    if 'roles' in tag_type:
+        roles=tag_type['roles']
+        del tag_type['roles']
+    else:
+        roles=['admin']
+    # just in case
+    if 'min_role_id' in tag_type:
+        print "WARNING: ignoring deprecated field min_role_id for tagtype %s"%tagname
+        del tag_type['min_role_id']
     # Create/update default slice tag types
-    if tag_type['tagname'] not in g_known_tag_types:
+    if tagname not in g_known_tag_types:
         AddTagType(tag_type)
-        g_known_tag_types.append(tag_type['tagname'])
+        g_known_tag_types.append(tagname)
         g_known_tag_types.sort()
     else:
-        UpdateTagType(tag_type['tagname'], tag_type)
-    if 'roles' in tag_type:
-        for role in tag_type['roles']:
-            AddRoleToTagType(role,tag_type['tagname'])
+        UpdateTagType(tagname, tag_type)
+    # enforce provided roles if present
+    try:
+        old_roles=GetTagTypes(tagname)[0]['roles']
+        for minus_role in set(old_roles).difference(set(roles)):
+            DeleteRoleFromTagType(minus_role,tagname)
+        for plus_role in set(roles).difference(set(old_roles)):
+            AddRoleToType(plus_role,tagname)
+    except:
+        # something went wrong for that tagname, 
+        # but don't want to break the whole startup sequence
+        print "Could not enforce roles on tagtype %s"%tagname
+        pass
 
 # Get list of existing (enabled, global) files
 g_conf_files = GetConfFiles()