-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
65 lines (56 loc) · 2.02 KB
/
forms.py
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
# coding: utf-8
from django import forms
from widgets import UEditorWidget
from DjangoUeditor.models import UEditorField as ModelUEditorField
class UEditorField(forms.CharField):
def __init__(
self,
label,
width=600,
height=300,
toolbars="full",
imagePath="",
filePath="",
upload_settings={},
settings={},
command=None,
event_handler=None,
*args,
**kwargs):
uSettings = locals().copy()
del uSettings["self"], uSettings[
"label"], uSettings["args"], uSettings["kwargs"]
kwargs["widget"] = UEditorWidget(attrs=uSettings)
kwargs["label"] = label
super(UEditorField, self).__init__(*args, **kwargs)
# def UpdateUploadPath(model_form, model_inst=None):
# """ 遍历model字段,如果是UEditorField则需要重新计算路径 """
# if model_inst is not None:
# try:
# for field in model_inst._meta.fields:
# if isinstance(field, ModelUEditorField):
# model_form.__getitem__(
# field.name).field.widget.recalc_path(model_inst)
# except:
# pass
# 修改前
def UpdateUploadPath(model_form, model_inst=None):
""" 遍历model字段,如果是UEditorField则需要重新计算路径 """
if not model_inst:
try:
for field in model_inst._meta.fields:
if isinstance(field, ModelUEditorField):
model_form.__getitem__(
field.name).field.widget.recalc_path(model_inst)
except:
pass
class UEditorModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(UEditorModelForm, self).__init__(*args, **kwargs)
try:
if "instance" in kwargs:
UpdateUploadPath(self, kwargs["instance"])
else:
UpdateUploadPath(self, None)
except Exception:
pass