Ignore:
Timestamp:
Dec 20, 2009, 9:45:14 PM (15 years ago)
Author:
broder
Message:

Make commands work from list and info pages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • package_branches/invirt-web/cherrypy-rebased/code/main.py

    r2680 r2684  
    5959    @cherrypy.expose
    6060    @cherrypy.tools.mako(filename="/list.mako")
    61     def list(self):
     61    def list(self, result=None):
    6262        """Handler for list requests."""
    6363        checkpoint.checkpoint('Getting list dict')
    6464        d = getListDict(cherrypy.request.login, cherrypy.request.state)
     65        if result is not None:
     66            d['result'] = result
    6567        checkpoint.checkpoint('Got list dict')
    6668        return d
     
    215217                     authtoken=token)
    216218            return d
     219        @cherrypy.expose
     220        @cherrypy.tools.mako(filename="/command.mako")
     221        def command(self, command_name, machine_id, **kwargs):
     222            """Handler for running commands like boot and delete on a VM."""
     223            if cherrypy.request.method != "POST":
     224                raise InvalidInput("request.method", command_name, "You must execute commands via POST")
     225            back = kwargs.get('back', None)
     226            try:
     227                d = controls.commandResult(cherrypy.request.login, cherrypy.request.state, command_name, machine_id, kwargs)
     228                if d['command'] == 'Delete VM':
     229                    back = 'list'
     230            except InvalidInput, err:
     231                if not back:
     232                    raise
     233                print >> sys.stderr, err
     234                result = err
     235            else:
     236                result = 'Success!'
     237                if not back:
     238                    return d
     239            if back == 'list':
     240                cherrypy.request.state.clear() #Changed global state
     241                raise cherrypy.InternalRedirect('/list?result=%s' % urllib.quote(result))
     242            elif back == 'info':
     243                raise cherrypy.HTTPRedirect(cherrypy.request.base + '/machine/%d/' % machine_id, status=303)
     244            else:
     245                raise InvalidInput('back', back, 'Not a known back page.')
    217246
    218247    machine = MachineView()
     
    432461    return disk_fields
    433462
    434 def command(username, state, path, fields):
    435     """Handler for running commands like boot and delete on a VM."""
    436     back = fields.getfirst('back')
    437     try:
    438         d = controls.commandResult(username, state, fields)
    439         if d['command'] == 'Delete VM':
    440             back = 'list'
    441     except InvalidInput, err:
    442         if not back:
    443             raise
    444         print >> sys.stderr, err
    445         result = err
    446     else:
    447         result = 'Success!'
    448         if not back:
    449             return templates.command(searchList=[d])
    450     if back == 'list':
    451         state.clear() #Changed global state
    452         d = getListDict(username, state)
    453         d['result'] = result
    454         return templates.list(searchList=[d])
    455     elif back == 'info':
    456         machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
    457         return ({'Status': '303 See Other',
    458                  'Location': 'info?machine_id=%d' % machine.machine_id},
    459                 "You shouldn't see this message.")
    460     else:
    461         raise InvalidInput('back', back, 'Not a known back page.')
    462 
    463463def modifyDict(username, state, fields):
    464464    """Modify a machine as specified by CGI arguments.
     
    652652
    653653mapping = dict(
    654                command=command,
    655654               modify=modify,
    656655               create=create,
Note: See TracChangeset for help on using the changeset viewer.