class ArticleTag(db.Model): # references Article and Tag: article_url = db.Column(db.String(2048), db.ForeignKey('article.url'), primary_key=True) tag_text = db.Column(db.String(500), db.ForeignKey('tag.text'), primary_key=True) def __repr__(self): return f"ArticleTag(article_url: {self.article_url}, tag: '{self.tag_text}')"
class Step(db.Model): __tablename__ = 'step' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), unique=False) description = db.Column(db.String(400), unique=False) link = db.Column(db.String(2048), unique=False) step_order = db.Column(db.Integer, index=True, unique=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) path_id = db.Column(db.Integer, db.ForeignKey('path.id')) def __repr__(self): return '<Step {}>'.format(self.name)
class Relationship(db.Model): """Data model for representation of relationships in current diagram. Use names 'from'/'to' as composite primary key because this combo is unique """ __tablename__ = 'relationship' from_name = db.Column(db.String(200), db.ForeignKey('class.name'), primary_key=True) to_name = db.Column(db.String(200), db.ForeignKey('class.name'), primary_key=True) rel_type = db.Column(db.String(10)) # agg,comp,gen,none parent_class = relationship("Class", back_populates="class_relationships", foreign_keys=[from_name, to_name], primaryjoin='Class.name==Relationship.from_name') __mapper_args__ = { 'confirm_deleted_rows': False }
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): return f'<Post {self.body}>'
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # The 'user' referenced here is the name of the table for the model def __repr__(self): return f'<Post {self.body}>'
class ArticleKeyword(db.Model): # references Article: article_url = db.Column(db.String(2048), db.ForeignKey('article.url'), primary_key=True) keyword = db.Column(db.String(200), primary_key=True) def __repr__(self): return f"ArticleKeyword(article_url: {self.article_url}, keyword: '{self.keyword}')"
class Attribute(db.Model): """Data model for representation of attributes in current diagram. Use 'name'/'parent_name' composite as primary key because this combo is unique """ __tablename__ = 'attribute' attribute = db.Column(db.String(200), primary_key=True) attr_type = db.Column(db.String(10)) #field, method date_created = db.Column(db.DateTime, default=datetime.utcnow) class_name = db.Column(db.String(200), db.ForeignKey('class.name'), primary_key=True) parent_class = relationship("Class", back_populates="class_attributes", foreign_keys=[class_name], primaryjoin='Class.name==Attribute.class_name')
class Path(db.Model): __tablename__ = 'path' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), index=True, unique=False) description = db.Column(db.String(400), index=True, unique=False) is_public = db.Column(db.Boolean, default=True, nullable=False) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) steps = db.relationship('Step', backref='path', lazy='dynamic') def __repr__(self): return '<Path {}>'.format(self.name)
class ArticleAction(db.Model): # references Article: article_url = db.Column(db.String(2048), db.ForeignKey('article.url'), primary_key=True) action = db.Column(db.String(100), primary_key=True) last_update_time = db.Column(db.DateTime, nullable=False, default=datetime.now()) def __repr__(self): return f"ArticleAction(article_url: {self.article_url}, action: {self.action})"
from flask_login import UserMixin from app_package import db, login from hashlib import md5 from time import time import jwt ''' Defining models for the database tables, the fields and the data types (varchar, integer etc.). So far, there are 3 tables in the db - User, Post and followers ''' # This is an auxiliary table and has no data other than foreign keys, so no need for a model class for it: followers = db.Table('followers', db.Column('follower_id', db.Integer, db.ForeignKey('user.id')), db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))) class User(UserMixin, db.Model): ''' db.Model is a base class for all models from flask-sqlalchemy. The UserMixin class includes generic implementations that are appropriate for most user model classes( is_authenticated, is_active etc.) ''' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128))
class Picture(db.Model): id = db.Column(db.Integer, primary_key=True) fileName = db.Column(db.String(100), nullable=False) filePath = db.Column(db.String(120), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) state_id = db.Column(db.Integer, db.ForeignKey('state.id'))
from flask import current_app from app_package import db, login_manager # import the db and login_manager variables from our app package file from flask_login import UserMixin @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) # https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/#many-to-many-relationships statesVisited = db.Table( 'statesvisited', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('state_id', db.Integer, db.ForeignKey('state.id'), primary_key=True)) class User(db.Model, UserMixin ): # This class inherits from both 'db.model' and 'UserMixin' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), unique=True, nullable=False) password = db.Column(db.String, nullable=False) firstname = db.Column(db.String(100), nullable=False) lastname = db.Column(db.String(100), nullable=False) email = db.Column(db.String(120), nullable=False)
class OAuth(OAuthConsumerMixin, db.Model): provider_user_id = db.Column(db.String(256), unique=True, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False) user = db.relationship(User)