from datetime import datetime

from api.__init__ import db


class User(db.Model):
    """
    User Database Modal
    """
    __table_name = 'users'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100))
    email = db.Column(db.String(100), unique=True)
    password = db.Column(db.String(200))
    bucket = db.relationship('Bucket', backref='user')

    def __init__(self, email, password, name=None):
        self.email = email
        self.password = password
        self.name = name

    def save(self):
        """
        Save User to DB        
        """
        db.session.add(self)
        db.session.commit()

    @staticmethod
    def get_all():
        """Get all Users"""
        return User.query.all()

    def delete(self):
        """Delete User"""
        db.session.delete(self)
        db.session.commit()

    def __repr__(self) -> str:
        return "<User: {}>".format(self.name)


class Bucket(db.Model):
    """
    Bucket database Modal
    """
    __table_name = 'buckets'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50))
    desc = db.Column(db.String(100))
    date_added = db.Column(db.DateTime, default=datetime.utcnow())
    item = db.relationship('Item', backref='bucket')
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __init__(self, name, desc):
        self.name = name
        self.desc = desc

    def save(self):
        """
        Save Bucket to DB
        """
        db.session.add(self)
        db.session.commit()

    @staticmethod
    def get_all():
        """Get all Buckets"""
        Bucket.query.all()

    def delete(self):
        """Delete Bucket"""
        db.session.delete(self)
        db.session.commit()

    def __repr__(self) -> str:
        return "<Bucket: {}>".format(self.name)


class Item(db.Model):
    """
    Item Database Modal
    """
    __table_name = 'items'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100))
    status = db.Column(db.String(5))
    date_added = db.Column(db.DateTime, default=datetime.utcnow())
    bucket_id = db.Column(db.Integer, db.ForeignKey('bucket.id'))

    def __init__(self, name, status):
        self.name = name
        self.status = status

    def save(self):
        """
        Save Item to DB
        """
        db.session.add(self)
        db.session.commit()

    @staticmethod
    def get_all():
        """Get all Items"""
        Item.query.all()

    def delete(self):
        """Delete Item"""
        db.session.delete(self)
        db.session.commit()

    def __repr__(self) -> str:
        return "<Item: {}>".format(self.name)

@@ -36,17 +36,8 @@
  api
-.__init__
  imp
@@ -43,16 +43,96 @@
 port db%0A
+from werkzeug.security import generate_password_hash, %5C%0A    check_password_hash%0A
 %0A%0Aclass 
@@ -194,29 +194,30 @@
 %0A    __table
-_
 name
+__
  = 'users'%0A 
@@ -416,63 +416,8 @@
 00))
-%0A    bucket = db.relationship('Bucket', backref='user')
 %0A%0A  
@@ -506,26 +506,91 @@
    self.
-password =
+name = name%0A        self.set_password(password)%0A%0A    def set_password(self,
  passwor
@@ -590,16 +590,18 @@
 password
+):
 %0A       
@@ -606,27 +606,158 @@
    self.
-name = name
+pw_hash = generate_password_hash(password)%0A%0A    def check_password(self, password):%0A        return check_password_hash(self.pw_hash, password)
 %0A%0A    de
@@ -1243,29 +1243,30 @@
 %0A    __table
-_
 name
+__
  = 'buckets'
@@ -1479,61 +1479,8 @@
 ())%0A
-    item = db.relationship('Item', backref='bucket')%0A
     
@@ -1530,16 +1530,17 @@
 ey('user
+s
 .id'))%0A%0A
@@ -2118,13 +2118,14 @@
 able
-_
 name
+__
  = '
@@ -2400,16 +2400,17 @@
 ('bucket
+s
 .id'))%0A%0A

Refactor modals