def article_info(article_id): print "===getcwd:", os.getcwd() print "===realpath:", os.path.dirname(os.path.realpath(__file__)) all_comments = [] current_article = Articles.query.filter(Articles.id == article_id).first() if current_article: # 显示文章 current_article.create_time = current_article.create_time.strftime( '%Y-%m-%d %H:%M:%S') article_info = class_to_dict(current_article) article_info['author'] = current_article.author.username # 显示评论 comments = Comments.query.filter( Comments.article_id == article_id).all() for c in comments: author = c.author.username c = class_to_dict(c) c['author'] = author all_comments.append(c) return render_template('article.html', article=article_info, comments=all_comments) print(url_for('not_found.not_found_404')) else: return redirect(url_for('not_found.not_found_404'))
def get_articles(): condition = request.values.get("condition", None) sort = request.values.get("sort", None) page_index = int(request.values.get("page_index", 1)) page_size = int(request.values.get("page_size", 10)) query_result = Articles.query data = [] try: if condition: query_result = query_result.filter(Articles.title.like('%{}%'.format(condition)) | Articles.content.like('%{}%'.format(condition)) | Articles.type.like('%{}%'.format(condition))) if sort: pass else: query_result = query_result.order_by(db.desc(Articles.create_time)) paginate = query_result.paginate(page_index, page_size) data = { 'total': paginate.total, 'data': [class_to_dict(item) for item in paginate.items] } results = jsonify(get_result('SUCCESS', data)) except: print traceback.format_exc() results = jsonify(get_result('DB_ERROR', data)) return results
def get_article(): page_size = int(request.values.get('page_size', 10)) page_index = int(request.values.get('page_index', 1)) sort = request.values.get('sort') title = request.values.get('title') content = request.values.get('content') article_type = request.values.get('type') try: query_article = Articles.query if sort: query_article = query_article.order_by( db.desc(Articles.create_time)) if title: query_article = query_article.filter( Articles.title.like('%{}%'.format(title))) if content: query_article = query_article.filter( Articles.content.like('%{}%'.format(content))) if article_type: query_article = query_article.filter( Articles.type.like('%{}%'.format(article_type))) paginate = query_article.paginate(page_index, page_size, False) data = { "total": len(paginate.items), "data": [class_to_dict(item) for item in paginate.items] } return jsonify(get_result("SUCCESS", data)) except: db.session.rollback() logger.info(traceback.extract_stack()) return jsonify(get_result("DB_ERROR", {}))
def all_articles(): all_articles = [] # 显示所有文章 articles = Articles.query.all() for article in articles: author = article.author.username article = class_to_dict(article) article['author'] = author article['content'] = article['content'][0:40] + '......' all_articles.append(article) return render_template('all_article.html', articles=all_articles)
def index_page(): all_articles = [] # 显示最新三篇文章 articles = Articles.query.order_by(db.desc(Articles.create_time)) articles = articles.all() if articles: article_num = (len(articles) if config.index_article_num >= len(articles) else config.index_article_num) for i in range(article_num): author = articles[i].author.username articles[i] = class_to_dict(articles[i]) articles[i]['author'] = author articles[i]['content'] = articles[i]['content'][0:40] + '......' all_articles.append(articles[i]) return render_template('index.html', articles=all_articles)