text: import hashlib
  from werkzeug.security import generate_password_hash, check_password_hash
  from datetime import datetime
  from flask import current_app, request
  from flask_login import UserMixin
  from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
  from .. import db, login_manager
  
  
  class User(UserMixin, db.Model):
      __tablename__ = 'users'
      id = db.Column(db.Integer, primary_key=True)
      email = db.Column(db.String(64), unique=True, index=True)
      username = db.Column(db.String(64), unique=True, index=True)
      password_hash = db.Column(db.String(128))
      confirmed = db.Column(db.Boolean, default=True)
      avatar_hash = db.Column(db.String(32))
      member_since = db.Column(db.DateTime, default=datetime.utcnow)
  
      minutes_for_break = db.Column(db.Integer, default=10)
      spaced_duration = db.Column(db.Integer, default=15)
      collections = db.relationship('Collection', backref='user', lazy='dynamic')
      flashcards = db.relationship('Flashcard', backref='user', lazy='dynamic')
  
      @property
      def password(self):
          raise AttributeError('Password is not a readable Attribute')
  
      @password.setter
      def password(self, password):
          self.password_hash = generate_password_hash(password)
  
      def verify_password(self, password):
          return check_password_hash(self.password_hash, password)
  
      def generate_confirmation_token(self, expiration=3600):
          s = Serializer(current_app.config['SECRET_KEY'], expiration)
          return s.dumps({'confirm': self.id})
  
      def confirm(self, token):
          s = Serializer(current_app.config['SECRET_KEY'])
          try:
              data = s.loads(token)
          except:
              return False
          if data.get('confirm') != self.id:
              return False
          self.confirmed = True
          db.session.add(self)
          return True
  
      def generate_reset_token(self, expiration=3600):
          s = Serializer(current_app.config['SECRET_KEY'], expiration)
          return s.dumps({'reset': self.id})
  
      def reset_password(self, token, new_password):
          s = Serializer(current_app.config['SECRET_KEY'])
          try:
              data = s.loads(token)
          except:
              return False
          if data.get('reset') != self.id:
              return False
          self.password = new_password
          db.session.add(self)
          return True
  
      def generate_email_change_token(self, new_email, expiration=3600):
          s = Serializer(current_app.config['SECRET_KEY'], expiration)
          return s.dumps({'change_email': self.id, 'new_email': new_email})
  
      def change_email(self, token):
          s = Serializer(current_app.config['SECRET_KEY'])
          try:
              data = s.loads(token)
          except:
              return False
          if data.get('change_email') != self.id:
              return False
          new_email = data.get('new_email')
          if new_email is None:
              return False
          if self.query.filter_by(email=new_email).first() is not None:
              return False
          self.email = new_email
          self.avatar_hash = hashlib.md5(
              self.email.encode('utf-8')).hexdigest()
          db.session.add(self)
          return True
  
      def gravatar(self, size=100, default='identicon', rating='g'):
          if request.is_secure:
              url = 'https://secure.gravatar.com/avatar'
          else:
              url = 'http://www.gravatar.com/avatar'
          hash = self.avatar_hash or hashlib.md5(
              self.email.encode('utf-8')).hexdigest()
          return '{url}/{hash}?s={size}&d={default}&r={rating}'.format(
              url=url, hash=hash, size=size, default=default, rating=rating)
  
      def __repr__(self):
          return '<User %r>' % self.username
  
  
  @login_manager.user_loader
  def load_user(user_id):
      return User.query.get(int(user_id))
  