示例#1
0
class User(UserMixin, db.Document):
    username = db.StringField(max_length=255, required=True)
    email = db.EmailField(max_length=255)
    password_hash = db.StringField(required=True)
    create_time = db.DateTimeField(default=datetime.datetime.now, required=True)
    last_login = db.DateTimeField(default=datetime.datetime.now, required=True)
    is_email_confirmed = db.BooleanField(default=False)
    # is_active = db.BooleanField(default=True)
    is_superuser = db.BooleanField(default=False)
    role = db.StringField(max_length=32, default='reader', choices=ROLES)
    display_name = db.StringField(max_length=255, default='Anonymous')
    biography = db.StringField()
    social_networks = db.DictField(default=SOCIAL_NETWORKS)
    homepage_url = db.URLField()

    @property
    def password(self):
        raise AttributeError('password is not a readle 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 get_id(self):
        try:
            return unicode(self.username)
        except AttributeError:
            raise NotImplementedError('No `username` attribute - override `get_id`')

    def __unicode__(self):
        return self.username
示例#2
0
class User(UserMixin, db.Document):
    username = db.StringField(max_length=255, required=True)
    email = db.EmailField(max_length=255)
    password_hash = db.StringField(required=True)
    create_time = db.DateTimeField(default=datetime.datetime.now,
                                   required=True)
    last_login = db.DateTimeField(default=datetime.datetime.now, required=True)
    is_email_confirmed = db.BooleanField(default=False)
    # is_active = db.BooleanField(default=True)
    is_superuser = db.BooleanField(default=False)
    role = db.StringField(max_length=32, default='reader', choices=ROLES)
    display_name = db.StringField(max_length=255, default=username)
    biography = db.StringField()
    social_networks = db.DictField(default=SOCIAL_NETWORKS)
    homepage_url = db.URLField()

    confirm_email_sent_time = db.DateTimeField()

    @property
    def password(self):
        raise AttributeError('password is not a readle 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):
        serializer = TimedJSONWebSignatureSerializer(
            current_app.config['SECRET_KEY'], expiration)
        return serializer.dumps({'confirm': self.username})

    def confirm_email(self, token, expiration=3600):
        s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except Exception:
            return False
        if data.get('confirm') != self.username:
            return False
        self.is_email_confirmed = True
        self.save()
        return True

    def generate_reset_token(self, expiration=3600):
        serializer = TimedJSONWebSignatureSerializer(
            current_app.config['SECRET_KEY'], expiration)
        return serializer.dumps({'reset': self.username})

    @staticmethod
    def reset_password(token, new_password):
        serializer = TimedJSONWebSignatureSerializer(
            current_app.config['SECRET_KEY'])
        try:
            data = serializer.loads(token)
        except:
            return False

        try:
            user = User.objects.get(username=data.get('reset'))
        except Exception:
            return False

        user.password = new_password
        user.save()
        return True

    def get_id(self):
        try:
            # return unicode(self.username)
            return self.username

        except AttributeError:
            raise NotImplementedError(
                'No `username` attribute - override `get_id`')

    def __unicode__(self):
        return self.username