The function "generate_action_handler" creates a function which handles
a string being returned by the underlying action function. However, in
some cases, another type of element might be returned. The action
"swupdate", for example, doesn't use "generate_action_handler"
because
it returns a Task instead of a string.
Make the function "generate_action_handler" more modular so it can
process different types of data.
Signed-off-by: Crístian Viana <vianac(a)linux.vnet.ibm.com>
---
src/kimchi/control/base.py | 37 +++++++++++++++++++++++++++----------
src/kimchi/control/host.py | 18 +++---------------
2 files changed, 30 insertions(+), 25 deletions(-)
diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py
index 1eb10bd..60db1df 100644
--- a/src/kimchi/control/base.py
+++ b/src/kimchi/control/base.py
@@ -65,6 +65,29 @@ class Resource(object):
raise cherrypy.HTTPRedirect(self.uri_fmt % tuple(uri_params), code)
def generate_action_handler(self, action_name, action_args=None):
+ def _render_element(self, ident):
+ self._redirect(ident)
+ uri_params = []
+ for arg in self.model_args:
+ if arg is None:
+ arg = ''
+ uri_params.append(urllib2.quote(arg.encode('utf-8'),
+ safe=""))
+ raise internal_redirect(self.uri_fmt % tuple(uri_params))
+
+ return self._generate_action_handler_base(action_name, _render_element,
+ action_args)
+
+ def generate_action_handler_task(self, action_name, action_args=None):
+ def _render_task(self, task):
+ cherrypy.response.status = 202
+ return kimchi.template.render('Task', task)
+
+ return self._generate_action_handler_base(action_name, _render_task,
+ action_args)
+
+ def _generate_action_handler_base(self, action_name, render_fn,
+ action_args=None):
def wrapper(*args, **kwargs):
validate_method(('POST'), self.role_key, self.admin_methods)
try:
@@ -76,16 +99,10 @@ class Resource(object):
if action_args is not None:
request = parse_request()
model_args.extend(request[key] for key in action_args)
- fn = getattr(self.model, model_fn(self, action_name))
- ident = fn(*model_args)
- self._redirect(ident)
- uri_params = []
- for arg in self.model_args:
- if arg is None:
- arg = ''
- uri_params.append(urllib2.quote(arg.encode('utf-8'),
- safe=""))
- raise internal_redirect(self.uri_fmt % tuple(uri_params))
+
+ action_fn = getattr(self.model, model_fn(self, action_name))
+ action_result = action_fn(*model_args)
+ return render_fn(self, action_result)
except MissingParameter, e:
raise cherrypy.HTTPError(400, e.message)
except InvalidParameter, e:
diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py
index 7bcae72..4362da7 100644
--- a/src/kimchi/control/host.py
+++ b/src/kimchi/control/host.py
@@ -17,12 +17,9 @@
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy
-
from kimchi.control.base import Collection, Resource, SimpleCollection
-from kimchi.control.utils import UrlSubNode, validate_method
-from kimchi.exception import OperationFailed, NotFoundError
-from kimchi.template import render
+from kimchi.control.utils import UrlSubNode
+from kimchi.exception import NotFoundError
@UrlSubNode('host', True)
@@ -41,16 +38,7 @@ class Host(Resource):
self.repositories = Repositories(self.model)
self.users = Users(self.model)
self.groups = Groups(self.model)
-
- @cherrypy.expose
- def swupdate(self):
- validate_method(('POST'), self.role_key, self.admin_methods)
- try:
- task = self.model.host_swupdate()
- cherrypy.response.status = 202
- return render("Task", task)
- except OperationFailed, e:
- raise cherrypy.HTTPError(500, e.message)
+ self.swupdate = self.generate_action_handler_task('swupdate')
@property
def data(self):
--
1.9.3