# Path: app/models.py
# class Tag(db.Model):
#     __tablename__ = 'tag'
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(64), nullable=False)
#     description = db.Column(db.String(256))
#     created_time = db.Column(db.DateTime(), default=datetime.utcnow)
#     is_valid = db.Column(db.Boolean, default=False)
#     creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))   #在标签的详情页会有展示，以鼓励活跃的内容贡献者
#     posts = db.relationship(
#         "Post", lazy='dynamic',
#         secondary=post_tag,
#         back_populates="tags")
#     users = db.relationship(
#         "User", lazy='dynamic',
#         secondary=user_tag,
#         back_populates="tags")


from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, BooleanField, SubmitField, HiddenField
from wtforms.fields.html5 import DateField
from wtforms.validators import Optional, Length, URL, Email, DataRequired
from flask_pagedown.fields import PageDownField
from app.models import Tag
class ProfileForm(FlaskForm):
    name = StringField(u'昵称', validators=[Optional(), Length(1, 64)])
    location = StringField(u'位置', validators=[Optional(), Length(1, 64)])
    bio = TextAreaField(u'简介')
    submit = SubmitField(u'提交')


class PostForm(FlaskForm):
    title = StringField(u'标题', validators=[DataRequired(), Length(1, 128)])
    description = TextAreaField(u'内容')
    # slides = StringField('Slides Embed Code (450 pixels wide)')
    # video = StringField('Video Embed Code (450 pixels wide)')
    tags = StringField(u'标签',
                        validators=[Length(1, 128)], description=u"多个标签请用空格分开")
    # venue_url = StringField(u'地点链接',
    #                         validators=[Optional(), Length(1, 128), URL()])
    # date = DateField(u'日期')
    submit_button = SubmitField(u'提交', id='submit_post')


    def from_model(self, post):
        self.title.data = post.title
        self.description.data = post.description


    def to_model(self, post):
        post.title = self.title.data
        post.description = self.description.data
        tag_list = self.tags.data.split(" ")
        for t in tag_list:

# Path: app/models.py
# class Tag(db.Model):
#     __tablename__ = 'tag'
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(64), nullable=False)
#     description = db.Column(db.String(256))
#     created_time = db.Column(db.DateTime(), default=datetime.utcnow)
#     is_valid = db.Column(db.Boolean, default=False)
#     creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))   #在标签的详情页会有展示，以鼓励活跃的内容贡献者
#     posts = db.relationship(
#         "Post", lazy='dynamic',
#         secondary=post_tag,
#         back_populates="tags")
#     users = db.relationship(
#         "User", lazy='dynamic',
#         secondary=user_tag,
#         back_populates="tags")


# Path: app/posts/forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, BooleanField, SubmitField, HiddenField
from wtforms.fields.html5 import DateField
from wtforms.validators import Optional, Length, URL, Email, DataRequired
from flask_pagedown.fields import PageDownField
from app.models import Tag

class ProfileForm(FlaskForm):
    name = StringField(u'昵称', validators=[Optional(), Length(1, 64)])
    location = StringField(u'位置', validators=[Optional(), Length(1, 64)])
    bio = TextAreaField(u'简介')
    submit = SubmitField(u'提交')


class PostForm(FlaskForm):
    title = StringField(u'标题', validators=[DataRequired(), Length(1, 128)])
    description = TextAreaField(u'内容')
    # slides = StringField('Slides Embed Code (450 pixels wide)')
    # video = StringField('Video Embed Code (450 pixels wide)')
    tags = StringField(u'标签',
                        validators=[Length(1, 128)], description=u"多个标签请用空格分开")
    # venue_url = StringField(u'地点链接',
    #                         validators=[Optional(), Length(1, 128), URL()])
    # date = DateField(u'日期')
    submit_button = SubmitField(u'提交', id='submit_post')


    def from_model(self, post):
        self.title.data = post.title
        self.description.data = post.description


    def to_model(self, post):
        post.title = self.title.data
        post.description = self.description.data
        tag_list = self.tags.data.split(" ")
        for t in tag_list:

            tag = Tag.query.filter_by(name=t).first()    #first方法在没有结果时返回None, 这一点需要注意