from clientjob.model import Job
from main.model import Computer
from utils import adquire_thread_orm
from decorators import i18n
import web

class OpengnsysStartJob(Job):
    @i18n
    def get_user_message(self):
        if self.status != 'FINISHED':
            return super(OpengnsysStartJob, self).get_user_message()
        return _('A computer was booted into OpenGnSys')

    def update(self):
        if self.status != 'FINISHED':
            return

        orm = adquire_thread_orm()
        ip = self.client_message.strip()
        print "OpengnsysStartJob ip = '%s'" % ip
        for computer in orm.query(Computer).filter(Computer.ip == ip):
            computer.online = True
            computer.os = "opengnsys"
        orm.commit()

class OpengnsysStopJob(Job):
    @i18n
    def get_user_message(self):
        if self.status != 'FINISHED':
            return super(OpengnsysStopJob, self).get_user_message()
        return _('A computer was stopped from within OpenGnSys')

    def update(self):
        if self.status != 'FINISHED':
            return

        orm = adquire_thread_orm()
        ip = self.client_message.strip()
        for computer in orm.query(Computer).filter(Computer.ip == ip):
            computer.online = False
            computer.os = ""
        orm.commit()
