from flask_login import current_user
from flask_wtf import FlaskForm
from flask_ckeditor import CKEditorField
from wtforms import StringField, SubmitField, ValidationError, SelectField, TextAreaField, HiddenField
from wtforms.validators import DataRequired, Length, Email, Optional, URL

from sites.models.blog import Category


class CategoryForm(FlaskForm):
    name = StringField('名称', validators=[DataRequired(), Length(1,30)])
    submit = SubmitField()

    def validate_name(self, field):
        #current_user.category.query.filter_by(name=field.data)这样也行吧
        if Category.query.filter_by(name=field.data, author_id=current_user.id).first():
            raise ValidationError('类别已存在')



class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(1,60)])
    category = SelectField('分类', coerce=int, default=1)
    body = CKEditorField('主体', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args,**kwargs)
        self.category.choices = [(category.id, category.name) for category in Category.query.order_by(Category.name).all()]


# class CommentForm(FlaskForm):
#     author = StringField('名字', validators=[DataRequired(), Length(1,30)])
#     email = StringField('邮箱', validators=[DataRequired(), Email(), Length(1,254)])
#     site = StringField('站点', validators=[Optional(), URL(), Length(0,25)])
#     body = TextAreaField('评论', validators=[DataRequired()])
#
# class AdminCommentForm(CommentForm):
#     author = HiddenField()
#     email = HiddenField()
#     site = HiddenField()
122360b75a150f1df3a9d21020b08595a26df817
<mask token>


class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(1, 60)])
    category = SelectField('分类', coerce=int, default=1)
    body = CKEditorField('主体', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.category.choices = [(category.id, category.name) for category in
            Category.query.order_by(Category.name).all()]

<mask token>


class CategoryForm(FlaskForm):
    <mask token>
    <mask token>

    def validate_name(self, field):
        if Category.query.filter_by(name=field.data, author_id=current_user.id
            ).first():
            raise ValidationError('类别已存在')


class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(1, 60)])
    category = SelectField('分类', coerce=int, default=1)
    body = CKEditorField('主体', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.category.choices = [(category.id, category.name) for category in
            Category.query.order_by(Category.name).all()]

<mask token>


class CategoryForm(FlaskForm):
    name = StringField('名称', validators=[DataRequired(), Length(1, 30)])
    submit = SubmitField()

    def validate_name(self, field):
        if Category.query.filter_by(name=field.data, author_id=current_user.id
            ).first():
            raise ValidationError('类别已存在')


class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(1, 60)])
    category = SelectField('分类', coerce=int, default=1)
    body = CKEditorField('主体', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.category.choices = [(category.id, category.name) for category in
            Category.query.order_by(Category.name).all()]

from flask_login import current_user
from flask_wtf import FlaskForm
from flask_ckeditor import CKEditorField
from wtforms import StringField, SubmitField, ValidationError, SelectField, TextAreaField, HiddenField
from wtforms.validators import DataRequired, Length, Email, Optional, URL
from sites.models.blog import Category


class CategoryForm(FlaskForm):
    name = StringField('名称', validators=[DataRequired(), Length(1, 30)])
    submit = SubmitField()

    def validate_name(self, field):
        if Category.query.filter_by(name=field.data, author_id=current_user.id
            ).first():
            raise ValidationError('类别已存在')


class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(1, 60)])
    category = SelectField('分类', coerce=int, default=1)
    body = CKEditorField('主体', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.category.choices = [(category.id, category.name) for category in
            Category.query.order_by(Category.name).all()]

from flask_login import current_user
from flask_wtf import FlaskForm
from flask_ckeditor import CKEditorField
from wtforms import StringField, SubmitField, ValidationError, SelectField, TextAreaField, HiddenField
from wtforms.validators import DataRequired, Length, Email, Optional, URL

from sites.models.blog import Category


class CategoryForm(FlaskForm):
    name = StringField('名称', validators=[DataRequired(), Length(1,30)])
    submit = SubmitField()

    def validate_name(self, field):
        #current_user.category.query.filter_by(name=field.data)这样也行吧
        if Category.query.filter_by(name=field.data, author_id=current_user.id).first():
            raise ValidationError('类别已存在')



class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(1,60)])
    category = SelectField('分类', coerce=int, default=1)
    body = CKEditorField('主体', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args,**kwargs)
        self.category.choices = [(category.id, category.name) for category in Category.query.order_by(Category.name).all()]


# class CommentForm(FlaskForm):
#     author = StringField('名字', validators=[DataRequired(), Length(1,30)])
#     email = StringField('邮箱', validators=[DataRequired(), Email(), Length(1,254)])
#     site = StringField('站点', validators=[Optional(), URL(), Length(0,25)])
#     body = TextAreaField('评论', validators=[DataRequired()])
#
# class AdminCommentForm(CommentForm):
#     author = HiddenField()
#     email = HiddenField()
#     site = HiddenField()