text: import hashlib
  from datetime import datetime
  
  from flask import current_app
  from flask import request
  from flask_login import AnonymousUserMixin
  from flask_login import UserMixin
  from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
  from werkzeug.security import check_password_hash
  from werkzeug.security import generate_password_hash
  
  from flasky import db
  from .permission import Permission
  from .role import Role
  
  
  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)
      role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
      password_hash = db.Column(db.String(128))
      confirmed = db.Column(db.Boolean, default=False)
      name = db.Column(db.String(64))
      location = db.Column(db.String(64))
      about_me = db.Column(db.Text)
      member_since = db.Column(db.DateTime, default=datetime.utcnow)
      last_seen = db.Column(db.DateTime, default=datetime.utcnow)
      avatar_hash = db.Column(db.String(32))
  
      def __init__(self, **kwargs):
          super(User, self).__init__(**kwargs)
          if self.email == current_app.config['FLASKY_ADMIN']:
              self.role = Role.query.filter_by(permissions=Permission.ALL).first()
          else:
              self.role = Role.query.filter_by(default=True).first()
  
          if self.email is not None and self.avatar_hash is None:
              self.avatar_hash = hashlib.md5(self.email.encode('utf-8')).hexdigest()
  
      @property
      def password(self):
          raise AttributeError("Password is not 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'], expires_in=expiration)
          return s.dumps(dict(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 can(self, permissions):
          return self.role is not None \
                 and (self.role.permissions & permissions) == permissions
  
      def is_administrator(self):
          return self.can(Permission.ADMINISTER)
  
      def ping(self):
          self.last_seen = datetime.utcnow()
          db.session.add(self)
  
      def gravatar(self, size=100, default='identicon', rating='g'):
          if request.is_secure:
              url = 'https://secure.gravatar.com/avatar'
          else:
              url = 'http://en.gravatar.com/avatar'
  
          hash_value = 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_value, size=size,
                                                                       default=default,
                                                                       rating=rating)
  
      def change_email(self, email):
          self.email = email
          self.avatar_hash = hashlib.md5(email.encode('utf-8')).hexdigest()
          db.session.add(self)
          return True
  
      def __repr__(self):
          return '<User {0}>'.format(self.username)
  
  
  class AnonymousUser(AnonymousUserMixin):
      def can(self, permissions):
          return False
  
      def is_administrator(self):
          return False
  