-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidgets.py
More file actions
157 lines (141 loc) · 7.24 KB
/
Copy pathwidgets.py
File metadata and controls
157 lines (141 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# coding:utf-8
from django import forms
from django.conf import settings
from django.contrib.admin.widgets import AdminTextareaWidget
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.http import urlencode
import settings as USettings
from commands import *
# 修正输入的文件路径,输入路径的标准格式:abc,不需要前后置的路径符号
# 如果输入的路径参数是一个函数则执行,否则可以拉接受时间格式化,用来生成如file20121208.bmp的重命名格式
def calc_path(OutputPath, instance=None):
if callable(OutputPath):
try:
OutputPath = OutputPath(instance)
except:
OutputPath = ""
else:
try:
import datetime
OutputPath = datetime.datetime.now().strftime(OutputPath)
except:
pass
return OutputPath
# width=600, height=300, toolbars="full", imagePath="", filePath="", upload_settings={},
# settings={},command=None,event_handler=None
class UEditorWidget(forms.Textarea):
def __init__(self, attrs=None):
params = attrs.copy()
width = params.pop("width")
height = params.pop("height")
toolbars = params.pop("toolbars", "full")
imagePath = params.pop("imagePath", "")
filePath = params.pop("filePath", "")
upload_settings = params.pop("upload_settings", {})
settings = params.pop("settings", {})
command = params.pop("command", None)
event_handler = params.pop("event_handler", None)
# 扩展命令
self.command = command
self.event_handler = event_handler
# 上传路径
print filePath
self.upload_settings = upload_settings.copy()
self.upload_settings.update({
"imagePathFormat": imagePath,
"filePathFormat": filePath
})
# 保存
self._upload_settings = self.upload_settings.copy()
self.recalc_path(None)
self.ueditor_settings = {
'toolbars': toolbars,
'initialFrameWidth': width,
'initialFrameHeight': height
}
# 以下处理工具栏设置,将normal,mini等模式名称转化为工具栏配置值
if toolbars == "full":
del self.ueditor_settings['toolbars']
elif isinstance(toolbars, basestring) and toolbars in USettings.TOOLBARS_SETTINGS:
self.ueditor_settings["toolbars"] = USettings.TOOLBARS_SETTINGS[toolbars]
else:
self.ueditor_settings["toolbars"] = toolbars
# raise ValueError('toolbars should be a string defined in DjangoUeditor.settings.TOOLBARS_SETTINGS, options are full(default), besttome, mini and normal!')
self.ueditor_settings.update(settings)
super(UEditorWidget, self).__init__(attrs)
def recalc_path(self, model_inst):
"""计算上传路径,允许是function"""
try:
uSettings = self.upload_settings
if self._upload_settings.has_key("filePathFormat"):
uSettings['filePathFormat'] = calc_path(self._upload_settings['filePathFormat'], model_inst)
if self._upload_settings.has_key("imagePathFormat"):
uSettings['imagePathFormat'] = calc_path(self._upload_settings['imagePathFormat'], model_inst)
if self._upload_settings.has_key("scrawlPathFormat"):
uSettings['scrawlPathFormat'] = calc_path(self._upload_settings['scrawlPathFormat'], model_inst)
if self._upload_settings.has_key("videoPathFormat"):
uSettings['videoPathFormat'] = calc_path(self._upload_settings['videoPathFormat'], model_inst),
if self._upload_settings.has_key("snapscreenPathFormat"):
uSettings['snapscreenPathFormat'] = calc_path(self._upload_settings['snapscreenPathFormat'], model_inst)
if self._upload_settings.has_key("catcherPathFormat"):
uSettings['catcherPathFormat'] = calc_path(self._upload_settings['catcherPathFormat'], model_inst)
if self._upload_settings.has_key("imageManagerListPath"):
uSettings['imageManagerListPath'] = calc_path(self._upload_settings['imageManagerListPath'], model_inst)
if self._upload_settings.has_key("fileManagerListPath"):
uSettings['fileManagerListPath'] = calc_path(self._upload_settings['fileManagerListPath'], model_inst)
# 设置默认值,未指定涂鸦、截图、远程抓图、图片目录时,默认均等于imagePath
if uSettings['imagePathFormat'] != "":
uSettings['scrawlPathFormat'] = uSettings['scrawlPathFormat'] if self._upload_settings.has_key(
"scrawlPathFormat") else uSettings['imagePathFormat']
uSettings['videoPathFormat'] = uSettings['videoPathFormat'] if self._upload_settings.has_key(
"videoPathFormat") else uSettings['imagePathFormat']
uSettings['snapscreenPathFormat'] = uSettings['snapscreenPathFormat'] if self._upload_settings.has_key(
"snapscreenPathFormat") else uSettings['imagePathFormat']
uSettings['catcherPathFormat'] = uSettings['catcherPathFormat'] if self._upload_settings.has_key(
"catcherPathFormat") else uSettings['imagePathFormat']
uSettings['imageManagerListPath'] = uSettings['imageManagerListPath'] if self._upload_settings.has_key(
"imageManagerListPath") else uSettings['imagePathFormat']
if uSettings['filePathFormat'] != "":
uSettings['fileManagerListPath'] = uSettings['fileManagerListPath'] if self._upload_settings.has_key(
"fileManagerListPath") else uSettings['filePathFormat']
except:
pass
def render(self, name, value, attrs=None):
if value is None: value = ''
# 传入模板的参数
editor_id = "id_%s" % name.replace("-", "_")
uSettings = {
"name": name,
"id": editor_id,
"value": value
}
if isinstance(self.command, list):
cmdjs = ""
if isinstance(self.command, list):
for cmd in self.command:
cmdjs = cmdjs + cmd.render(editor_id)
else:
cmdis = self.command.render(editor_id)
uSettings["commands"] = cmdjs
uSettings["settings"] = self.ueditor_settings.copy()
uSettings["settings"].update({
"serverUrl": "/ueditor/controller/?%s" % urlencode(self._upload_settings)
})
# 生成事件侦听
if self.event_handler:
uSettings["bindEvents"] = self.event_handler.render(editor_id)
context = {
'UEditor': uSettings,
'STATIC_URL': settings.STATIC_URL,
'STATIC_ROOT': settings.STATIC_ROOT,
'MEDIA_URL': settings.MEDIA_URL,
'MEDIA_ROOT': settings.MEDIA_ROOT
}
return mark_safe(render_to_string('ueditor.html', context))
class Media:
js = ("ueditor/ueditor.config.js",
"ueditor/ueditor.all.min.js")
class AdminUEditorWidget(AdminTextareaWidget, UEditorWidget):
def __init__(self, **kwargs):
super(AdminUEditorWidget, self).__init__(**kwargs)