Exemplo n.º 1
0
class User(db_sql.Model, UserMixin):
    id = db_sql.Column(db_sql.Integer(), primary_key=True)
    fullname = db_sql.Column(db_sql.String(32))
    username = db_sql.Column(db_sql.String(32), nullable=False, unique=True)
    password = db_sql.Column(db_sql.String(95), nullable=False)
    instance = db_sql.Column(db_sql.String(32), nullable=False)
    location = db_sql.Column(db_sql.Text(), nullable=False)
    created = db_sql.Column(db_sql.DateTime())
    updated = db_sql.Column(db_sql.DateTime())

    def add_timestamp(self):
        self.created = datetime_jakarta()
        self.updated = self.created

    def update_timestamp(self):
        self.updated = datetime_jakarta()

    @staticmethod
    def add(data):
        try:
            data.add_timestamp()
            data.password = User.hash_password(data.password)
            db_sql.session.add(data)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def update(data):
        try:
            data.update_timestamp()
            data.password = User.hash_password(data.password)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def delete(id_data):
        try:
            data = User.query.get(id_data)
            db_sql.session.delete(data)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def check_username(username):
        data = User.query.filter(User.username == username).first()
        if data is not None:
            return True
        return False

    @staticmethod
    def hash_password(password):
        hash_password = generate_password_hash(password)
        return hash_password

    def check_password(self, password):
        return check_password_hash(self.password, password)

    def to_dict(self):
        data = {
            'id': self.id,
            'username': self.username,
            'instance': self.instance,
            'location': self.location
        }
        return data
Exemplo n.º 2
0
class User(db_sql.Model, UserMixin):
    id = db_sql.Column(db_sql.Integer(), primary_key=True)
    fullname = db_sql.Column(db_sql.String(32), nullable=False)
    username = db_sql.Column(db_sql.String(32), nullable=False, unique=True)
    password = db_sql.Column(db_sql.String(95), nullable=False)
    created = db_sql.Column(db_sql.DateTime())
    updated = db_sql.Column(db_sql.DateTime())

    def add_timestamp(self):
        self.created = datetime_jakarta()
        self.updated = self.created

    def update_timestamp(self):
        self.updated = datetime_jakarta()

    @staticmethod
    def add(data):
        try:
            data.add_timestamp()
            # data.password = User.hash_password(data.password)
            db_sql.session.add(data)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def update(data):
        try:
            data.update_timestamp()
            # data.password = User.hash_password(data.password)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def delete(id_data):
        try:
            data = User.query.get(id_data)
            db_sql.session.delete(data)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'fullname': self.fullname
        }
Exemplo n.º 3
0
class Category(db_sql.Model):
    id = db_sql.Column(db_sql.Integer(), primary_key=True)
    category_code = db_sql.Column(db_sql.String(10),
                                  nullable=False,
                                  unique=True)
    category_name = db_sql.Column(db_sql.String(32), nullable=False)
    created = db_sql.Column(db_sql.DateTime())
    updated = db_sql.Column(db_sql.DateTime())

    def add_timestamp(self):
        self.created = datetime_jakarta()
        self.updated = self.created

    def update_timestamp(self):
        self.updated = datetime_jakarta()

    @staticmethod
    def add(item):
        try:
            item.add_timestamp()
            db_sql.session.add(item)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def update(item):
        try:
            item.update_timestamp()
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def delete(category_code):
        try:
            item = Category.query.filter(
                Category.category_code == category_code).first()
            db_sql.session.delete(item)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    def to_dict(self):
        data = {
            'id': self.id,
            'category_code': self.category_code,
            'category_name': self.category_name
        }
        return data

    def to_table(self, index, product_total):
        data = {
            'number': index + 1,
            'category_code': self.category_code,
            'category_name': self.category_name,
            'product_total': product_total
        }
        return data

    def to_list(self):
        data = [self.category_code, self.category_name]
        return data
