from app import db, metadata
from sqlalchemy.orm import declarative_base


class classproperty(property):
    def __get__(self, owner_self, owner_cls):
        return self.fget(owner_cls)


class Base:
    @classproperty
    def query(cls):
        return db.session.query(cls)

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def __eq__(self, other):
        if hasattr(self, 'id') and hasattr(other, 'id'):
            return self.id == other.id
        else:
            return self == other

    def __hash__(self):
        if hasattr(self, 'id'):
            return hash(self.id)
        else:
            return super().__hash__()


Base = declarative_base(cls=Base, metadata=metadata)
