def publish_article(): form = ArticleForm().validate_for_api() title = form.title.data article_title = Article.query.filter_by(title=title).first() column_id = form.column_id.data column = Submenu.query.filter_by(id=column_id).first() user_id = form.user_id.data create_time = form.create_time.data if article_title: data = {"error_code": 100, "msg": "文章标题重复"} return restful_json(data) else: with db.auto_commit(): article = Article() article.title = form.title.data article.author = form.author.data article.content = form.content.data article.column_id = form.column_id.data article.column_name = column.name_zh article.menu_id = column.menu_id article.en_name = column.menu.en_name article.menu_name = column.menu.menu_name article.recommend = form.recommend.data article.status = form.status.data if user_id: article.user_id = user_id else: article.user_id = g.user.uid if create_time: article.create_time = create_time else: article.create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) db.session.add(article) return Success()
def add_article(self, form, user): try: article = Article(form=form) article.user_id = user.id article.read_count = 0 category = self.dao.getCategoryById(form['category']) article.category = [category] self.dao.add_article(article) return PublicMethod.true_return(data='', msg=u'发布成功') except Exception, e: traceback.print_exc() return PublicMethod.false_return(data='', msg=u'后台报错,请联系管理员')