upload_3.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import urllib
  2. from skeletonz.modules.template import PluginTemplate
  3. from skeletonz.model import CMSModel
  4. import filter
  5. from skeletonz.mylib.converters import makeLinkAble
  6. from skeletonz.mylib.amicache import AmiCache
  7. from amilib.template import render
  8. from skeletonz.server import getConfig, getFormatManager
  9. from amilib.amiweb import amiweb
  10. import model
  11. class Upload:
  12. def __init__(self, handler):
  13. """If your mapping is to /files/, then handler='files'"""
  14. template = PluginTemplate()
  15. self.header = template.getHeader()
  16. self.footer = template.getFooter()
  17. self.handler = handler
  18. @amiweb.expose
  19. def showUpload(self, ftype, id=0, linkonly=0):
  20. file = model.Files.getFileById(id)
  21. ns = {'site': self,
  22. 'BASE_URL': getConfig().BASE_URL,
  23. 'file': file,
  24. 'page_id': amiweb.session()['current_page_id'],
  25. 'ftype': ftype,
  26. 'linkonly': linkonly}
  27. return render("skeletonz/plugins/upload/view/upload.tmpl", ns)
  28. @amiweb.expose
  29. def deleteFile(self, file_id, ftype, linkonly=0):
  30. ident = model.Files.deleteFileById(file_id)
  31. page_obj = CMSModel.Pages.getPageById(amiweb.session()['current_page_id'])
  32. AmiCache.expireCurrentPage()
  33. return self.getEditHTML(ftype, ident, linkonly)
  34. @amiweb.expose
  35. def fixFilename(self, action, new_name):
  36. ns = amiweb.session()['upload_ns']
  37. if action == 'change':
  38. if model.Files.countFilesWithFilename(new_name):
  39. ns['filename'] = filename
  40. ns['new_filename'] = model.Files.newFilename(filename)
  41. return render("skeletonz/plugins/upload/view/upload_already_found.tmpl", ns)
  42. ns['filename'] = new_name
  43. #Else let is stay the same
  44. del amiweb.session()['upload_ns']
  45. return self.uploadComplete(ns)
  46. @amiweb.expose
  47. def uploadFile(self, file, ident, ftype, id=0, linkonly=0):
  48. bdata = ""
  49. for l in file.file.readlines():
  50. bdata += l
  51. filename = file.filename.split("\\")
  52. filename = filename[-1:][0]
  53. ns = {'site': self,
  54. 'BASE_URL': getConfig().BASE_URL,
  55. 'ident': ident,
  56. 'id': id,
  57. 'filename': filename,
  58. 'ident_id': makeLinkAble(ident),
  59. 'linkonly': linkonly,
  60. 'ftype': ftype}
  61. ns['data'] = bdata
  62. if model.Files.countFilesWithFilename(filename):
  63. ns['new_filename'] = model.Files.newFilename(filename)
  64. amiweb.session()['upload_ns'] = ns
  65. return render("skeletonz/plugins/upload/view/upload_already_found.tmpl", ns)
  66. return self.uploadComplete(ns)
  67. def uploadComplete(self, ns):
  68. id = ns['id']
  69. bdata = ns['data']
  70. ftype = ns['ftype']
  71. ident = ns['ident']
  72. linkonly = ns['linkonly']
  73. filename = ns['filename']
  74. if id != 0:
  75. file_obj = model.Files.uploadFileEdit(id, filename, ftype, bdata)
  76. else:
  77. file_obj = model.Files.uploadFile(ident, filename, ftype, bdata)
  78. ns['filename'] = file_obj.getFilename()
  79. new_html = self.getEditHTML(ftype, ident, linkonly)
  80. new_html = new_html.replace("'", "\\'")
  81. ns['new_html'] = new_html
  82. AmiCache.expireCurrentPage()
  83. return render("skeletonz/plugins/upload/view/upload_complete.tmpl", ns)
  84. @amiweb.expose
  85. def getEditHTML(self, type, ident, linkonly=0):
  86. if type == "skfile":
  87. args = getFormatManager().getPluginArguments('file', ident)
  88. return filter.wikiFiles(args, True, None)
  89. if type == "skimg":
  90. args = getFormatManager().getPluginArguments('image', ident)
  91. return filter.wikiImages(args, True, None)
  92. if type == "personnelimage":
  93. args = getFormatManager().getPluginArguments('personnelimage', ident)
  94. return filter.personnelWikiImages(args, True, None)