# 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 import session
from flask.ext.login import current_user
from flask_wtf import Form
from wtforms import StringField
from wtforms import ValidationError
from wtforms.validators import Email
from wtforms.validators import InputRequired
from wtforms.validators import Regexp
from daimaduan.models.base import User
# coding: utf-8



class UserInfoForm(Form):
    username = StringField(u'昵称', validators=[
        InputRequired(), Regexp(r'^[a-zA-Z0-9-_]{3,12}$', message=u'3到12个字符，不能包含空格')])
    email = StringField(u'邮箱地址', validators=[InputRequired(), Email()])
    description = StringField(u'个性签名', validators=[
        InputRequired(), Regexp(r'^.{1,20}$', message=u'1到20个字符')])

    def validate_username(self, field):
        if current_user.is_authenticated and current_user.user.username == field.data:
            return True

        user = User.objects(username=field.data).first()
        if user:
            raise ValidationError(u'用户名已被使用')

    def validate_email(self, field):
        if current_user.is_authenticated:
            field.data = current_user.user.email
            return True

        if session['email']:
            if session['email'] != field.data:
                raise ValidationError(u'不能修改第三方登录的email地址')

# 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/userinfo.py
from flask import session
from flask.ext.login import current_user
from flask_wtf import Form
from wtforms import StringField
from wtforms import ValidationError
from wtforms.validators import Email
from wtforms.validators import InputRequired
from wtforms.validators import Regexp
from daimaduan.models.base import User

# coding: utf-8



class UserInfoForm(Form):
    username = StringField(u'昵称', validators=[
        InputRequired(), Regexp(r'^[a-zA-Z0-9-_]{3,12}$', message=u'3到12个字符，不能包含空格')])
    email = StringField(u'邮箱地址', validators=[InputRequired(), Email()])
    description = StringField(u'个性签名', validators=[
        InputRequired(), Regexp(r'^.{1,20}$', message=u'1到20个字符')])

    def validate_username(self, field):
        if current_user.is_authenticated and current_user.user.username == field.data:
            return True

        user = User.objects(username=field.data).first()
        if user:
            raise ValidationError(u'用户名已被使用')

    def validate_email(self, field):
        if current_user.is_authenticated:
            field.data = current_user.user.email
            return True

        if session['email']:
            if session['email'] != field.data:
                raise ValidationError(u'不能修改第三方登录的email地址')

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