widgets.py 6.6 KB

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