def _prepare_base_data(): """Get base data of all frontend web page""" labels = Label.get_labels() logging.info("labels: {}".format(labels)) # Check user login has_login = (session.get('username', None) is not None) return labels, has_login
def edit(): article_id = request.args.get("id", None) if article_id is None: return redirect(url_for("manage")) article = Article.get_article_by_id(article_id) if not article: return redirect(url_for("manage")) article_id = article.id title = article.title summary = article.summary content = article.content default_label_id = article.labelid labels = Label.get_labels() username = session.get("username", "") return render_template( "backend/post_article.html", id=article_id, title=title, summary=summary, content=content, default_label_id=default_label_id, labels=labels, username=username, )
def post(): logging.info("** backend post request") if request.method == "POST": logging.info("request form: {}".format(request.form)) ariticle_id = request.form["id"] title = request.form["title"] content = request.form["content"] label_id = int(request.form["label"]) summary = request.form["summary"] status = request.form["status"] if not title: return jsonify({"msg": u"请输入标题", "error": True}) if len(title) > 50: return jsonify({"msg": u"标题太长", "error": True}) if not summary: return jsonify({"msg": u"请输入摘要", "error": True}) if len(summary) > 200: return jsonify({"msg": u"摘要太长", "error": True}) publishtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) kwargs = { "title": title, "summary": summary, "content": content, "labelid": label_id, "publishtime": publishtime, "status": int(status), } if ariticle_id != "": # Update the article id = int(ariticle_id) Article(id=id).update(**kwargs) else: # Insert new article record Article(**kwargs).insert() return jsonify({"redirect": "/backend/manage", "error": False}) username = session.get("username", "") labels = Label.get_labels() default_label_id = 1 return render_template( "/backend/post_article.html", username=username, labels=labels, default_label_id=default_label_id )
def manage(): page = request.args.get("page", 1) logging.info("page: {}".format(page)) try: current_page = int(page) except Exception: return abort(404) uri = str(request.url_rule) template = "/backend/manage_articles.html" # Choose article status article_status = STATUS_PUBLISH if len(uri) > 5 and uri[-5:] == "draft": article_status = STATUS_SAVE template = "/backend/draft.html" logging.info("uri: {}".format(uri)) paging_amount = Article.get_articles_paging_amount() # Deal with current_page params error if current_page > paging_amount or current_page < 1: return abort(404) articles = Article.get_articles_of_current_page(current_page=current_page, article_status=article_status) logging.info("articles: {}".format(articles)) label = Label.get_labels() status = {STATUS_PUBLISH: u"发布", STATUS_SAVE: u"保存"} for article in articles: article.label = label[article.labelid - 1]["name"] article.status = status[article.status] username = session.get("username", "") return render_template( template, username=username, articles=articles, current_page=current_page, paging_amount=paging_amount, uri=uri )
def test_get_labels(): labels = Label.get_labels() assert len(labels) == 5