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

Write handler for create form, and consolidate code to require POST into a CherryPy? tool

Location:
package_branches/invirt-web/cherrypy-rebased/code
Files:
2 edited

Legend:

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

    r2684 r2685  
    147147    help._cp_config['tools.require_login.on'] = False
    148148
     149    def parseCreate(self, fields):
     150        kws = dict([(kw, fields.get(kw)) for kw in 'name description owner memory disksize vmtype cdrom autoinstall'.split()])
     151        validate = validation.Validate(cherrypy.request.login, cherrypy.request.state, strict=True, **kws)
     152        return dict(contact=cherrypy.request.login, name=validate.name, description=validate.description, memory=validate.memory,
     153                    disksize=validate.disksize, owner=validate.owner, machine_type=getattr(validate, 'vmtype', Defaults.type),
     154                    cdrom=getattr(validate, 'cdrom', None),
     155                    autoinstall=getattr(validate, 'autoinstall', None))
     156
     157    @cherrypy.expose
     158    @cherrypy.tools.mako(filename="/list.mako")
     159    @cherrypy.tools.require_POST()
     160    def create(self, **fields):
     161        """Handler for create requests."""
     162        try:
     163            parsed_fields = self.parseCreate(fields)
     164            machine = controls.createVm(cherrypy.request.login, cherrypy.request.state, **parsed_fields)
     165        except InvalidInput, err:
     166            pass
     167        else:
     168            err = None
     169        cherrypy.request.state.clear() #Changed global state
     170        d = getListDict(cherrypy.request.login, cherrypy.request.state)
     171        d['err'] = err
     172        if err:
     173            for field in fields.keys():
     174                setattr(d['defaults'], field, fields.get(field))
     175        else:
     176            d['new_machine'] = parsed_fields['name']
     177        return d
     178
    149179    @cherrypy.expose
    150180    @cherrypy.tools.mako(filename="/helloworld.mako")
     
    219249        @cherrypy.expose
    220250        @cherrypy.tools.mako(filename="/command.mako")
     251        @cherrypy.tools.require_POST()
    221252        def command(self, command_name, machine_id, **kwargs):
    222253            """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")
    225254            back = kwargs.get('back', None)
    226255            try:
     
    337366            return 'location' in d
    338367    return False
    339 
    340 def parseCreate(username, state, fields):
    341     kws = dict([(kw, fields.getfirst(kw)) for kw in 'name description owner memory disksize vmtype cdrom autoinstall'.split()])
    342     validate = validation.Validate(username, state, strict=True, **kws)
    343     return dict(contact=username, name=validate.name, description=validate.description, memory=validate.memory,
    344                 disksize=validate.disksize, owner=validate.owner, machine_type=getattr(validate, 'vmtype', Defaults.type),
    345                 cdrom=getattr(validate, 'cdrom', None),
    346                 autoinstall=getattr(validate, 'autoinstall', None))
    347 
    348 def create(username, state, path, fields):
    349     """Handler for create requests."""
    350     try:
    351         parsed_fields = parseCreate(username, state, fields)
    352         machine = controls.createVm(username, state, **parsed_fields)
    353     except InvalidInput, err:
    354         pass
    355     else:
    356         err = None
    357     state.clear() #Changed global state
    358     d = getListDict(username, state)
    359     d['err'] = err
    360     if err:
    361         for field in fields.keys():
    362             setattr(d['defaults'], field, fields.getfirst(field))
    363     else:
    364         d['new_machine'] = parsed_fields['name']
    365     return templates.list(searchList=[d])
    366368
    367369
     
    653655mapping = dict(
    654656               modify=modify,
    655                create=create,
    656657               unauth=unauthFront,
    657658               admin=admin,
  • package_branches/invirt-web/cherrypy-rebased/code/view.py

    r2673 r2685  
    8383cherrypy.tools.require_login = cherrypy.Tool('on_start_resource', require_login, priority=150)
    8484
     85def require_POST():
     86    """If the request isn't a POST request, raise 405 Method Not Allowed"""
     87    if cherrypy.request.method != "POST":
     88        raise cherrypy.HTTPError(405,
     89                                 "You must submit this request with POST")
     90
     91cherrypy.tools.require_POST = cherrypy.Tool('on_start_resource', require_POST, priority=150)
     92
    8593def remote_user_login():
    8694    """Get the current user based on the SSL or GSSAPI environment variables"""
Note: See TracChangeset for help on using the changeset viewer.