import sqlite3
from src_1.db import db
import datetime

class UserModel(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(64))
    password = db.Column(db.String(128))
    active = db.Boolean(db.Boolean)
    created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)

    def __init__(self,username,password):
        self.username = username
        self.password = password

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def find_by_username(cls,username):
        return cls.query.filter_by(username=username).first()

    @classmethod
    def get_all_users(cls):
        return cls.query.filter_by().all()

    @classmethod
    def find_by_id(cls,_id):
        return cls.query.filter_by(id=_id).first()