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

from decorators import pi18n


class ProfileHardwareView:
    @pi18n
    def GET(self, item_type, item):
        orm = web.ctx.orm

        # Testing plugin translations
        test_plugin_translation = _("testing plugin translations")
        print test_plugin_translation

        try:
            if item_type == 'profile':
                hwprofile = orm.query(HardwareProfile).filter(
                        HardwareProfile.id == item).one()
            elif item_type == 'computer':
                computer = orm.query(Computer).filter(
                        Computer.name == item).one()
                hwprofile = computer.hardware[0]

        except NoResultFound:
            raise web.notfound()

        if hwprofile:
            return web.ctx.render.plugins.hardware_inventory.viewprofile(hwprofile)

        return web.notfound()


class UpdateHardwareView:
    def GET(self, computer_name):
        orm = web.ctx.orm

        computer = orm.query(Computer).filter(
                Computer.name == computer_name).first()

        computer.hardware = []
        orm.commit()
        web.ctx.plugin_manager.get_plugin_by_name('hardware_inventory').\
                send_job_create_hardware(computer.id)

        raise web.seeother('/jobs')
'''
class UpdateHardwareProfileView:
    def GET(self, computer_name):
        orm = web.ctx.orm
        try:
            computer = orm.query(Computer).filter(Computer.name == computer_name).one()
        except NoResultFound:
            return "%s not found" % computer_name
        job = Job("hardware_inventory", 'create_hardware_response', 'echo "hola"')
        orm.commit()
        job.send(computer)
        web.seeother("/jobs")
'''
