SHARE
TWEET

Untitled

a guest Feb 21st, 2020 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from datetime import datetime
  2. from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
  3. from flaskblog import db, login_manager, app
  4. from flask_login import UserMixin
  5.  
  6.  
  7. @login_manager.user_loader
  8. def load_user(user_id):
  9.     return User.query.get(int(user_id))
  10.  
  11. class Grade(db.Model):
  12.     id = db.Column(db.Integer, primary_key=True)
  13.     user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
  14.     title = db.Column(db.String(100), nullable=False)
  15.     score = db.Column(db.Integer, nullable=False)
  16.     date_added = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
  17.  
  18. class User(db.Model, UserMixin):
  19.     id = db.Column(db.Integer, primary_key=True)
  20.     username = db.Column(db.String(20), unique=True, nullable=False)
  21.     email = db.Column(db.String(120), unique=True, nullable=False)
  22.     image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
  23.     password = db.Column(db.String(60), nullable=False)
  24.     posts = db.relationship('Post', backref='author', lazy=True)
  25.     first_name = db.Column(db.String(50), nullable=False)
  26.     last_name = db.Column(db.String(50), nullable=False)
  27.  
  28.     def get_reset_token(self, expires_sec=1800):
  29.         s = Serializer(app.config['SECRET_KEY'], expires_sec)
  30.         return s.dumps({'user_id': self.id}).decode('utf-8')
  31.  
  32.     @staticmethod
  33.     def verify_reset_token(token):
  34.         s = Serializer(app.config['SECRET_KEY'])
  35.         try:
  36.             user_id = s.loads(token)['user_id']
  37.         except:
  38.             return None
  39.         return User.query.get(user_id)
  40.  
  41.     def __repr__(self):
  42.         return f"User('{self.username}', '{self.first_name}', '{self.last_name}', '{self.email}', '{self.image_file}')"
  43.  
  44.  
  45. class Post(db.Model):
  46.     id = db.Column(db.Integer, primary_key=True)
  47.     title = db.Column(db.String(100), nullable=False)
  48.     date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
  49.     content = db.Column(db.Text, nullable=False)
  50.     user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
  51.  
  52.     def __repr__(self):
  53.         return f"Post('{self.title}', '{self.date_posted}')"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top