Example #1
0
class SigninForm(BaseForm):
    account = TextField(
        '邮箱',
        [
            validators.Required(),
            validators.Length(min=4, max=30),
        ],
    )
    password = PasswordField('密码', [
        validators.Required(),
        validators.Length(min=6, max=24),
    ])
    # permanent = BooleanField('记住我')

    @orm.db_session
    def validate_password(self, field):
        account = self.account.data
        if '@' in account:
            user = User.get(email=account)
        else:
            user = User.get(name=account)

        if not user:
            raise ValidationError('用户名或密码错误')
        if user.check_password(field.data):
            self.user = user
            return user
        raise ValidationError('用户名或密码错误')
Example #2
0
 def init(cls, choices, selected):
     cls.parent_name = SelectMultipleField(
         '父节点', [
         ],
         choices=choices,
     )
     cls.name = TextField(
         '节点名', [
             validators.Required(),
             validators.Length(min=1, max=16),
         ],
     )
     cls.urlname = TextField(
         '节点地址', [
             validators.Required(),
             validators.Length(min=2, max=32),
             validators.Regexp(
                 '^[a-zA-Z0-9_]+$',
                 message='节点地址只能包含英文字母和数字',
             ),
         ],
         description='节点地址只能包含英文字母和数字'
     )
     cls.description = TextAreaField(
         '描述', [
             validators.Length(min=0, max=3000),
         ],
         description='节点描述'
     )
     nf = cls()
     nf.parent_name.data = selected
     return nf
Example #3
0
 def init(cls, choices, selected, **kwargs):
     cls.node_name = SelectField(
         '节点',
         [
             validators.Required(),
         ],
         choices=choices,
     )
     cls.title = TextField(
         '标题',
         [
             validators.Required(),
             validators.Length(min=4, max=100),
         ],
     )
     cls.content = TextAreaField(
         '内容',
         [
             validators.Required(),
             validators.Length(min=3, max=1000000),
         ],
     )
     tf = cls(kwargs)
     tf.node_name.data = selected
     return tf
Example #4
0
class SignupForm(BaseForm):
    name = TextField('用户名', [
        validators.Required(),
        validators.Length(min=4, max=16),
        validators.Regexp(
            '^[a-zA-Z0-9]+$',
            message='用户名只能包含英文字母和数字',
        ),
    ],
                     description='用户名只能包含英文字母和数字')
    email = TextField('邮箱', [
        validators.Required(),
        validators.Length(min=4, max=30),
        validators.Email(),
    ],
                      description='邮箱用于管理帐户')
    password = PasswordField('密码', [
        validators.Required(),
        validators.Length(min=6, max=24),
    ],
                             description='密码最少 6 字节')
    password2 = PasswordField(
        '密码确认',
        [
            validators.Required(),
            validators.Length(min=6, max=24),
        ],
    )

    @orm.db_session
    def validate_name(self, field):
        data = field.data.lower()
        if data in config.forbidden_name_list or User.get(name=data):
            raise ValidationError('此用户名已注册')

    @orm.db_session
    def validate_email(self, field):
        data = field.data.lower()
        if User.get(email=data):
            raise ValidationError('此邮箱已注册')

    def validate_password(self, field):
        if field.data != self.password2.data:
            raise ValidationError('密码不匹配')

    def save(self, role=None):
        data = self.data
        data.pop('password2')
        user = User.init(**data)
        if role:
            user.role = role
        user.save()
        return user
Example #5
0
class AlbumForm(BaseForm):
    name = TextAreaField(
        '名字',
        [
            validators.Required(),
            validators.Length(min=1, max=10),
        ],
    )

    @property
    def result(self):
        msg = u'专辑创建失败'
        for field in self:
            if field.errors:
                if field.name == 'content':
                    msg = u'名字至少 3 字节'
        result = {'status': 'error', 'message': msg}
        return result

    def save(self, user):
        data = self.data
        name = unicode(data.get('name'))
        data.update({'user_id': user.id, 'name': strip_xss_tags(name)})
        album = Album(**data)
        return album.save()
Example #6
0
class ReplyForm(BaseForm):
    content = TextAreaField(
        '内容',
        [
            validators.Required(),
            validators.Length(min=3, max=1000000),
        ],
    )

    def validate_content(self, field):
        """ 为了照顾一图流
        """
        if field.data.find('<img class="upload-reply-image"') == -1 and\
           field.data.find('<embed type="application') == -1:
            data = strip_tags(field.data)
            if len(data) < 3:
                raise ValidationError('内容至少 3 字符')

    @property
    def result(self):
        msg = '评论创建失败'
        for field in self:
            if field.errors:
                if field.name == 'content':
                    msg = '内容至少 3 字节'
        result = {'status': 'error', 'message': msg}
        return result

    def save(self, user, topic, reply=None):
        data = self.data
        content = str(data.get('content'))
        data.update({
            'user_id': user.id,
            'topic_id': topic.id,
            'content': strip_xss_tags(content)
        })
        if reply:
            category = 'edit'
            pre_content = reply.content
            cur_content = data.get('content')
            changed = 0
            if pre_content != cur_content:
                diff_content = ghdiff.diff(pre_content, cur_content, css=None)
                changed = 1
            if changed == 1:
                reply.content = cur_content
                History(user_id=user.id,
                        content=diff_content,
                        reply_id=reply.id).save()
            else:
                return reply
        else:
            category = 'create'
            reply = Reply(**data)
        return reply.save(category=category)
Example #7
0
class MessageForm(BaseForm):
    content = TextAreaField(
        '内容',
        [
            validators.Required(),
            validators.Length(min=2, max=2000),
        ],
    )

    def save(self, **kargs):
        data = self.data
        data.update(kargs)
        message = Message(**data).save()
        return message
Example #8
0
    def init(cls, user=None, **kwargs):
        cls.nickname = TextField('昵称', [
            validators.Required(),
            validators.Length(min=4, max=16),
        ],
                                 description='您还有 %s 次修改昵称的机会' %
                                 user.edit_nickname_count)
        cls.urlname = TextField('域名', [
            validators.Required(),
            validators.Length(min=4, max=30),
            validators.Regexp(
                '^[a-zA-Z0-9_]+$',
                message='域名只能包含英文字母和数字',
            ),
        ],
                                description='您还有 %s 次修改域名的机会' %
                                user.edit_urlname_count)
        cls.address = TextField(
            '城市',
            [
                validators.Length(min=0, max=200),
            ],
        )
        cls.website = TextField(
            '网址',
            [
                validators.Length(min=0, max=200),
            ],
        )
        cls.description = TextAreaField(
            '简介',
            [
                validators.Length(min=0, max=10000),
            ],
        )
        cls.style = TextAreaField(
            '样式',
            [
                validators.Length(min=0, max=1000),
            ],
        )
        cls.site_style = TextAreaField(
            '全站样式',
            [
                validators.Length(min=0, max=1000),
            ],
        )

        if not kwargs and user:
            kwargs = {
                'nickname': [user.nickname],
                'urlname': [user.urlname],
                'address': [user.address],
                'website': [user.website],
                'description': [user.description],
                'style': [user.style],
                'site_style': [user.site_style]
            }

        sf = cls(kwargs)
        if user:
            sf.edit_nickname_count = user.edit_nickname_count
            sf.edit_urlname_count = user.edit_urlname_count
            sf.user = user
        return sf