Esempio n. 1
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'] = '上传接口出错'
Esempio n. 2
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)