forms.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #coding: utf-8
  2. from django import forms
  3. from widgets import UEditorWidget
  4. from utils import MadeUeditorOptions
  5. class UEditorField(forms.CharField):
  6. def __init__(self,label,width=600,height=300,plugins=(),toolbars="normal",filePath="",imagePath="",scrawlPath="",imageManagerPath="",css="",options={}, *args, **kwargs):
  7. uOptions=MadeUeditorOptions(width,height,plugins,toolbars,filePath,imagePath,scrawlPath,imageManagerPath,css,options)
  8. kwargs["widget"]=UEditorWidget(**uOptions)
  9. kwargs["label"]=label
  10. super(UEditorField,self).__init__( *args, **kwargs)
  11. def UpdateUploadPath(widget,model_inst=None):
  12. try:
  13. from DjangoUeditor.models import UEditorField as ModelUEditorField
  14. for field in model_inst._meta.fields:
  15. if isinstance(field, ModelUEditorField):
  16. if callable(field.ueditor_options["O_imagePath"]):
  17. newPath=field.ueditor_options["O_imagePath"](model_inst)
  18. widget.__getitem__(field.name).field.widget.ueditor_options["imagePath"] =newPath
  19. if field.ueditor_options["O_imageManagerPath"]=="":widget.__getitem__(field.name).field.widget.ueditor_options["imageManagerPath"] =newPath
  20. if field.ueditor_options["O_scrawlPath"]=="":widget.__getitem__(field.name).field.widget.ueditor_options["scrawlPath"] =newPath
  21. if callable(field.ueditor_options["O_filePath"]):
  22. widget.__getitem__(field.name).field.widget.ueditor_options["filePath"] =field.ueditor_options["O_filePath"](model_inst)
  23. if callable(field.ueditor_options["O_imageManagerPath"]):
  24. widget.__getitem__(field.name).field.widget.ueditor_options["imageManagerPath"] =field.ueditor_options["O_imageManagerPath"](model_inst)
  25. if callable(field.ueditor_options["O_scrawlPath"]):
  26. widget.__getitem__(field.name).field.widget.ueditor_options["scrawlPath"] =field.ueditor_options["O_scrawlPath"](model_inst)
  27. except:
  28. pass
  29. class UEditorModelForm(forms.ModelForm):
  30. def __init__(self,*args,**kwargs):
  31. super(UEditorModelForm,self).__init__(*args,**kwargs)
  32. try:
  33. if kwargs.has_key("instance"):
  34. UpdateUploadPath(self,kwargs["instance"])
  35. else:
  36. UpdateUploadPath(self,None)
  37. except Exception:
  38. pass