django与百度ueditor 集成之二:涂鸦,抓图,搜视频,图片浏览

摘要: 上一篇文章提到django与百度ueditor 结合实现文件上传,图片上传。但还有如下功能没实现:1. 在线涂鸦后,图片的保存,并显示2. 图片的在线管理,浏览(目录递归浏览)3. 在线视频搜索4. 远程抓图

上一篇文章提到django与百度ueditor 结合实现文件上传,图片上传。但还有如下功能没实现:
1. 在线涂鸦后,图片的保存,并显示
2. 图片的在线管理,浏览(目录递归浏览)
3. 在线视频搜索
4. 远程抓图

在看测试代码之前,请注意在django程序的settings.py 中配置:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__),'static/upload/').replace('\\','/'),


这是我的配置,你可以改成适合你的配置。

今天又抽了一个小时,参考了下java 的代码,将上面的四个功能也实现,有需要的朋友可以参考. 同样在前面文章的基础上,在views.py 里面添加代码:
在线涂鸦
@csrf_exempt 
def ueditor_ScrawUp(request):
    """ 涂鸦文件,处理 """
    print request
    param = request.POST.get("action",'')
    fileType = [".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"];
    
    if  param=='tmpImg':
        fileObj = request.FILES.get('upfile', None)   
        source_pictitle = request.POST.get('pictitle','')
        source_filename = request.POST.get('fileName','')  
        response = HttpResponse()  
        myresponse = __myuploadfile(fileObj, source_pictitle, source_filename,'pic')
        myresponsedict=dict(myresponse)
        url=myresponsedict.get('url','')
        response.write("" %(url,'SUCCESS'))
        return response
    else:
        #========================base64上传的文件======================= 
        base64Data = request.POST.get('content','')
        subfolder = time.strftime("%Y%m")
        if not os.path.exists(settings.MEDIA_ROOT[0] + subfolder):
            os.makedirs(settings.MEDIA_ROOT[0] + subfolder)
        file_name = str(uuid.uuid1())
        path = str(subfolder + '/' + file_name + '.' + 'png')
        phisypath = settings.MEDIA_ROOT[0] + path        
        f=open(phisypath,'wb+')
        f.write(base64.decodestring(base64Data))
        f.close()
        response=HttpResponse()
        response.write("{'url':'%s',state:'%s'}" % ('/static/upload/' + subfolder + '/' + file_name + '.' + 'png','SUCCESS'));
        return response



图片的在线管理,浏览(目录递归浏览)
def listdir(path,filelist):
    """ 递归 得到所有图片文件信息 """
    phisypath = settings.MEDIA_ROOT[0]
    if os.path.isfile(path):
        return '[]' 
    allFiles=os.listdir(path)
    retlist=[]
    for cfile in allFiles:
        fileinfo={}
        filepath=(path+os.path.sep+cfile).replace("\\","/").replace('//','/')        
        
        if os.path.isdir(filepath):
            listdir(filepath,filelist)
        else:
            if cfile.endswith('.gif') or cfile.endswith('.png') or cfile.endswith('.jpg') or cfile.endswith('.bmp'):
                filelist.append(filepath.replace(phisypath,'/static/upload/').replace("//","/"))

@csrf_exempt
def ueditor_imageManager(request):
    """ 图片在线管理  """
    filelist=[]
    phisypath = settings.MEDIA_ROOT[0]
    listdir(phisypath,filelist)
    imgStr="ue_separate_ue".join(filelist)
    response=HttpResponse()
    response.write(imgStr)     
    return response


在线视频搜索
@csrf_exempt
def ueditor_getMovie(request):
    """ 获取视频数据的地址 """
    content ="";   
    searchkey = request.POST.get("searchKey");
    videotype = request.POST.get("videoType");
    try:        
        url = "http://api.tudou.com/v3/gw?method=item.search&appKey=myKey&format=json&kw="+ searchkey+"&pageNo=1&pageSize=20&channelId="+videotype+"&inDays=7&media=v&sort=s";
        content=urllib2.urlopen(url).read()
    except Exception,e:
        pass
    response=HttpResponse()  
    response.write(content);
    return response




远程抓图,将别人网站的图片保存到本地,并显示出来
@csrf_exempt 
def ueditor_getRemoteImage(request):
    print request
    """ 把远程的图抓到本地,爬图 """
    file_name = str(uuid.uuid1())
    subfolder = time.strftime("%Y%m")    
    if not os.path.exists(settings.MEDIA_ROOT[0] + subfolder):
        os.makedirs(settings.MEDIA_ROOT[0] + subfolder)    
    #====get request params=================================
    urls = str(request.POST.get("upfile"));
    urllist=urls.split("ue_separate_ue")
    outlist=[]
    #====request params end=================================    
    fileType = [".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"];    
    for url in urllist:
        fileExt=""
        for suffix in fileType:
            if url.endswith(suffix):
                fileExt=suffix
                break;
        if fileExt=='':
            continue
        else:
            path = str(subfolder + '/' + file_name + '.' + fileExt)
            phisypath = settings.MEDIA_ROOT[0] + path
            piccontent= urllib2.urlopen(url).read()
            picfile=open(phisypath,'wb')
            picfile.write(piccontent)
            picfile.close()
            outlist.append('/static/upload/' + subfolder + '/' + file_name + '.' + fileExt)
    outlist="ue_separate_ue".join(outlist)
    
    response=HttpResponse()
    myresponse="{'url':'%s','tip':'%s','srcUrl':'%s'}" % (outlist,'成功',urls)
    response.write(myresponse);
    return response


