class User(db.Model, UserMixin): __tablename__ = 'users' 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()) password = db.Column(db.String(60), nullable=False) ## backref allows relationship lazy will load db as necessary posts = db.relationship('Post', backref='user', lazy=True) #TOKEN CREATION-- will expire in 30 minutes for the user def get_reset_token(self, expires_sec=1800): s = Serializer(current_app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') # TOKEN VERIFICATION #staticmethod tell python not to accept self as an argument only the given arugment @staticmethod def verify_reset_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}','{self.image_file}')"
class User(db.Model, UserMixin): 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='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) # Get token and return with payload def get_reset_token(self, expires_sec=1800): # 30 minutes s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') # Try except because token may be expired # Tell python that self is not used in method @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class User(db.Model, UserMixin): # table no 1: __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(64), nullable=False, default='default_profile.png') email = db.Column(db.String(64), unique=True, index=True) # what does later two attributes mean? username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) # 'posts' connects BlogPosts to a User Author posts = db.relationship('BlogPost', backref='author', lazy=True) def __init__(self, email, username, password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def __repr__(self): return f"Username: {self.username}"
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 '<Post {}>'.format(self.body)
class wifi(db.Model): __tablename__ = "wifi" id = db.Column(db.Integer, primary_key=True) SSID_ = db.Column(db.String(120)) SIGNAL_ = db.Column(db.Integer) MAC_ = db.Column(db.String(120)) def __init__(self, SSID_, SIGNAL_, MAC_): self.SSID_ = SSID_ self.SIGNAL_ = SIGNAL_ self.MAC_ = MAC_
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class Blog(db.Model): __tablename__ = "blogs" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) comments = db.relationship('Comment', backref='post', lazy=True) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class user(db.Model): __tablename__ = "users_data" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(20), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) #posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(15), unique=True, nullable=False) email = db.Column(db.String(30), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) blog = db.relationship('Blog', backref='author', lazy=True) comments = db.relationship('Comment', backref='author', lazy=True) def __repr__(self): return f'User{self.username}'
class temp(db.Model): __tablename__ = "temp" id = db.Column(db.Integer, primary_key=True) date_ = db.Column(db.String(120)) temp_ = db.Column(db.Integer) def __init__(self, date_, temp_): self.date_ = date_ self.temp_ = temp_ # class Post(db.Model): # id = db.Column(db.Integer, primary_key=True) # title = db.Column(db.String(100), nullable=False) # date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) # content = db.Column(db.Text, nullable=False) # user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # # def __repr__(self): # return f"Post'{self.title}', '{self.date_posted}')"
class Comment(db.Model): __tablename__ = "comments" id = db.Column(db.Integer, primary_key=True) comment = db.Column(db.Text, nullable=False) posted_date = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) blog_id = db.Column(db.Integer, db.ForeignKey('blogs.id')) def save_comment(self): db.session.add(self) db.session.commit() @classmethod def get_comment(cls, id): comments = Comment.query.filter_by(post_id=id).all() return comments def __repr__(self): return f"Comment('{self.comment}', '{self.posted_date}')"
class BlogPost(db.Model): # table no 2: users = db.relationship(User) id = db.Column(db.Integer, primary_key=True) # user_id connects User and BlogPost user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title = db.Column(db.String(140), nullable=False) text = db.Column(db.Text, nullable=True) def __init__(self, title, text, user_id): self.title = title self.text = text self.user_id = user_id def __repr__(self): return f"Post ID: {self.id} -- Date: {self.date} --- Title: {self.title}"
class Articles(db.Model): __tablename__ = "articles" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) author = db.Column(db.String) body = db.Column(db.String) create_date = db.Column(db.DateTime, default=datetime.datetime.now(TIMEZONE_INDIA)) image_name = db.Column(db.String)
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) post_image = db.Column(db.String()) #relationship user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}'"
class BlogPost(db.Model): __tablename__ = 'blogposts' users = db.relationship(User) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) picture = db.Column(db.String(64), nullable=False, default='default.png') date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title = db.Column(db.String(140), nullable=False) text = db.Column(db.Text, nullable=False) def __init__(self, title, picture, text, user_id): self.title = title self.picture = picture self.text = text self.user_id = user_id def __repr__(self): return f"Post ID: {self.id}"
class Directory(db.Model): __tablename__ = "directory" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) family = db.Column(db.String) email = db.Column(db.String) username = db.Column(db.String) password = db.Column(db.String) GENDER = db.Column(db.String) PROFESSION = db.Column(db.String) father = db.Column(db.String) mother = db.Column(db.String) gotra = db.Column(db.String) marraige = db.Column(db.String) children = db.Column(db.Integer) phone = db.Column(db.String) address = db.Column(db.String) dob = db.Column(db.Date) profile_image = db.Column(db.String)
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') followed = 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 __repr__(self): return '<User {}>'.format(self.username) 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_reset_password_token(self, expires_in=600): return jwt.encode( { 'reset_password': self.id, 'exp': time() + expires_in }, current_app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8') @staticmethod def verify_reset_password_token(token): try: id = jwt.decode(token, current_app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password'] except: return return User.query.get(id) def avatar(self, size): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( digest, size) def follow(self, user): if not self.is_following(user): self.followed.append(user) def unfollow(self, user): if self.is_following(user): self.followed.remove(user) def is_following(self, user): return self.followed.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())
from datetime import datetime from hashlib import md5 from time import time from flask import current_app from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin from hashlib import md5 import jwt from blogsite import db, login 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') followed = 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 __repr__(self): return '<User {}>'.format(self.username)