Esempio n. 1
0
def get_list(page=1, page_size=20):
    """
    获取栏目列表
    :param page:        指定页
    :param page_size:   每页的条目数

    :param q:   查询关键词
    :param s:   排序字段
    :param o:   排序次序(asc、desc)
    :return:
    """
    # GET /category/v1/1_20.json
    # curl 'http://127.0.0.1:5000/category/v1/1_20.json?q=www&s=created_at&o=asc'

    query, sort, order = get_query_info(request)

    _total, _pages, _list = Category.get_page_with_query(
        query, sort, order, page, page_size)

    print _total, _pages
    print _list

    return render_json({
        "data_list": _list,
        "total": _total,
        "page": page,
        "page_size": page_size
    })
Esempio n. 2
0
def get_list(category_slug, page=1, page_size=20):
    """
    获取指定栏目下的内容列表
    :param category_slug:
    :param page:
    :param page_size:
    :return:
    """
    # GET /entry/1/1_20.html
    # curl 'http://127.0.0.1:5000/software/1_20.json?q=www&s=created_at&o=asc'

    query, sort, order = get_query_info(request)

    category = Category.get_by_slug(category_slug)

    if not category:
        abort(404)

    # 通过反射获得数据类
    ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[
        category.entry_type.code]]

    _total, _pages, _list = ModelClass.get_page_by_category_id(
        category.id, sort, order, page, page_size)

    return render_json({
        "data_list": _list,
        "total": _total,
        "page": page,
        "page_size": page_size
    })
Esempio n. 3
0
def create_v1():
    tag_name = request.form.get('tag_name', '')
    slug = request.form.get('slug', '')

    print '212121', tag_name, slug
    tag = Tag.add_tag(tag_name, slug)

    return render_json({'code': ResultCode.SUCCESS, 'data': tag})
Esempio n. 4
0
def get_list(page):
    # GET /entry/list/1/20.html
    _total, _pages, _list = Entry.get_page_by_category(1, "", "", "id", "DESC",
                                                       1)

    return render_json({
        "list": _list,
        "total": _total,
        "page": page,
        "page_size": 20
    })
Esempio n. 5
0
def create():
    """
    创建栏目
    """
    # curl 'http://127.0.0.1:5000/category/v1/create'

    form = CategoryForm(request.form)
    model = Category()
    form.populate_obj(model)
    model.save()

    return render_json({"code": 0})
Esempio n. 6
0
def create():
    """
    创建栏目
    """
    # curl 'http://127.0.0.1:5000/category/v1/create'

    form = CategoryForm(request.form)
    model = Category()
    form.populate_obj(model)
    model.save()

    return render_json({"code": 0})
Esempio n. 7
0
def create():
    """
    创建新的文章
    http://127.0.0.1:5000/entry/v1/create?category_id=1

    @param category_id:
    @return:
    """

    try:
        category_id = int(request.args.get('category_id', 0))
    except:
        return render_json({'code': ResultCode.FAILED, 'message': u'category_id无效'})

    category = Category.get_by_id(category_id)

    if not category:
        return render_json({'code': ResultCode.FAILED, 'message': u'没有有效的分类/栏目'})

    # 处理各类权限
    # if category.show_role:
    #     if not g.user or category.show_role > g.user.role:
    #         abort(404)
    #

    FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]]
    ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]]
    form = FormClass(request.form, next=request.args.get('next', None),
                        entry_type=category.entry_type)

    if request.method == 'GET':
        return render_json({'code': ResultCode.FAILED, 'message': u'仅支持POST请求'})

    if form.validate():
        # 获取指定的表单数据
        entry = ModelClass()
        form.populate_obj(entry)

        is_draft = False

        if form.draft.data:
            is_draft = True

        # print category.to_dict()
        # todo user.id
        if not ModelClass.add_or_update(category, entry, is_draft, True):
            return render_json({'code': ResultCode.FAILED, 'message': u'add_or_update error'})

        return render_json({'code': ResultCode.SUCCESS, 'data': entry})
    elif form.errors:
        if __debug__:
            print "form errors", form.validate()

        _list = []
        for error_name, error_value in form.errors.iteritems():
            if __debug__:
                print "error: %s %s" % (error_name, error_value)
            _list.append((error_name, error_value))

        return render_json({'code': ResultCode.SYSTEM_ERROR, 'message': _list})
