Handle changing ComplexList options
[cascardo/ipsilon.git] / ipsilon / admin / common.py
old mode 100755 (executable)
new mode 100644 (file)
index 2b83314..4e92d1f
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-#
 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
 #
 # see file 'COPYING' for use and warranty information
@@ -68,6 +66,67 @@ class AdminPluginConfig(AdminPage):
                                                          self._po.name),
                               config=self._po.get_config_obj())
 
+    def get_complex_list_value(self, name, old_value, **kwargs):
+        delete = list()
+        change = dict()
+        for key, val in kwargs.iteritems():
+            if not key.startswith(name):
+                continue
+            n = key[len(name):]
+            if len(n) == 0 or n[0] != ' ':
+                continue
+            try:
+                index, field = n[1:].split('-')
+            except ValueError:
+                continue
+            if field == 'delete':
+                delete.append(int(index))
+            elif field == 'name':
+                change[int(index)] = val
+
+        if len(delete) == 0 and len(change) == 0:
+            return None
+
+        value = old_value
+
+        # remove unwanted changes
+        for i in delete:
+            if i in change:
+                del change[i]
+
+        # perform requested changes
+        for index, val in change.iteritems():
+            val_list = val.split('/')
+            stripped = list()
+            for v in val_list:
+                stripped.append(v.strip())
+            if len(stripped) == 1:
+                stripped = stripped[0]
+            if len(value) <= index:
+                value.extend([None]*(index + 1 - len(value)))
+            value[index] = stripped
+
+            if len(value[index]) == 0:
+                value[index] = None
+
+        # the previous loop may add 'None' entries
+        # if any still exists mark them to be deleted
+        for i in xrange(0, len(value)):
+            if value[i] is None:
+                delete.append(i)
+
+        # remove duplicates and set in reverse order
+        delete = list(set(delete))
+        delete.sort(reverse=True)
+
+        for i in delete:
+            if len(value) > i:
+                del value[i]
+
+        if len(value) == 0:
+            value = None
+        return value
+
     @admin_protect
     def GET(self, *args, **kwargs):
         return self.root_with_msg()
@@ -102,6 +161,12 @@ class AdminPluginConfig(AdminPage):
                         aname = '%s_%s' % (name, a)
                         if aname in kwargs:
                             value.append(a)
+                elif type(option) is pconfig.ComplexList:
+                    value = self.get_complex_list_value(name,
+                                                        option.get_value(),
+                                                        **kwargs)
+                    if value is None:
+                        continue
                 else:
                     continue
 
@@ -331,5 +396,5 @@ class Admin(AdminPage):
         cherrypy.response.headers.update({'Content-Type': 'image/svg+xml'})
         urls = self.get_menu_urls()
         # pylint: disable=star-args
-        return self._template('admin/ipsilon-scheme.svg', **urls)
+        return str(self._template('admin/ipsilon-scheme.svg', **urls))
     scheme.public_function = True