From b76afc3d3abee3df8701a352b3c546be83c150c4 Mon Sep 17 00:00:00 2001
From: root <root@tcsia13.intinfra.com>
Date: Wed, 1 Apr 2009 12:00:37 +0200
Subject: [PATCH] use redirects to return to list page after operation or search

---
 cobbler/webui/CobblerWeb.py |   34 +++++++++++++++++-----------------
 scripts/index.py            |    9 ++++++---
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/cobbler/webui/CobblerWeb.py b/cobbler/webui/CobblerWeb.py
index 30b305e..21fce39 100644
--- a/cobbler/webui/CobblerWeb.py
+++ b/cobbler/webui/CobblerWeb.py
@@ -102,6 +102,17 @@ class CobblerWeb(object):
         tmpl = Template( file=filepath, searchList=[data] )
         return str(tmpl)
 
+    def __redirect(self, mode=None, args=None):
+        """
+        Redirect to URL using the redirect helper in index.py
+        """
+        url=self.base_url
+        if mode is not None:
+            url="%s?mode=%s" % (url,mode)
+        if args is not None:
+            url="%s%s" % (url,args)
+        return "# REDIRECT "+url
+
     def modes(self):
         """
         Returns a list of methods in this object that can be run as web
@@ -173,18 +184,7 @@ class CobblerWeb(object):
         self.remote.modify_filter(filterobj, 'criteria', criteria, self.token)
         self.remote.save_filter(filterobj, self.token, savemode,editmode)
 
-        if what == "distro":
-            return self.distro_list(filtername=filtername)
-        elif what == "profile":
-            return self.profile_list(filtername=filtername)
-        elif what == "system":
-            return self.system_list(filtername=filtername)
-        elif what == "image":
-            return self.image_list(filtername=filtername)
-        elif what == "repo":
-            return self.repo_list(filtername=filtername)
-        else:
-            raise "internal error, unknown search type"
+        return self.__redirect(what+"_list","&"+filtername)
 
     def __search(self,what,filtername=None):
         if not self.__xmlrpc_setup():
@@ -483,7 +483,7 @@ class CobblerWeb(object):
             for system in systems:
                 self.remote.modify_system(system, 'netboot_enabled', netboot, self.token)
                 self.remote.save_system(system, self.token)
-            return self.system_list()
+            return self.__redirect("system_list")
         except Exception, e:
             log_exc(self.apache)
             return self.error_page("Error while saving system: %s" % str(e))
@@ -522,7 +522,7 @@ class CobblerWeb(object):
                 systems.append(self.remote.get_system_handle(targetname,self.token))
             for system in systems:
                 self.remote.power_system(system, power, self.token)
-            return self.system_list()
+            return self.__redirect("system_list")
         except Exception, e:
             log_exc(self.apache)
             return self.error_page("Error while controlling power of system: %s" % str(e))
@@ -539,7 +539,7 @@ class CobblerWeb(object):
                 systems.append(self.remote.get_system_handle(targetname,self.token))
             for system in systems:
                 self.remote.rename_system(system, name, self.token)
-            return self.system_list()
+            return self.__redirect("system_list")
         except Exception, e:
             log_exc(self.apache)
             return self.error_page("Error while renaming system: %s" % str(e))
@@ -553,7 +553,7 @@ class CobblerWeb(object):
         try:
             for targetname in targetlist.split():
                 self.remote.remove_system(targetname, self.token)
-            return self.system_list()
+            return self.__redirect("system_list")
         except Exception, e:
             log_exc(self.apache)
             return self.error_page("Error while deleting system: %s" % str(e))
@@ -731,7 +731,7 @@ class CobblerWeb(object):
             log_exc(self.apache)
             return self.error_page("Error while saving system: %s" % str(e))
 
-        return self.system_list()
+        return self.__redirect("system_list")
 
     def system_search(self,**spam):
         return self.__search('system')
diff --git a/scripts/index.py b/scripts/index.py
index 5acfe09..95f65c8 100755
--- a/scripts/index.py
+++ b/scripts/index.py
@@ -132,9 +132,12 @@ def handler(req):
         func = getattr( cw, 'error_page' )
         content = func( "Invalid Mode: \"%s\"" % mode )
 
-    # apache.log_error("%s:%s ... %s" % (my_user, my_uri, str(form)))
-    req.content_type = "text/html;charset=utf-8"
-    req.write(unicode(content).encode('utf-8'))
+    if content.startswith("# REDIRECT "):
+        util.redirect(req, location=content[11:], permanent=False)
+    else:
+        # apache.log_error("%s:%s ... %s" % (my_user, my_uri, str(form)))
+        req.content_type = "text/html;charset=utf-8"
+        req.write(unicode(content).encode('utf-8'))
     
     if not content.startswith("# ERROR") and content.find("<!-- ERROR -->") == -1:
        return apache.OK
-- 
1.5.4.7