Esempio n. 8
0
def edit_v1():
    # http://127.0.0.1:5000/tag/v1/edit?tag_id=1
    # curl 'http://127.0.0.1:5000/tag/v1/edit?tag_id=1' -d 'tag_name=test1&slug=test1'

    tag_id = request.args.get('tag_id', '')

    tag_name = request.form.get('tag_name', '')
    slug = request.form.get('slug', '')

    print '212121', tag_name, slug
    tag = Tag.get_by_id(tag_id)
    tag.tag_name = tag_name
    tag.slug = slug
    tag.save()
    # tag = Tag.add_tag(tag_name, slug)

    return render_json({'code': ResultCode.SUCCESS, 'data': tag.to_dict()})
Esempio n. 9
0
def create():
    """
    创建新的文章
    @param category_id:
    @return:
    """

    new_templates = 'entry/create.html'
    form = SoftwareForm(request.form,
                        next=request.args.get('next', None),
                        entry_type=1)

    if request.method == 'POST' and form.validate():
        # 获取指定的表单数据
        entry = Entry()
        form.populate_obj(entry)
        category = Category.get_by_id(int(1))

        is_draft = False

        #if form.draft.data:
        #    is_draft = True
        print entry.title
        if not Entry.add_or_update(category, entry, is_draft, True):
            flash(_("Internal error"), "failed")
            return render_template(new_templates,
                                   form=form,
                                   category=category,
                                   current_category=category)

        flash(_("Create success"), "success")

        #next_url = form.next.data

        # todo wqq: 这里能否不做跳转,送回json数据返回ID号,由前端去做处理
        #if not next_url or next_url == request.path:
        #    next_url = url_for('portal.entry', slug=entry.slug)

        return render_json({"success": "true", "id": entry.id})
    elif form.errors:
        print "oooooooooooooooooooooooooooo", form.validate()
        for error_name, error_value in form.errors.iteritems():
            print "error: %s %s" % (error_name, error_value)
        flash(_("Cause an error"), "failed")
    return render_html(new_templates, form=form)
Esempio n. 10
0
def get_list(page=1, page_size=20):
    """
    获取栏目列表
    :param page:        指定页
    :param page_size:   每页的条目数

    :param q:   查询关键词
    :param s:   排序字段
    :param o:   排序次序(asc、desc)
    :return:
    """
    # GET /category/v1/1_20.json
    # curl 'http://127.0.0.1:5000/category/v1/1_20.json?q=www&s=created_at&o=asc'

    query, sort, order = get_query_info(request)

    _total, _pages, _list = Category.get_page_with_query(query, sort, order, page, page_size)

    print _total, _pages
    print _list

    return render_json({"data_list": _list, "total": _total, "page": page, "page_size": page_size})
Esempio n. 11
0
def get_list(category_slug, page=1, page_size=20):
    """
    获取指定栏目下的内容列表
    :param category_slug:
    :param page:
    :param page_size:
    :return:
    """
    # GET /entry/1/1_20.html
    # curl 'http://127.0.0.1:5000/software/1_20.json?q=www&s=created_at&o=asc'

    query, sort, order = get_query_info(request)

    category = Category.get_by_slug(category_slug)

    if not category:
        abort(404)

    # 通过反射获得数据类
    ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]]

    _total, _pages, _list = ModelClass.get_page_by_category_id(category.id, sort, order, page, page_size)

    return render_json({"data_list": _list, "total": _total, "page": page, "page_size": page_size})
Esempio n. 12
0
def show(id_, w=0, h=0):
    """
    显示一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 13
0
def upload():
    """
    上传一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 14
