# -*- coding: utf-8 -*-

from sqlalchemy.schema import Column
from sqlalchemy.types import Integer, Text, TIMESTAMP
from sqlalchemy.dialects.mysql import TINYINT,SMALLINT
from application import db
import datetime

class Tasks(db.Model):
    __tablename__ = "tasks"

    id              = Column(Integer, primary_key=True)
    type            = Column(SMALLINT,nullable=False,index=True)
    target_id       = Column(Integer,nullable=False,index=True)
    command         = Column(Text, nullable=False)
    success         = Column(Text, nullable=False)
    fail            = Column(Text, nullable=False)
    hosts           = Column(Text, nullable=False)
    status          = Column(TINYINT(3), nullable=False,index=True)
    note            = Column(Text,nullable=False)
    created         = Column(TIMESTAMP, nullable=False,server_default='0000-00-00 00:00:00')
    updated         = Column(TIMESTAMP, nullable=False,server_default='0000-00-00 00:00:00')

    STATUS_UNPROCESS = 0
    STATUS_SUCCESS = 1
    STATUS_FAIL = 2

    TYPE_AUTO =1
    TYPE_CUSTOM=2

    def __init__(self, type, target_id, hosts,command, status,success='',fail='', note=''):
        self.type       = type
        self.target_id  = target_id
        self.hosts      = hosts
        self.command    = command
        self.success    = success
        self.fail       = fail
        self.status     = status
        self.note       = note
        self.created    = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.updated    = self.created