Exemplo n.º 4
0
class Computer(
        db_sql.Model, ):
    id = db_sql.Column(db_sql.Integer(), primary_key=True)
    computer_id = db_sql.Column(db_sql.String(40), nullable=False, unique=True)
    computer_name = db_sql.Column(db_sql.String(32), nullable=False)
    computer_location = db_sql.Column(db_sql.String(32), nullable=False)
    computer_power_status = db_sql.Column(db_sql.Integer(),
                                          default=0,
                                          nullable=False)  # 0: off, 1: on
    computer_cmd = db_sql.Column(db_sql.Integer(), default=1,
                                 nullable=False)  # 0: off, 1: on, 2: restart
    computer_cmd_date = db_sql.Column(db_sql.Integer(),
                                      default=timestamp_jakarta(),
                                      nullable=False)
    computer_ping_timestamp = db_sql.Column(db_sql.Integer(),
                                            default=timestamp_jakarta(),
                                            nullable=False)
    computer_instance = db_sql.Column(db_sql.String(32), nullable=False)
    created = db_sql.Column(db_sql.DateTime())
    updated = db_sql.Column(db_sql.DateTime())

    def add_timestamp(self):
        self.created = datetime_jakarta()
        self.updated = self.created

    def update_timestamp(self):
        self.updated = datetime_jakarta()

    @staticmethod
    def add(data):
        try:
            data.add_timestamp()
            db_sql.session.add(data)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def update(data):
        try:
            data.update_timestamp()
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def delete(id_data):
        try:
            data = Computer.query.get(id_data)
            db_sql.session.delete(data)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    def to_dict(self):
        data = {
            'id': self.computer_id,
            'name': self.computer_name,
            'location': self.computer_location,
            'power_status': self.computer_power_status,
            'instance': self.computer_instance,
            'cmd': self.computer_cmd,
            'cmd_date': self.computer_cmd_date
        }
        return data

    def on_ping(self):
        try:
            self.computer_ping_timestamp = timestamp_jakarta()
            self.computer_power_status = 1
            db_sql.session.commit()
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()

    def on_action(self):
        self.computer_cmd_date = timestamp_jakarta()
Exemplo n.º 5
0
class Transaction(db_sql.Model):
    id = db_sql.Column(db_sql.Integer(), primary_key=True)
    transaction_code = db_sql.Column(db_sql.String(36), nullable=False)
    product_code = db_sql.Column(db_sql.String(10), nullable=False)
    product_price = db_sql.Column(db_sql.Integer(), nullable=False, default=0)
    product_stock_price = db_sql.Column(db_sql.Integer(),
                                        nullable=False,
                                        default=0)
    transaction_type = db_sql.Column(db_sql.Integer(), nullable=False)
    transaction_count = db_sql.Column(db_sql.Integer(), nullable=False)
    transaction_date = db_sql.Column(db_sql.Date(), nullable=False)
    created = db_sql.Column(db_sql.DateTime())
    updated = db_sql.Column(db_sql.DateTime())

    def add_timestamp(self):
        self.created = datetime_jakarta()
        self.updated = self.created

    def update_timestamp(self):
        self.updated = datetime_jakarta()

    @staticmethod
    def add(item):
        try:
            item.transaction_code = str(uuid4())
            item.transaction_date = date_jakarta_o()
            item.add_timestamp()
            db_sql.session.add(item)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def update(item):
        try:
            item.update_timestamp()
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    @staticmethod
    def delete(transaction_code):
        try:
            item = Transaction.query.filter(
                Transaction.product_code == transaction_code).first()
            db_sql.session.delete(item)
            db_sql.session.commit()
            return True
        except Exception as e:
            print(e)
            db_sql.session.rollback()
            db_sql.session.flush()
            return False

    def to_dict(self):
        data = {
            'id': self.id,
            'transaction_code': self.transaction_code,
            'product_code': self.product_code,
            'transaction_type': self.transaction_type,
            'transaction_count': self.transaction_count,
            'transaction_date': str(self.transaction_date),
            'stock_price': self.product_stock_price,
            'product_price': self.product_price
        }
        return data

    def to_table(self, index, product_name):
        data = {
            'number': index + 1,
            'transaction_code': self.transaction_code,
            'product_code': self.product_code,
            'product_name': product_name,
            'product_price': self.product_price,
            'product_stock_price': self.product_stock_price,
            'transaction_type': self.transaction_type,
            'transaction_count': self.transaction_count,
            'transaction_date': str(self.transaction_date),
        }
        return data