def keyword_new(): u"""新建关键字""" try: plan_form = PlanKeywordsForm(request.form) context = { 'form': plan_form, } if request.method == 'POST' and plan_form.validate_on_submit(): content = plan_form.content.data.strip() with db_session_cm() as session: plan_keyword = PlanKeyword(content=content) plan_keyword.type = PlanKeyword.TYPE_PRESET session.add(plan_keyword) session.commit() flash(_(u"添加成功!"), category="success") return redirect(url_for('admin_frontend.keywords')) return render_template("admin/keyword/new.html", **context) except Exception, e: common_logger.error(traceback.format_exc(e)) abort(500)
def plan_new(): u"""新建方案""" plan_form = PlanForm(request.form) plan_form.keywords.choices = PlanKeyword.choices() context = { 'form': plan_form, } try: if request.method == 'POST' and plan_form.validate_on_submit(): title = plan_form.title.data.strip() content = plan_form.content.data.strip() keywords = plan_form.keywords.data with db_session_cm() as session: plan = Plan(title) plan_content = PlanContent(content=content) plan.content = plan_content u"""upload image """ request_file = request.files['image'] if request_file: filename = image_resources.save(request_file, folder=str(current_user.id)) irs = ImageResource(filename, current_user.id) name, suffix = os.path.splitext(request_file.filename) irs.format = suffix session.add(irs) session.commit() plan.cover_image_id = irs.id if current_user and current_user.is_authenticated: plan.author_id = current_user.get_id() if keywords: keywords = session.query(PlanKeyword).filter(PlanKeyword.id.in_(keywords)).all() plan.keywords = keywords session.add(plan) session.commit() flash(_(u"方案添加成功!"), category="success") return redirect(url_for('admin_frontend.plans')) return render_template("admin/plan/new.html", **context) except UploadNotAllowed as e: flash(u"封面图片格式不支持", category="warning") return render_template("admin/plan/new.html", **context) except Exception, e: common_logger.error(traceback.format_exc(e)) abort(500)
def plan_edit(plan_id): u"""修改方案""" try: with db_session_cm() as session: plan = session.query(Plan).options(subqueryload(Plan.content)).get(plan_id) plan_form = PlanForm( title=plan.title, content=plan.content and plan.content.content or "", keywords=[kw.id for kw in plan.keywords] ) plan_form.keywords.choices = PlanKeyword.choices() context = { 'form': plan_form, 'plan': plan } return render_template("admin/plan/edit.html", **context) except Exception as e: common_logger.error(traceback.format_exc(e)) abort(500)
def plan_update(plan_id): u"""更新方案""" try: plan_form = PlanForm(request.form) plan_form.keywords.choices = PlanKeyword.choices() if request.method == 'POST' and plan_form.validate_on_submit(): with db_session_cm() as session: title = plan_form.title.data.strip() content = plan_form.content.data.strip() keywords = plan_form.keywords.data request_file = request.files['image'] plan = session.query(Plan).options(subqueryload(Plan.content)).\ join(Plan.keywords).filter(Plan.id == plan_id).first() plan.content.content = content plan.title = title if request_file: name, suffix = os.path.splitext(request_file.filename) filename = image_resources.save(request_file, folder=str(current_user.id)) common_logger.debug(plan.cover_image) common_logger.debug(type(plan.cover_image)) cover_image = plan.cover_image or ImageResource(filename, current_user.id) common_logger.debug(cover_image) cover_image.path = filename cover_image.format = suffix plan.cover_image = cover_image if keywords: keywords = session.query(PlanKeyword).filter(PlanKeyword.id.in_(keywords)).all() plan.keywords = keywords session.add(plan) session.commit() flash(_(u"方案编辑成功!"), category="success") return redirect(url_for('admin_frontend.plan_show', plan_id=plan.id)) except Exception, e: common_logger.error(traceback.format_exc(e)) flash(_(u"方案编辑失败!"), category="danger") return redirect(url_for('admin_frontend.plan_show', plan_id=plan_id))