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()
        computer = orm.query(Computer).filter(Computer.ip == ip).one()
        computer.online = True
        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()
        computer = orm.query(Computer).filter(Computer.ip == ip).one()
        computer.online = False
        orm.commit()
