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
class Comment(db.Document): author = db.StringField(required=True) email = db.EmailField(max_length=255) homepage = db.URLField() # post = db.ReferenceField(Post) post_slug = db.StringField(required=True) post_title = db.StringField(default='default article') md_content = db.StringField() html_content = db.StringField() pub_time = db.DateTimeField() update_time = db.DateTimeField() replay_to = db.ReferenceField('self') status = db.StringField(choices=COMMENT_STATUS, default='pending') misc = db.StringField() # If the comment is imported, this field will store something useful gavatar_id = db.StringField(default='00000000000') def reset_gavatar_id(self): if not self.email: self.gavatar_id = '00000000000' return self.gavatar_id = hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() def save(self, *args, **kwargs): if self.md_content: html_content = markdown2.markdown(self.md_content, extras=['code-friendly', 'fenced-code-blocks', 'tables', 'nofollow']).encode('utf-8') self.html_content = get_clean_html_content(html_content) if not self.pub_time: self.pub_time = datetime.datetime.now() self.update_time = datetime.datetime.now() if self.gavatar_id=='00000000000': self.reset_gavatar_id() return super(Comment, self).save(*args, **kwargs) def get_gavatar_url(self, base_url=GAVATAR_CDN_BASE, img_size=0, default_image_url=None): gavatar_url = base_url + self.gavatar_id params = {} if img_size: params['s'] = str(img_size) if default_image_url: params['d'] = default_image_url if params: gavatar_url = '{0}?{1}'.format(gavatar_url, urllib.urlencode(params)) return gavatar_url def __unicode__(self): return self.md_content[:64] meta = { 'ordering': ['-update_time'] }
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