"""Database models""" from sqlalchemy import Column, Integer, String, DateTime, ForeignKey from sqlalchemy.sql import func from app import db, bcrypt class User(db.Model): """A registered user""" id = Column(Integer, primary_key=True) name = Column(String(255), nullable=False) _password = Column('password', String(60)) created_at = Column(DateTime, default=func.now()) @property def password(self): """Getter for the password column""" return self._password @password.setter def password(self, value): if not value: return value = value.encode('utf-8') self._password = bcrypt.generate_password_hash(value).decode('utf-8') def authenticate(self, plaintext): """checks the plaintext password against the stored hash""" if self.password is None: return False stored_hash = self.password.encode('utf-8') plaintext = plaintext.encode('utf-8') return bcrypt.check_password_hash(stored_hash, plaintext) class Tile(db.Model): """A map tile""" id = Column(Integer, primary_key=True) x = Column(Integer, nullable=False) y = Column(Integer, nullable=False) z = Column(Integer, nullable=False) created_at = Column(DateTime, default=func.now()) user_id = Column(Integer, ForeignKey('user.id'))