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
class AblumForm(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()
def init(choices, selected, args=None): node_name = SelectField( '节点', [ validators.Required(), ], choices=choices, ) title = TextField( '标题', [ validators.Required(), validators.Length(min=4, max=100), ], ) content = TextAreaField( '内容', [ validators.Required(), ], ) TopicForm.node_name = node_name TopicForm.title = title TopicForm.content = content if args: tf = TopicForm(args) else: tf = TopicForm() tf.node_name.data = selected return tf
def init(choices, selected): NodeForm.parent_name = SelectMultipleField( '父节点', [], choices=choices, ) NodeForm.name = TextField( '节点名', [ validators.Required(), validators.Length(min=1, max=16), ], ) NodeForm.urlname = TextField('节点地址', [ validators.Required(), validators.Length(min=2, max=32), validators.Regexp( '^[a-zA-Z0-9_]+$', message='节点地址只能包含英文字母和数字', ), ], description='节点地址只能包含英文字母和数字') NodeForm.description = TextAreaField('描述', [ validators.Length(min=0, max=3000), ], description='节点描述') nf = NodeForm() nf.parent_name.data = selected return nf
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): result = {'status': 'error', 'message': '评论创建失败'} for field in self: if field.errors: for error in field.errors: if field.name == 'content': prev = '内容至少 3 字节' result = {'status': 'error', 'message': prev} return result def save(self, user, topic, reply=None): data = self.data content = unicode(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)
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
def init(choices, selected, args=None, node=None): NodeEditForm.parent_name = SelectMultipleField( '父节点', [], choices=choices, ) NodeEditForm.name = TextField( '节点名', [ validators.Required(), validators.Length(min=1, max=16), ], ) NodeEditForm.urlname = TextField('节点地址', [ validators.Required(), validators.Length(min=2, max=32), validators.Regexp( '^[a-zA-Z0-9_]+$', message='节点地址只能包含英文字母和数字', ), ], description='节点地址只能包含英文字母和数字') NodeEditForm.description = TextAreaField('描述', [ validators.Length(min=0, max=3000), ], description='节点描述') NodeEditForm.style = TextAreaField('样式', [ validators.Length(min=0, max=1000), ], description='节点样式') if args: nf = NodeEditForm(args) else: nf = NodeEditForm() nf.parent_name.data = selected nf.node = node return nf
class MessageForm(BaseForm): content = TextAreaField( '内容', [ validators.Required(), validators.Length(min=2, max=2000), ], ) @db_session def save(self, user_id, message_box1_id, message_box2_id): data = self.data data.update({ 'user_id': user_id, 'message_box1_id': message_box1_id, 'message_box2_id': message_box2_id }) message = Message(**data).save() return message
def init(user=None, args=None): nickname = TextField('昵称', [ validators.Required(), validators.Length(min=4, max=16), ], description='您还有 %s 次修改昵称的机会' % user.edit_nickname_count) urlname = TextField('域名', [ validators.Required(), validators.Length(min=4, max=30), validators.Regexp( '^[a-zA-Z0-9_]+$', message='域名只能包含英文字母和数字', ), ], description='您还有 %s 次修改域名的机会' % user.edit_urlname_count) address = TextField( '城市', [ validators.Length(min=0, max=200), ], ) website = TextField( '网址', [ validators.Length(min=0, max=200), ], ) description = TextAreaField( '简介', [ validators.Length(min=0, max=10000), ], ) style = TextAreaField( '样式', [ validators.Length(min=0, max=1000), ], ) SettingForm.nickname = nickname SettingForm.urlname = urlname SettingForm.address = address SettingForm.website = website SettingForm.description = description SettingForm.style = style if not args: if user: args = { 'nickname': [user.nickname], 'urlname': [user.urlname], 'address': [user.address], 'website': [user.website], 'description': [user.description], 'style': [user.style] } sf = SettingForm(args) sf.edit_nickname_count = user.edit_nickname_count sf.edit_urlname_count = user.edit_urlname_count sf.user = user else: sf = SettingForm() else: if user: sf = SettingForm(args) sf.edit_nickname_count = user.edit_nickname_count sf.edit_urlname_count = user.edit_urlname_count sf.user = user else: sf = SettingForm(args) return sf
class SettingForm(BaseForm): nickname = TextField( '昵称', [ validators.Required(), validators.Length(min=4, max=16), ], ) urlname = TextField('域名', [ validators.Required(), validators.Length(min=4, max=30), validators.Regexp( '^[a-zA-Z0-9_]+$', message='域名只能包含英文字母和数字', ), ], description='让您的域名具有个性') address = TextField( '城市', [ validators.Length(min=0, max=200), ], ) website = TextField( '网址', [ validators.Length(min=1, max=200), ], ) description = TextAreaField( '简介', [ validators.Length(min=1, max=10000), ], ) style = TextAreaField( '样式', [ validators.Length(min=0, max=1000), ], ) @staticmethod def init(user=None, args=None): nickname = TextField('昵称', [ validators.Required(), validators.Length(min=4, max=16), ], description='您还有 %s 次修改昵称的机会' % user.edit_nickname_count) urlname = TextField('域名', [ validators.Required(), validators.Length(min=4, max=30), validators.Regexp( '^[a-zA-Z0-9_]+$', message='域名只能包含英文字母和数字', ), ], description='您还有 %s 次修改域名的机会' % user.edit_urlname_count) address = TextField( '城市', [ validators.Length(min=0, max=200), ], ) website = TextField( '网址', [ validators.Length(min=0, max=200), ], ) description = TextAreaField( '简介', [ validators.Length(min=0, max=10000), ], ) style = TextAreaField( '样式', [ validators.Length(min=0, max=1000), ], ) SettingForm.nickname = nickname SettingForm.urlname = urlname SettingForm.address = address SettingForm.website = website SettingForm.description = description SettingForm.style = style if not args: if user: args = { 'nickname': [user.nickname], 'urlname': [user.urlname], 'address': [user.address], 'website': [user.website], 'description': [user.description], 'style': [user.style] } sf = SettingForm(args) sf.edit_nickname_count = user.edit_nickname_count sf.edit_urlname_count = user.edit_urlname_count sf.user = user else: sf = SettingForm() else: if user: sf = SettingForm(args) sf.edit_nickname_count = user.edit_nickname_count sf.edit_urlname_count = user.edit_urlname_count sf.user = user else: sf = SettingForm(args) return sf def validate_nickname(self, field): data = field.data if data != self.user.nickname: if self.user.edit_nickname_count < 1: raise ValidationError('您已经没有修改昵称的机会') def validate_urlname(self, field): data = field.data if data != self.user.urlname: if self.user.edit_urlname_count < 1: field.data = self.user.urlname raise ValidationError('您已经没有修改域名的机会') if data in config.forbidden_name_list or User.get(urlname=data): raise ValidationError('此域名已经被占用') def save(self, user=None): data = self.data user = user.update(data) return user
class TopicForm(BaseForm): node_name = SelectField( '节点', [ validators.Required(), ], ) title = TextField( '标题', [ validators.Required(), validators.Length(min=4, max=100), ], ) content = TextAreaField( '内容', [ validators.Required(), validators.Length(min=3, max=1000000), ], ) def validate_content(self, field): """ 为了照顾一图流 """ if field.data.find('<img class="upload-topic-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): result = {'status': 'error', 'message': '主题创建失败'} for field in self: if field.errors: for error in field.errors: if field.name == 'title': prev = '标题至少 4 字节' elif field.name == 'content': prev = '内容至少 3 字节' elif field.name == 'node_name': prev = '节点必须选择' result = {'status': 'error', 'message': prev} return result @staticmethod def init(choices, selected, args=None): node_name = SelectField( '节点', [ validators.Required(), ], choices=choices, ) title = TextField( '标题', [ validators.Required(), validators.Length(min=4, max=100), ], ) content = TextAreaField( '内容', [ validators.Required(), ], ) TopicForm.node_name = node_name TopicForm.title = title TopicForm.content = content if args: tf = TopicForm(args) else: tf = TopicForm() tf.node_name.data = selected return tf def validate_node_name(self, field): node_name = unicode(self.node_name.data) node = Node.get(name=node_name) if not node: raise ValidationError('不存在此节点') self.node = node return node def save(self, user, topic=None): data = self.data try: data.pop('node_name') except: print(data) content = unicode(data.get('content')) data.update({ 'user_id': user.id, 'node_id': self.node.id, 'content': strip_xss_tags(content) }) if topic: category = 'edit' pre_node_id = topic.node_id pre_title = topic.title pre_content = topic.content cur_node_id = data.get('node_id') cur_title = data.get('title') cur_content = data.get('content') changed = 0 if pre_node_id != cur_node_id: topic.node.topic_count -= 1 self.node.topic_count += 1 diff_content = '主题节点从' + '<a class="node" href="' +\ topic.node.url + '">' + topic.node.name +\ '</a>移动到<a class="node" href="' + self.node.url + '">' +\ self.node.name + '</a>' changed = 1 if pre_title != cur_title or pre_content != cur_content: content1 = '<p><h2>' + pre_title + '</h2></p>' + pre_content content2 = '<p><h2>' + cur_title + '</h2></p>' + cur_content diff_content = ghdiff.diff(content1, content2, css=None) changed = 1 if changed == 1: topic.node_id = cur_node_id topic.title = cur_title topic.content = cur_content History(user_id=user.id, content=diff_content, topic_id=topic.id).save() else: return topic else: category = 'create' topic = Topic(**data) return topic.save(category=category)
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