Example #1
0
def plan_show(plan_id):
    u"""查看方案"""
    try:
        with db_session_cm() as session:
            plan = session.query(Plan).outerjoin(Plan.keywords).outerjoin(Plan.content).filter(Plan.id == plan_id).first()
            if plan:
                return render_template("admin/plan/show.html", plan=plan)
            abort(404)
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #2
0
def impress_delete(impress_id):
    u"""删除印象"""
    try:
        with db_session_cm() as session:
            impress_content = session.query(ImpressContent).get(impress_id)
            session.delete(impress_content)
            session.commit()
            flash(_(u"删除成功!"))
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        flash(_(u"删除失败!"))
    return redirect(url_for('admin_frontend.impresses'))
Example #3
0
def share_detail(plan_id):
    try:
        with db_session_cm() as session:
            plan = session.query(Plan).outerjoin(Plan.keywords).outerjoin(Plan.content).\
                filter(Plan.status == Plan.STATUS_PUBLISH).filter(Plan.id == plan_id).one()
            return render_template("plan/share_detail.html", plan=plan)
    except NoResultFound:
        common_logger.warn("view not exists or unpublished plan = %s" % plan_id)
        abort(404)
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #4
0
def impress_to_preset(impress_id):
    u"""修改印象为预设"""
    try:
        with db_session_cm() as session:
            impress_content = session.query(ImpressContent).get(impress_id)
            impress_content.type = ImpressContent.TYPE_PRESET
            session.add(impress_content)
            session.commit()
            flash(_(u"修改为预设成功!"))
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        flash(_(u"修改为预设失败!"))
    return redirect(url_for('admin_frontend.impresses'))
Example #5
0
def keyword_edit(keyword_id):
    u"""关键之详情"""
    try:
        with db_session_cm() as session:
            keyword = session.query(PlanKeyword).filter(PlanKeyword.id == keyword_id).first()
            plan_form = PlanKeywordsForm(id=keyword.id,content = keyword.content)
            context = {
                'form': plan_form
            }
        return render_template("admin/keyword/edit.html",**context)
    except Exception , e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #6
0
def keyword_to_preset(keyword_id):
    u"""修改关键字为预设"""
    try:
        with db_session_cm() as session:
            keyword = session.query(PlanKeyword).get(keyword_id)
            keyword.type = ImpressContent.TYPE_PRESET
            session.add(keyword)
            session.commit()
            flash(_(u"修改为预设成功!"))
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        flash(_(u"修改为预设失败!"))
    return redirect(url_for('admin_frontend.keywords'))
Example #7
0
def impress_update(impress_id):
   u"""impress Update"""
   try:
        with db_session_cm() as session:
            impress_content_form = PresetImpressForm(request.form)
            if request.method == 'POST' and impress_content_form.validate_on_submit():
                content = impress_content_form.content.data.strip()
                session.query(ImpressContent).filter(ImpressContent.id == impress_id).update(dict(content=content))
                session.commit()
        return redirect(url_for('admin_frontend.impresses'))
   except Exception as e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #8
0
def impress_edit(impress_id):
    u"""编辑印象"""
    try:
        with db_session_cm() as session:
            impress_content = session.query(ImpressContent).filter(ImpressContent.id == impress_id).first()
            impress_content_form = PresetImpressForm(content=impress_content.content)
            context = {
                'form': impress_content_form,
                'impress': impress_content
            }
        return render_template("admin/impress/edit.html", **context)
    except Exception , e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #9
0
def keyword_update():
   u"""keyword Update"""
   try:
        with db_session_cm() as session:
            if request.method == 'POST':
                plan_form = PlanKeywordsForm(request.form)
                keyword_id = plan_form.id.data.strip()
                content = plan_form.content.data.strip()
                session.query(PlanKeyword).filter(PlanKeyword.id == keyword_id).update(dict(content=content))
                session.commit()
        return redirect(url_for('admin_frontend.keywords'))
   except Exception ,e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #10