0
def get(id_):
    """
    获取一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 15
0
def upload_img():
    print 'upload_img enter'
    # 原始文件名,表单名固定,不可配置
    oriName = request.args.get('fileName', '').strip()

    # 上传图片框中的描述表单名称,
    title = request.args.get('pictitle', '').strip()

    fn = request.args.get('CKEditorFuncNum', '')

    # Type=Image&CKEditor=editor1&CKEditorFuncNum=2&langCode=zh-cn
    # 文件上传状态,当成功时返回SUCCESS,其余值将直接返回对应字符窜并显示在图片预览框,同时可以在前端页面通过回调函数获取对应字符窜
    state = "FAILED"

    # 重命名后的文件名
    fileName=""

    if 'Filedata' in request.files:
        f_type = 'Filedata'
    if 'upload' in request.files:
        f_type = 'upload'

    if f_type:
        file = request.files[f_type]
        name, ext = os.path.splitext(file.filename)
        ext = ext[1:]
        ext = ext.lower()
        m = hashlib.md5()
        m.update(file.read())
        # todo 为何多2个字节?
        size = file.tell() - 2
        print size
        file.seek(0)

        material = Material.get_by_file_signature(m.hexdigest())

        if not material:
            # 按日期存放
            folder_name, file_name = build_date_folder_file()
            print folder_name, file_name
            filename = images.save(file, folder=folder_name, name='%s.%s' % (file_name, ext))

            material = Material()
            material.added_user_id = current_user.id
            material.file_name = file_name
            material.file_ext = ext
            material.file_path = folder_name
            material.file_type = ext
            material.file_size = size
            material.file_signature = m.hexdigest()
            material.thumbnail_file = ''
            material.url = images.url(filename)
            material.title = title
            material.save()

        # 向浏览器返回数据json数据
        result = {
            'url':      url_for('misc.photo', id=material.id, ext=ext),   # 保存后的文件路径
            'title':    title,   # 文件描述,对图片来说在前端会添加到title属性上
            'original': oriName,   # 原始文件名
            'error': 0,
            'state':    'SUCCESS'  # 上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
        }

    #    echo "{'url':'".$fileName."','title':'".$title."','original':'".$oriName."','state':'".$state."'}";
        if f_type == "upload":
            return '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction(%s,"%s", "%s");</script>' \
                   % (fn, url_for('misc.photo', id=material.id, ext=ext), '')

        if f_type == "Filedata":
            return render_json(result)

    # todo
    # UploadNotAllowed
    # 文件大小

    return json_encode({'state': state})
Esempio n. 16
0
def edit():
    # http://127.0.0.1:5000/entry/edit?category_id=1&entry_id=1
    # int_value_verify(id)

    try:
        category_id = int(request.args.get('category_id', 0))
    except:
        return render_json({'code': ResultCode.FAILED, 'message': u'category_id无效'})

    category = Category.get_by_id(category_id)

    if not category:
        return render_json({'code': ResultCode.FAILED, 'message': u'没有有效的分类/栏目'})

    # 处理各类权限
    # if category.show_role:
    #     if not g.user or category.show_role > g.user.role:
    #         abort(404)
    #

    if request.method == 'GET':
        return render_json({'code': ResultCode.FAILED, 'message': u'仅支持POST请求'})

    FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]]
    ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]]

    try:
        entry_id = int(request.args.get('entry_id', 0))
    except:
        return render_json({'code': ResultCode.DATA_NOT_EXIST, 'message': u'内容数据不存在, entry_id: %d' %
                                                                          request.args.get('entry_id', 0)})

    entry = ModelClass.get_by_id(entry_id)
    if not entry:
        return render_json({'code': ResultCode.DATA_NOT_EXIST, 'message': u'内容数据不存在, class: %s, entry_id: %d' %
                                                                          (ModelClass.__tablename__, entry_id)})

    # if entry.author.id != g.user.id and not g.user.is_supervisor:
    #     abort(404)
    #
    # if entry.entry_status != EntryStatus.published and not g.user.is_supervisor:
    #     abort(404)
    #
    # if entry.category.show_role:
    #     if not g.user or entry.category.show_role > g.user.role:
    #         abort(404)

    category = entry.category

    form = FormClass(request.form, next=request.args.get('next', None), entry_type=category.entry_type)

    if form.validate():
        form.populate_obj(entry)

        is_draft = False

        if form.draft.data:
            is_draft = True

        if not ModelClass.add_or_update(category, entry, is_draft, True):
            return render_json({'code': ResultCode.FAILED, 'message': u'add_or_update error'})

        return render_json({'code': ResultCode.SUCCESS, 'data': entry})
    elif form.errors:
        _list = []
        for error_name, error_value in form.errors.iteritems():
            if __debug__:
                print "error: %s %s" % (error_name, error_value)
            _list.append((error_name, error_value))

        return render_json({'code': ResultCode.SYSTEM_ERROR, 'message': _list})
Esempio n. 17
0
def delete(id_):
    """
    删除一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 18
0
def show(id_, w=0, h=0):
    """
    显示一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 19
0
def get(id_):
    """
    获取一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 20
0
def upload():
    """
    上传一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 21
0
def delete(id_):
    """
    删除一张缩略图
    :return:
    """
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 22
0
def index():
    return render_json({'code': ResultCode.SUCCESS})
Esempio n. 23
0
def index():
    return render_json({'code': ResultCode.SUCCESS})