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
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 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), ], ) @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('此用户名已注册') @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
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('记住我') @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('用户名或密码错误')
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, 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
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