Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update views.py #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DjangoUeditor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def GenerateRndFilename(filename):
import random
from os.path import splitext
f_name,f_ext=splitext(filename)
if type_ext is not None:
f_ext=type_ext
return "%s_%s%s%s" % (f_name, datetime.datetime.now().strftime("%Y%m%d_%H%M%S_"),random.randrange(10,99),f_ext)

#文件大小类
Expand Down Expand Up @@ -176,4 +178,4 @@ def MadeUeditorOptions(width=600,height=300,plugins=(),toolbars="normal",filePat
uOptions['options']=options
uOptions['width']=width
uOptions['height']=height
return uOptions
return uOptions
37 changes: 28 additions & 9 deletions DjangoUeditor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils import simplejson
from utils import GenerateRndFilename
from django.views.decorators.csrf import csrf_exempt
from django.core.files.storage import default_storage

#保存上传的文件
def SaveUploadFile(PostFile,FilePath):
Expand All @@ -19,22 +20,38 @@ def SaveUploadFile(PostFile,FilePath):
return u"SUCCESS"

#上传附件
#addnew
mime2type = {}
mime2type["image/gif"]="gif"
mime2type["image/jpeg"]="jpg"
mime2type["image/png"]="png"
mime2type["application/pdf"]="pdf"
@csrf_exempt
def UploadFile(request,uploadtype,uploadpath):
if not request.method=="POST": return HttpResponse(simplejson.dumps( u"{'state:'ERROR'}"),mimetype="Application/javascript")
state="SUCCESS"
file=request.FILES.get("upfile",None)
if "upfile" in request.FILES:
f = request.FILES["upfile"]
else:
return HttpResponse(simplejson.dumps( u"{'state:'ERROR'}"),mimetype="Application/javascript")

#如果没有提交upfile则返回错误
if file is None:return HttpResponse(simplejson.dumps(u"{'state:'ERROR'}") ,mimetype="Application/javascript")
#取得上传的文件的原始名称
original_name,original_ext=file.name.split('.')
#类型检验
if uploadtype=="image" or uploadtype=="scrawlbg":
allow_type= USettings.UEditorSettings["images_upload"]['allow_type']
allow_type = USettings.UEditorSettings["images_upload"]['allow_type']
else:
allow_type= USettings.UEditorSettings["files_upload"]['allow_type']
if not original_ext in allow_type:
state=u"服务器不允许上传%s类型的文件。" % original_ext
allow_type = USettings.UEditorSettings["files_upload"]['allow_type']

if not file.content_type in allow_type:
allow_type_exts = [mime2type[x] for x in USettings.UEditorSettings["images_upload"]['allow_type']]
allow_type_str = ",".join(allow_type_exts)
state=u"服务器只允许上传%s类型的文件。" % allow_type_str

if not file.content_type in allow_type:
state=u"服务器只允许上传%s类型的文件。" % type_str
#大小检验
max_size=USettings.UEditorSettings["images_upload"]['max_size']
if max_size!=0:
Expand All @@ -43,15 +60,16 @@ def UploadFile(request,uploadtype,uploadpath):
if file.size>MF.size:
state=u"上传文件大小不允许超过%s。" % MF.FriendValue
#检测保存路径是否存在,如果不存在则需要创建
OutputPath=os.path.join(USettings.gSettings.MEDIA_ROOT,os.path.dirname(uploadpath)).replace("//","/")
if not os.path.exists(OutputPath):
os.makedirs(OutputPath)
#OutputPath=os.path.join(USettings.gSettings.MEDIA_ROOT,os.path.dirname(uploadpath)).replace("//","/")
#if not os.path.exists(OutputPath):
# os.makedirs(OutputPath)
#要保存的文件名格式使用"原文件名_当前时间.扩展名"
OutputFile=GenerateRndFilename(file.name)
#所有检测完成后写入文件
if state=="SUCCESS":
#保存到文件中
state=SaveUploadFile(file,os.path.join(OutputPath,OutputFile))
#state=SaveUploadFile(file,os.path.join(OutputPath,OutputFile))
default_storage.save(os.path.join(uploadpath,OutputFile),upfile)
#返回数据

if uploadtype=="image" or uploadtype=="scrawlbg":
Expand All @@ -74,6 +92,7 @@ def UploadFile(request,uploadtype,uploadpath):
return HttpResponse(simplejson.dumps(rInfo),mimetype="application/javascript")

#图片文件管理器
@csrf_exempt #如果不加这个,在开启了CSRF功能的站点上,无妨正常访问图片管理器
def ImageManager(request,imagepath):
if not request.method!="GET": return HttpResponse(simplejson.dumps(u"{'state:'ERROR'}") ,mimetype="Application/javascript")
#取得动作
Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
这个项目在集成的时候我也做了一些修改,但是因为和自己的项目结合比较紧密,所以就不share了。
说一个问题:这个项目所带的Ueditor版本在某些浏览器下有中文输入困难的问题,所以碰到此问题的同学请把Ueditor本身升级到1.6.1以后的版本升级方法:
简单说一下,怎样手动升级静态包:
1、官方目前提供的完整包里没有完整uetior_all_min.js。不过你到官方地址:http://ueditor.baidu.com/ueditor/ueditor.all.min.js 这个可以用。
2、themes/default的_css目录得改成css。
-----------------------------------------------
本模块帮助在Django应用中集成百度Ueditor HTML编辑器,Ueditor HTML编辑器是百度开源的HTML编辑器,

*2013-2-22*
Expand Down Expand Up @@ -155,4 +161,4 @@
**目前暂时不支持ueditor的插件
**别忘记了运行collectstatic命令,该命令可以将ueditor的所有文件复制到{{STATIC_ROOT}}文件夹里面
**Django默认开启了CSRF中间件,因此如果你的表单没有加入{% csrf_token %},那么当您上传文件和图片时会失败