更新 urls.py
    url(r'^ueditor_imgup$','MyNet.app.Ueditor.views.ueditor_ImgUp'),
    url(r'^ueditor_fileup$','MyNet.app.Ueditor.views.ueditor_FileUp'),
    url(r'^ueditor_getRemoteImage$','MyNet.app.Ueditor.views.ueditor_getRemoteImage'),
    url(r'^ueditor_scrawlUp$','MyNet.app.Ueditor.views.ueditor_ScrawUp'),
    url(r'^ueditor_getMovie$','MyNet.app.Ueditor.views.ueditor_getMovie'),
    url(r'^ueditor_imageManager$','MyNet.app.Ueditor.views.ueditor_imageManager'),


更改ueditor config 配置文件
        ,imageUrl:"/ueditor_imgup"
        ,imagePath:""  
        //涂鸦图片配置区
        ,scrawlUrl:"/ueditor_scrawlUp" 
        ,scrawlPath:"" 

        //附件上传配置区
        ,fileUrl:"/ueditor_fileup" 
        ,filePath:""  

         //远程抓取配置区
        ,catcherUrl:"/ueditor_getRemoteImage" 
        ,catcherPath:""  

        //图片在线管理配置区
        ,imageManagerUrl:"/ueditor_imageManager" 
        ,imageManagerPath:""    

        //屏幕截图配置区
        ,snapscreenHost: '127.0.0.1'
        ,snapscreenServerUrl: "/ueditor_imgup" 
        ,snapscreenPath: ""
        
        //word转存配置区
       
        ,wordImageUrl:"/ueditor_imgup"
        ,wordImagePath:"" 
      

        //获取视频数据的地址
        ,getMovieUrl:"/ueditor_getMovie" 


到此为止,这两篇文章将所有需要集成的都集成了。


源码下载: django ueditor集成源码

上一篇: django 与 百度 ueditor 富文本编辑器集成之一:图片上传,文件上传
下一篇: 安装IBM FileNet 之二(pe,ce,ae 安装)

Avatar

rushfly 评论于: 2013-04-11

涂鸦中的背景代码有问题,首先action变量是通过GET传递的,其次表单中没有pictitle和fileName变量了,最后dict函数并不能转换字符串。

Avatar

rushfly 评论于: 2013-04-11

file_name = str(uuid.uuid1())
生成文件名应该放在循环中,否则造成抓的图都用一个名字保存了,结果只能保存最后一个图了。

Avatar

G清 评论于: 2012-11-13

呵呵,背景就是在模板后面的那个啊,完全版的那里有。。。不知道是不是我的STATIC路径有问题,所以远程抓图才会出错
[reply=轻舞肥羊,2012-11-13 09:52 PM]注意我的这个方法 ueditor_getRemoteImage  里面,settings.MEDIA_ROOT[0] 的配置,还有 
subfolder = time.strftime("%Y%m")    
    if not os.path.exists(settings.MEDIA_ROOT[0] + subfolder):
        os.makedirs(settings.MEDIA_ROOT[0] + subfolder)    
....
 outlist.append('/static/upload/' + subfolder + '/' + file_name + '.' + fileExt)
    outlist="ue_separate_ue".join(outlist)
这几个地方,如果你用pydev 或其他工具,你可以debug 一下,就知道了,debug 调试很重要的。[/reply]

Avatar

G清 评论于: 2012-11-12

呵呵。顺便搞一下那个背景的功能?我在火狐浏览器可以看,但是保存之后就没了。
[reply=轻舞肥羊,2012-11-13 09:29 AM]哪个背景?我知道的话,应该可以搞的。[/reply]

Avatar

G清 评论于: 2012-11-12

我用你的方法配合django-ueditor弄出这些功能了。不过奇怪得很,远程抓图在没其他图片在的时候可以正常使用,但我截了百度谷歌地图或者添加了其他图片先的话,保存远程抓图,会被之前上传的其中某个图片替换掉了,并且出现undefined
[reply=轻舞肥羊,2012-11-13 09:30 AM]我这边我测试了下,可以保存远程图片。没有undefined ,如果出现 undefined 这个是 javascript 报出来的,说明没找到东西。[/reply]
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号