class Post(db.Model): """Defines the post database model""" id = db.Column(db.Integer, primary_key=True) message = db.Column(db.String(5000)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref='posts') created_at = db.Column(db.DateTime, default=db.func.now()) updated_at = db.Column(db.DateTime, nullable=True)
class Token(db.Model): id = db.Column(db.Integer, primary_key=True) client_id = db.Column( db.Text, db.ForeignKey('client.client_id'), nullable=False, ) client = db.relationship('Client') user_id = db.Column( db.Integer, db.ForeignKey('user.id') ) user = db.relationship('User') # currently only bearer is supported token_type = db.Column(db.String(40)) access_token = db.Column(db.String(255), unique=True) refresh_token = db.Column(db.String(255), unique=True) expires = db.Column(db.DateTime) _scopes = db.Column(db.Text) def delete(self): db.session.delete(self) db.session.commit() return self @property def scopes(self): if self._scopes: return self._scopes.split() return []
class User(db.Model, UserMixin): """Defines the user database model""" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) _password = db.Column('password', db.Text, nullable=False) first_name = db.Column(db.String(50), nullable=False) last_name = db.Column(db.String(50), nullable=False) picture = db.relationship('Picture', backref='user', uselist=False) created_at = db.Column(db.DateTime, default=db.func.now()) updated_at = db.Column(db.DateTime, nullable=True) def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return self.username @hybrid_property def password(self): return self._password @password.setter def _set_pasword(self, plaintext): self._password = bcrypt.generate_password_hash(plaintext).decode('utf-8') def is_correct_password(self, plaintext): return bcrypt.check_password_hash(self._password, plaintext)
class Grant(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column( db.Integer, db.ForeignKey('user.id', ondelete='CASCADE') ) user = db.relationship('User') client_id = db.Column( db.Text, db.ForeignKey('client.client_id'), nullable=False, ) client = db.relationship('Client') code = db.Column(db.String(255), index=True, nullable=False) redirect_uri = db.Column(db.String(255)) expires = db.Column(db.DateTime) _scopes = db.Column('scopes', db.Text) def delete(self): db.session.delete(self) db.session.commit() return self @property def scopes(self): if self._scopes: return self._scopes.split() return []
class Client(db.Model): # human readable name, not required name = db.Column(db.String(40)) # human readable description, not required description = db.Column(db.String(400)) # creator of the client, not required user_id = db.Column(db.ForeignKey('user.id')) # required if you need to support client credential user = db.relationship('User') client_id = db.Column(db.Text, primary_key=True, server_default=sqlalchemy.text('gen_random_uuid()')) client_secret = db.Column(db.Text, unique=True, index=True, nullable=False) # public or confidential is_confidential = db.Column(db.Boolean) _redirect_uris = db.Column('redirect_uris', db.Text) _default_scopes = db.Column('default_scopes', db.Text) @property def client_type(self): if self.is_confidential: return 'confidential' return 'public' @property def redirect_uris(self): if self._redirect_uris: return self._redirect_uris.split(',') return [] @redirect_uris.setter def redirect_uris(self, redirect_uris): self._redirect_uris = redirect_uris @property def default_redirect_uri(self): return self.redirect_uris[0] @property def default_scopes(self): if self._default_scopes: return self._default_scopes.split(',') return [] @default_scopes.setter def default_scopes(self, scopes): self._default_scopes = scopes
class Picture(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), unique=True) created_at = db.Column(db.DateTime, default=db.func.now()) updated_at = db.Column(db.DateTime, nullable=True)