from flask_app import db, login_manager from flask_login import UserMixin from itsdangerous import TimedJSONWebSignatureSerializer as TimedSerializer from itsdangerous import JSONWebSignatureSerializer as Serializer from flask import current_app from datetime import datetime @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) #create association table for users and quotes? user_subscriptions = db.Table( 'user_subscriptions', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('quote_id', db.Integer, db.ForeignKey('quote.id'))) class User(db.Model, UserMixin): """static methods cant modify instnace or class state - provides a way to restrict the data that a method can access, primarily a way to namespace your methods""" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='') password = db.Column(db.String(60), nullable=False) verified = db.Column(db.Boolean(), nullable=False, default=False) currency = db.Column(db.String(), nullable=False, default='GBP') """here lazily load b/c no need to load it unless in the watchlist page..."""
if isinstance(obj, SearchableMixin): delete_from_index(obj.__tablename__, obj) session._changes = None @classmethod def reindex(cls): for obj in cls.query: add_to_index(cls.__tablename__, obj) db.event.listen(db.session, 'before_commit', SearchableMixin.before_commit) db.event.listen(db.session, 'after_commit', SearchableMixin.after_commit) 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(db.Model, UserMixin): 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)) about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow()) posts = db.relationship('Post', backref='author', lazy='dynamic') followed = db.relationship('User', secondary=followers, primaryjoin=(followers.columns.follower_id == id), secondaryjoin=(followers.columns.followed_id == id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
from flask_app import db from flask_login import UserMixin #The collect table indicates a many to many relationship #between users and posts. #In another word, a user can collect various number of posts #meanwhile a post can be collected by multiple users collect = db.Table( 'collect', db.Column('postId', db.Integer, db.ForeignKey('post.id'), primary_key=True), db.Column('userId', db.Integer, db.ForeignKey('user.id'), primary_key=True)) #User is an instance class that stored vital information #about the user. Such as email address, username, password, etc. #We use id as our primary key #Comment, post are all relationship to another instance #class class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(80), unique=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(400)) post = db.relationship('Post', backref='user', lazy=True) comment = db.relationship('Comment', backref='user', lazy=True) def __repr__(self): return '<User %r>' % self.username
from datetime import datetime from flask_app import db, login_manager from flask_login import UserMixin @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) likes = db.Table( 'likes', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('movie_id', db.Integer, db.ForeignKey('movie.id'), primary_key=True)) dislikes = db.Table( 'dislikes', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('movie_id', db.Integer, db.ForeignKey('movie.id'), primary_key=True))
from flask_app import db, app from flask.ext.security import Security, SQLAlchemyUserDatastore, \ UserMixin, RoleMixin from flask_app.forms import ExtendedRegisterForm import json roles_users = db.Table('roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) def date_serial(value): return value.strftime("%Y-%m-%d")+" "+value.strftime("%H:%M:%S") class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) organization = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))
from flask_app import db from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin from flask_app import login # our relationship tables profession_to_skill = db.Table( 'profession_to_skill', db.Model.metadata, db.Column('profession_id', db.Integer, db.ForeignKey('profession.id')), db.Column('skill_id', db.Integer, db.ForeignKey('skill.id'))) skill_to_courses = db.Table( 'skill_to_courses', db.Model.metadata, db.Column('skill_id', db.Integer, db.ForeignKey('skill.id')), db.Column('course_id', db.Integer, db.ForeignKey('course.id'))) class Profession(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), unique=True, nullable=False) skills = db.relationship("Skill", secondary=profession_to_skill) def __repr__(self): return '<Profession %r>' % self.name class Skill(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), unique=True, nullable=False) courses = db.relationship("Course", secondary=skill_to_courses)
from flask_login import UserMixin from flask_app import db, login_manager @login_manager.user_loader def load_user(user_id): return Employee.query.get(int(user_id)) feature_assign = db.Table( 'features_assign', db.Column('employee_id', db.Integer, db.ForeignKey('employee.id'), primary_key=True), db.Column('feature_id', db.Integer, db.ForeignKey('feature.id'), primary_key=True), ) class Employee(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) emp_id = db.Column(db.String(30), unique=True, nullable=False) name = db.Column(db.String(30), unique=False, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) features = db.relationship('Feature', secondary=feature_assign, lazy=True,