def post(self, action='', post_id=0): ''' 编辑或者提交新文章 ''' if action == 'delete': try: post = Post.query.get_or_404(post_id) post.permissions.delete.test(self.identity, 401) db.session.delete(post) db.session.commit() detail = {'id': post_id} self.reply(0, u'删除成功', **detail) return except Exception as e: self.reply(1, str(e)) return # 默认都是编辑或者修改 # 0 表示不公开, 1表示公开 public = self.get_argument("public", '0') == '1' tags = self.get_argument("tags", '') title = self.get_argument("title", '') if not title: self.reply(1, u'请输入标题') return content = self.get_argument("content", '') if not content: self.reply(1, u'请输入内容') return # 如果是编辑 post = None if action == 'edit': try: post = Post.query.get_or_404(post_id) except Exception as e: self.reply(1, u'找不到该文章') return # 标题不能重复 if post.title != title and Post.query.get_by_title(title): self.reply(1, u'标题已经存在') return else: # 默认都是提交新文章 if Post.query.get_by_title(title): self.reply(1, u'标题已经存在') return post = Post(author_id=self.current_user.id) post.title = title post.content = content post.public = public post.tags = tags db.session.add(post) db.session.commit() detail = {'id': post.id} if action == 'edit': self.reply(0, u'修改成功', **detail) else: self.reply(0, u'保存成功', **detail)
def post(self, action='', post_id=0): ''' 编辑或者提交新文章 ''' if action == 'delete': try: post = Post.query.get_or_404(post_id) post.permissions.delete.test(self.identity, 401) db.session.delete(post) db.session.commit() detail = {'id': post_id} self.reply(0, u'删除成功', **detail) return except Exception as e: self.reply(1, str(e)) return # 默认都是编辑或者修改 # 0 表示不公开, 1表示公开 public = self.get_argument("public", '0') == '1' tags = self.get_argument("tags", '') title = self.get_argument("title", '') version = int(self.get_argument('version', '0')) if not title: self.reply(1, u'请输入标题') return html = self.get_argument("html", "") markdown = self.get_argument("markdown", "") if not html or not markdown: self.reply(1, u'请输入内容') return if tags == '20': # 日记 try: datetime.strptime(title, "%Y.%m.%d") except: self.reply(1, u'日记的标题格式为%Y.%m.%d') return # 如果是编辑 post = None if action == 'edit': try: post = Post.query.get_or_404(post_id) except Exception as e: self.reply(1, u'找不到该文章') return if version < post.version(): self.reply(1, u'版本太旧') return # 标题不能重复 if post.title != title and Post.query.get_by_title(title): self.reply(1, u'标题已经存在') return else: # 默认都是提交新文章 if Post.query.get_by_title(title): self.reply(1, u'标题已经存在') return post = Post(author_id=self.current_user.id) post.title = title post.html = html post.markdown = markdown post.public = public post.tags = tags db.session.add(post) db.session.commit() detail = {'id': post.id, 'version': post.version()} if action == 'edit': self.reply(0, u'修改成功', **detail) else: self.reply(0, u'保存成功', **detail)