当前位置: 移动技术网 > IT编程>脚本编程>Python > 在python web.py中使用百度富文本编辑器 UEditor

在python web.py中使用百度富文本编辑器 UEditor

2019年04月19日  | 移动技术网IT编程  | 我要评论

金素恩图片,缘分吉他谱,花马轩卷饼王

ueditor官方没有支持python的版本,有人改了个python的django版本,但是没找到web.py的。

于是参考php版本,实现了一下web.py集成ueditor,包含了文件上传,图片上传,视频上传,图片远程抓取,涂鸦等。

可能会有一些session之类的没有处理。

 

首先改ueditor.config.js,把原来指向php的链接改成web.py的

 

 //图片上传配置区
        ,imageurl:/ue_imageup             //图片上传提交地址
        ,imagepath:                     //图片修正地址,引用了fixedimagepath,如有特殊需求,可自行配置
        //,imagefieldname:upfile                  //图片数据的key,若此处修改,需要在后台对应文件修改对应参数
        //,compressside:0                           //等比压缩的基准,确定maximagesidelength参数的参照对象。0为按照最长边,1为按照宽度,2为按照高度
        //,maximagesidelength:900                   //上传图片最大允许的边长,超过会自动等比缩放,不缩放就设置一个比较大的值,更多设置在image.html中
        //,savepath: [ 'upload1', 'upload2', 'upload3' ]    //图片保存在服务器端的目录, 默认为空, 此时在上传图片时会向服务器请求保存图片的目录列表,
                                                            // 如果用户不希望发送请求, 则可以在这里设置与服务器端能够对应上的目录名称列表
                                                            //比如: savepath: [ 'upload1', 'upload2' ]

        //涂鸦图片配置区
        ,scrawlurl:/ue_scrawlup           //涂鸦上传地址
        ,scrawlpath:                            //图片修正地址,同imagepath

        //附件上传配置区
        ,fileurl:/ue_fileup               //附件上传提交地址
        ,filepath:                   //附件修正地址,同imagepath
        //,filefieldname:upfile                    //附件提交的表单名,若此处修改,需要在后台对应文件修改对应参数

        //远程抓取配置区
        //,catchremoteimageenable:true               //是否开启远程图片抓取,默认开启
        ,catcherurl:/ue_getremoteimage   //处理远程图片抓取的地址
        ,catcherpath:                  //图片修正地址,同imagepath
        //,catchfieldname:upfile                   //提交到后台远程图片uri合集,若此处修改,需要在后台对应文件修改对应参数
        //,separater:'ue_separate_ue'               //提交至后台的远程图片地址字符串分隔符
        //,localdomain:[]                            //本地顶级域名,当开启远程图片抓取时,除此之外的所有其它域名下的图片都将被抓取到本地,默认不抓取127.0.0.1和localhost

        //图片在线管理配置区
        ,imagemanagerurl:/ue_imagemanager       //图片在线管理的处理地址
        ,imagemanagerpath:                                    //图片修正地址,同imagepath

        //屏幕截图配置区
        ,snapscreenhost: location.hostname                                 //屏幕截图的server端文件所在的网站地址或者ip,请不要加https://
        ,snapscreenserverurl: /ue_imageup //屏幕截图的server端保存程序,ueditor的范例代码为“url +server/upload/php/snapimgup.php”
        ,snapscreenpath: 
        ,snapscreenserverport: location.port                                   //屏幕截图的server端端口
        //,snapscreenimgalign: ''                                //截图的图片默认的排版方式

        //word转存配置区
        ,wordimageurl:/ue_imageup             //word转存提交地址
        ,wordimagepath:                       //
        //,wordimagefieldname:upfile                     //word转存表单名若此处修改,需要在后台对应文件修改对应参数

        //视频上传配置区
        ,getmovieurl:/ue_getmovie                   //视频数据获取地址
        ,videourl:/ue_fileup               //附件上传提交地址
        ,videopath:                   //附件修正地址,同imagepath
        //,videofieldname:upfile                    //附件提交的表单名,若此处修改,需要在后台对应文件修改对应参数
然后配置web.py的urls映射

 

 

urls = (
    '/', 'index',
    '/ue_imageup', ue_imageup,
    '/ue_fileup', ue_fileup,
    '/ue_scrawlup', ue_scrawlup,
    '/ue_getremoteimage', ue_getremoteimage,
    '/ue_getmovie', ue_getmovie,
    '/ue_imagemanager', ue_imagemanager,
)
最后实现这些web.py的class。

 

 

#coding=utf-8
import base64
import uuid
import urllib2
import os

import web

