From 3ed0e0b48d90d7b4b58371f296756007bd07f573 Mon Sep 17 00:00:00 2001
From: Peter Vreman <peter.vreman@acision.com>
Date: Wed, 1 Apr 2009 20:24:50 +0200
Subject: [PATCH] use redirects to return to list page after operation

---
 cobbler/webui/CobblerWeb.py |   21 ++++++++++++++++-----
 scripts/index.py            |    9 ++++++---
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/cobbler/webui/CobblerWeb.py b/cobbler/webui/CobblerWeb.py
index 150a2f8..453e39a 100644
--- a/cobbler/webui/CobblerWeb.py
+++ b/cobbler/webui/CobblerWeb.py
@@ -101,6 +101,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
@@ -461,7 +472,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))
@@ -500,7 +511,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))
@@ -517,7 +528,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))
@@ -531,7 +542,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))
@@ -709,7 +720,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

