Exemple #1
0
 def init(cls, choices, selected, node=None, **kwargs):
     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='节点描述')
     cls.style = TextAreaField('样式', [
         validators.Length(min=0, max=1000),
     ],
                               description='节点样式')
     nf = cls(kwargs)
     nf.parent_name.data = selected
     nf.node = node
     return nf
Exemple #2
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()
Exemple #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
Exemple #4
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)
Exemple #5
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
Exemple #6
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