ueconfig_dir = 'static/upload'
ueconfig_url = '/' + ueconfig_dir


def listimage(rootdir, retlist):
    for cfile in os.listdir(rootdir):
        path = os.path.join(rootdir, cfile)
        if os.path.isdir(path):
            listimage(path, retlist)
        else:
            if cfile.endswith('.gif') or cfile.endswith('.png') or cfile.endswith('.jpg') or cfile.endswith('.bmp'):
                retlist.append('/static/upload/' + cfile)


def saveuploadfile(filename, content):
    filename = filename.replace('\', '/') # replaces the windows-style slashes with linux ones.
    fout = open(ueconfig_dir + '/' + filename, 'wb') # creates the file where the uploaded file should be stored
    fout.write(content) # writes the uploaded file to the newly created file.
    fout.close() # closes the file, upload complete.


class ue_imageup:
    def get(self):
        reqdata = web.input()
        if 'fetch' in reqdata:
            web.header('content-type', 'text/javascript')
            return 'updatesavepath([upload]);'
        web.header(content-type, text/html; charset=utf-8)
        return 

    def post(self):
        postdata = web.input(upfile={}, pictitle=)
        web.debug(postdata)
        fileobj = postdata.upfile
        pictitle = postdata.pictitle
        filename = fileobj.filename
        newfilename = str(uuid.uuid1()) + .png
        saveuploadfile(newfilename, fileobj.file.read())
        return {'url':' + ueconfig_url + '/' + newfilename + ','title':' + pictitle + ','original':' + filename + ','state':' + success + '}


class ue_fileup:
    def get(self):
        web.header(content-type, text/html; charset=utf-8)
        return 

    def post(self):
        postdata = web.input(upfile={})
        fileobj = postdata.upfile
        filename = postdata.filename
        ext = '.' + filename.split('.')[-1]
        #web.py的static目录对中文文件名不支持,会404
        newfilename = str(uuid.uuid1()) + ext
        #filenameformat = postdata.filenameformat
        saveuploadfile(newfilename, fileobj.file.read())
        return {'url':' + ueconfig_url + '/' + newfilename + ','filetype':' + ext + ','original':' + filename + ','state':' + success + '}


class ue_scrawlup:
    def get(self):
        web.header(content-type, text/html; charset=utf-8)
        return 

    def post(self):
        reqdata = web.input(upfile={})
        if 'action' in reqdata:
            if reqdata.action == 'tmpimg':
                #上传背景
                fileobj = reqdata.upfile
                filename = fileobj.filename
                saveuploadfile(filename, fileobj.file.read())
                return <script>parent.ue_callback( + ueconfig_url + '/' + filename + ',' + success + ')</script>
        else:
            base64content = reqdata.content
            filename = str(uuid.uuid1()) + '.png'
            saveuploadfile(filename, base64.decodestring(base64content))
            return {'url':' + ueconfig_url + '/' + filename + ',state:' + success + '}


class ue_getremoteimage:
    def get(self):
        web.header(content-type, text/html; charset=utf-8)
        return 

    def post(self):
        postdata = web.input()
        urls = postdata.upfile
        #urls = urls.replace('&','&')
        urllist = urls.split(ue_separate_ue)
        filetype = [.gif, .png, .jpg, .jpeg, .bmp]
        outlist = []
        for fileurl in urllist:
            if not fileurl.startswith('http'):
                continue
            ext = . + fileurl.split('.')[-1]
            web.debug(ext + | + fileurl)
            if ext in filetype:
                filename = str(uuid.uuid1()) + ext
                saveuploadfile(filename, urllib2.urlopen(fileurl).read())
                outlist.append(ueconfig_url + / + filename)
        outlist = ue_separate_ue.join(outlist)
        return {'url':' + outlist + ','tip':'远程图片抓取成功!','srcurl':' + urls + '}


class ue_getmovie:
    def post(self):
        reqdata = web.input()
        skey = reqdata.searchkey
        vtype = reqdata.videotype
        surl = 'https://api.tudou.com/v3/gw?method=item.search&appkey=mykey&format=json&kw=' + skey + '&pageno=1&pagesize=20&channelid=' + vtype + '&indays=7&media=v&sort=s'
        htmlcontent = urllib2.urlopen(surl).read()
        web.debug(htmlcontent)
        return htmlcontent


class ue_imagemanager:
    def post(self):
        reqdata = web.input()
        if 'action' in reqdata:
            if reqdata.action == 'get':
                retfiles = []
                listimage(ueconfig_dir, retfiles)
                htmlcontent = ue_separate_ue.join(retfiles)
                return htmlcontent




 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网