import web
from main.model import Computer
from sqlalchemy.orm.exc import NoResultFound

class ShowBootMenu:
    def GET(self, ip=None):
        if not ip:
            ip = web.ctx.env.get('REMOTE_ADDR')
        try:
            computer = web.ctx.orm.query(Computer).filter(Computer.ip == ip).one()
            disk_image_plugin = web.ctx.plugin_manager.get_plugin_by_name("disk_image")
            disk_profile = disk_image_plugin.get_diskprofile_for_computer(computer)
            return web.ctx.normal_render.plugins.bootmenu.bootmenu(disk_profile)
        except NoResultFound:
            return web.ctx.normal_render.plugins.bootmenu.bootmenu(None)

class ShowBootMenuAction:
    def GET(self, name=None):
        try:
            computer = web.ctx.orm.query(Computer).filter_by(name=name).one()
            return ShowBootMenu().GET(computer.ip)
        except NoResultFound:
            return web.ctx.normal_render.plugins.bootmenu.bootmenu(None)