0
def upload():
    u"""UEditor文件上传接口
    config 配置文件
    result 返回结果
    """
    mimetype = 'application/json'
    result = {}
    action = request.args.get('action')
    # 解析JSON格式的配置文件

    CONFIG = setting.EDITOR_CONFIG
    if action == 'config':
        # 初始化时,返回配置文件给客户端
        result = CONFIG
    elif action in ('uploadimage', 'uploadfile', 'uploadvideo',):
        # 图片、文件、视频上传
        if action == 'uploadimage':
            fieldName = CONFIG.get('imageFieldName')
        elif action == 'uploadvideo':
            fieldName = CONFIG.get('videoFieldName')
        else:
            fieldName = CONFIG.get('fileFieldName')
        if fieldName in request.files:
            try:
                request_file = request.files[fieldName]
                with db_session_cm() as session:
                    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()
                    image = session.query(ImageResource).get(irs.id)
                    if image is None:
                        result['state'] = 'SUCCESS'
                        result['url'] = ''
                    else:
                        url = image_resources.url(image.path)
                        result['state'] = 'SUCCESS'
                        result['url'] = url
                        result['title'] = filename
                        result = json.dumps(result)
                        res = make_response(result)
                        return res
            except Exception, e:
                common_logger.error(traceback.format_exc(e))
            else:
                result['state'] = '上传接口出错'
Example #11
0
def keyword_delete(keyword_id):
    u"""删除关键字"""
    try:
        with db_session_cm() as session:
            keyword = session.query(PlanKeyword).get(keyword_id)
            if current_user and current_user.is_authenticated:
                session.delete(keyword)
                session.commit()
                flash(_(u"删除成功!"))
            else:
                flash(_(u"没有权限!"))
        return redirect(url_for('admin_frontend.keywords'))
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        flash(_(u"删除失败!"))
        return redirect(url_for('admin_frontend.keywords'))
Example #12
0
def plan_revocation(plan_id):
    u"""撤销方案"""
    try:
        with db_session_cm() as session:
            plan = session.query(Plan).get(plan_id)
            if current_user and current_user.is_authenticated:
                plan.status = Plan.STATUS_UNPUBLISHED
                session.merge(plan)
                session.commit()
                flash(_(u"撤销成功!"))
            else:
                flash(_(u"没有权限!"))
                return redirect(url_for('admin_frontend.plans'))
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        flash(_(u"失败!"))
    return redirect(url_for('admin_frontend.plans'))
Example #13
0
def plan_publish(plan_id):
    u"""发布方案"""
    try:
        with db_session_cm() as session:
            plan = session.query(Plan).get(plan_id)
            if current_user and current_user.is_authenticated:
                plan.publish()
                session.merge(plan)
                session.commit()
                flash(_(u"发布成功!"), category="info")
            else:
                flash(_(u"没有权限!"), category="warning")
            return redirect(url_for('admin_frontend.plans'))
    except Exception as e:
        common_logger.error(traceback.format_exc(e))
        flash(_(u"失败!"), category="danger")
    return redirect(url_for('admin_frontend.plans'))
Example #14
0
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)
Example #15
0
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)
Example #16
0
def impress_new():
    u"""新建印象"""
    try:
        impress_form = PresetImpressForm(request.form)
        context = {
            'form': impress_form,
        }
        if request.method == 'POST' and impress_form.validate_on_submit():
            content = impress_form.content.data.strip()
            with db_session_cm() as session:
                impress_content = ImpressContent(content=content)
                impress_content.type = ImpressContent.TYPE_PRESET
                session.add(impress_content)
                session.commit()
            flash(_(u"添加成功!"), category="success")
            return redirect(url_for('admin_frontend.impresses'))
        return render_template("admin/impress/new.html", **context)
    except Exception, e:
        common_logger.error(traceback.format_exc(e))
        abort(500)
Example #17
0
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)
Example #18
0
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))