# Path: daimaduan/models/base.py
# class User(BaseDocument):
#     username = db.StringField(required=True)
#     email = db.StringField(required=True)
#     password = db.StringField()
#     salt = db.StringField()
#     is_email_confirmed = db.BooleanField(default=False)
#     email_confirmed_on = db.DateTimeField(default=None)
#     number = db.IntField(default=0, unique=True)
#     description = db.StringField(default=u"这个家伙很懒, TA什么都没写...")
# 
#     oauths = db.ListField(db.ReferenceField(UserOauth))
# 
#     likes = db.ListField(db.ReferenceField('Paste'))
#     followings = db.ListField(db.ReferenceField('User'))
# 
#     @property
#     def pastes(self):
#         return Paste.objects(user=self)
# 
#     @property
#     def pastes_count(self):
#         return len(self.pastes)
# 
#     @property
#     def comments_count(self):
#         return len(Comment.objects(user=self))
# 
#     @property
#     def private_pastes_count(self):
#         return len(self.pastes(is_private=True))
# 
#     @property
#     def public_pastes_count(self):
#         return len(self.pastes) - len(self.pastes(is_private=True))
# 
#     @property
#     def public_bookmarks(self):
#         return Bookmark.objects(user=self, is_private=False)
# 
#     @property
#     def public_bookmarks_count(self):
#         return Bookmark.objects(user=self, is_private=False).count()
# 
#     @property
#     def unread_messages_count(self):
#         return Message.objects(user=self, is_read=False).count()
# 
#     def save(self, *args, **kwargs):
#         if not self.salt:
#             self.salt = hashlib.sha1(str(time.time())).hexdigest()
#             self.password = self.generate_password(self.password)
#             number = User.objects().count() + 1
#             while(User.objects(number=number).first()):
#                 number += 1
#             self.number = number
#         super(User, self).save(*args, **kwargs)
# 
#     def owns_record(self, record):
#         return record.user == self
# 
#     def generate_password(self, string):
#         return hashlib.sha1('%s%s' % (string, self.salt)).hexdigest()
# 
#     def check_login(self, password):
#         return self.generate_password(password) == self.password
# 
#     @classmethod
#     def find_by_oauth(cls, provider, openid):
#         """Find user that has oauth info with given provider and openid"""
#         oauth = UserOauth.objects(provider=provider, openid=openid).first()
# 
#         if oauth and oauth.user:
#             return oauth.user
# 
#     def is_following(self, user):
#         return user in self.followings
# 
#     @property
#     def followers(self):
#         return User.objects(followings=self)


from flask_wtf import Form
from wtforms import StringField
from wtforms import PasswordField
from wtforms import ValidationError
from wtforms.validators import InputRequired
from wtforms.validators import Email
from wtforms.validators import Regexp
from wtforms.validators import EqualTo
from daimaduan.models.base import User
# coding: utf-8


class SignupForm(Form):
    username = StringField(u'昵称', validators=[
        InputRequired(), Regexp(r'^[a-zA-Z0-9-_]{3,12}$', message=u'3到12个字符，不能包含空格')])
    email = StringField(u'邮箱地址', validators=[InputRequired(), Email()])
    password = PasswordField(u'密码', validators=[InputRequired()])
    password_confirm = PasswordField(u'密码确认', validators=[EqualTo('password', message=u'两次密码不同')])

    def validate_username(self, field):
        user = User.objects(username=field.data).first()
        if user is not None:
            raise ValidationError(u'用户名已被使用')

    def validate_email(self, field):

# Path: daimaduan/models/base.py
# class User(BaseDocument):
#     username = db.StringField(required=True)
#     email = db.StringField(required=True)
#     password = db.StringField()
#     salt = db.StringField()
#     is_email_confirmed = db.BooleanField(default=False)
#     email_confirmed_on = db.DateTimeField(default=None)
#     number = db.IntField(default=0, unique=True)
#     description = db.StringField(default=u"这个家伙很懒, TA什么都没写...")
# 
#     oauths = db.ListField(db.ReferenceField(UserOauth))
# 
#     likes = db.ListField(db.ReferenceField('Paste'))
#     followings = db.ListField(db.ReferenceField('User'))
# 
#     @property
#     def pastes(self):
#         return Paste.objects(user=self)
# 
#     @property
#     def pastes_count(self):
#         return len(self.pastes)
# 
#     @property
#     def comments_count(self):
#         return len(Comment.objects(user=self))
# 
#     @property
#     def private_pastes_count(self):
#         return len(self.pastes(is_private=True))
# 
#     @property
#     def public_pastes_count(self):
#         return len(self.pastes) - len(self.pastes(is_private=True))
# 
#     @property
#     def public_bookmarks(self):
#         return Bookmark.objects(user=self, is_private=False)
# 
#     @property
#     def public_bookmarks_count(self):
#         return Bookmark.objects(user=self, is_private=False).count()
# 
#     @property
#     def unread_messages_count(self):
#         return Message.objects(user=self, is_read=False).count()
# 
#     def save(self, *args, **kwargs):
#         if not self.salt:
#             self.salt = hashlib.sha1(str(time.time())).hexdigest()
#             self.password = self.generate_password(self.password)
#             number = User.objects().count() + 1
#             while(User.objects(number=number).first()):
#                 number += 1
#             self.number = number
#         super(User, self).save(*args, **kwargs)
# 
#     def owns_record(self, record):
#         return record.user == self
# 
#     def generate_password(self, string):
#         return hashlib.sha1('%s%s' % (string, self.salt)).hexdigest()
# 
#     def check_login(self, password):
#         return self.generate_password(password) == self.password
# 
#     @classmethod
#     def find_by_oauth(cls, provider, openid):
#         """Find user that has oauth info with given provider and openid"""
#         oauth = UserOauth.objects(provider=provider, openid=openid).first()
# 
#         if oauth and oauth.user:
#             return oauth.user
# 
#     def is_following(self, user):
#         return user in self.followings
# 
#     @property
#     def followers(self):
#         return User.objects(followings=self)


# Path: daimaduan/forms/signup.py
from flask_wtf import Form
from wtforms import StringField
from wtforms import PasswordField
from wtforms import ValidationError
from wtforms.validators import InputRequired
from wtforms.validators import Email
from wtforms.validators import Regexp
from wtforms.validators import EqualTo
from daimaduan.models.base import User

# coding: utf-8


class SignupForm(Form):
    username = StringField(u'昵称', validators=[
        InputRequired(), Regexp(r'^[a-zA-Z0-9-_]{3,12}$', message=u'3到12个字符，不能包含空格')])
    email = StringField(u'邮箱地址', validators=[InputRequired(), Email()])
    password = PasswordField(u'密码', validators=[InputRequired()])
    password_confirm = PasswordField(u'密码确认', validators=[EqualTo('password', message=u'两次密码不同')])

    def validate_username(self, field):
        user = User.objects(username=field.data).first()
        if user is not None:
            raise ValidationError(u'用户名已被使用')

    def validate_email(self, field):

        user = User.objects(email=field.data).first()