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 Animal(db.Model): __tablename__ = 'animal' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) color = db.Column(db.String(100)) @property def serialize(self): return {'id': self.id, 'name': self.name, 'color': self.color}
class UserFunction(db.Model): """ Model for the User Function association table """ __tablename__ = 'user_function' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) function_id = db.Column(db.Integer, db.ForeignKey('functions.id'))
class UserEnvironment(db.Model): """ Model for the User Environment association table """ __tablename__ = 'user_environment' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) environment_id = db.Column(db.Integer, db.ForeignKey('environments.id'))
class Function(db.Model): """ Model for User Defined Functions """ __tablename__ = 'functions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) code = db.Column(db.String) private = db.Column(db.Boolean) # todo: might need to do a language field def __init__(self, name, code): self.name = name self.code = code
class User(db.Model): """ Model for Users """ __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer) environments = db.relationship('Environment', backref='users', secondary='user_environment') functions = db.relationship('Function', backref='users', secondary='user_function') def __init__(self, user_id): self.user_id = user_id
class Article(db.Model): __tablename__ = 'articles' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) body = db.Column(db.String(1000), unique=False, nullable=True) img_url = db.Column(db.String(1000), unique=False, nullable=True) url = db.Column(db.String(1000), unique=False, nullable=True) created_ts = db.Column(db.Integer, unique=True, nullable=True) def __repr__(self): return '<Article %r>' % self.title
class User(UserMixin, db.Model): 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)) posts = db.relationship('Post', backref='author', lazy='dynamic') bio = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) followed_by = db.relationship( 'User', secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref( 'followers', lazy='dynamic' ), lazy='dynamic' ) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def get_avatar(self): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return GRAVATAR_URL.replace('hash', digest) def follow(self, user): if not self.is_following(user): self.followed_by.append(user) def unfollow(self, user): if self.is_following(user): self.followed_by.remove(user) def is_following(self, user): return self.followed_by.filter( followers.c.followed_id == user.id ).count() > 0 def followed_posts(self): followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id)).filter(followers.c.follower_id == self.id) own = Post.query.filter_by(user_id=self.id) return followed.union(own).order_by(Post.timestamp.desc()) def __repr__(self): return f"User {self.username}"
class Environment(db.Model): """ Model for User Environments """ __tablename__ = 'environments' id = db.Column(db.Integer, primary_key=True) env_name = db.Column(db.String, unique=True) private_key = db.Column(db.String) user_ocid = db.Column(db.String) tenancy_ocid = db.Column(db.String) fingerprint = db.Column(db.String) region = db.Column(db.String) def __init__(self, env_name, private_key, user_ocid, tenancy_ocid, fingerprint, region): self.env_name = env_name self.private_key = private_key self.user_ocid = user_ocid self.tenancy_ocid = tenancy_ocid self.fingerprint = fingerprint self.region = region
from hashlib import md5 from datetime import datetime from flask_login import UserMixin from werkzeug.security import generate_password_hash, check_password_hash from flask_app.app import db, login GRAVATAR_URL = 'https://www.gravatar.com/avatar/{hash}' 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): 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)) posts = db.relationship('Post', backref='author', lazy='dynamic') bio = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) followed_by = db.relationship( 'User', secondary=followers, primaryjoin=(followers.c.follower_id == id),