comparison data/plugin/action/submitattachment.py @ 622:12465aa315be

add code from ActionMarket/FormSubmit (form_submit-1.7.tar.gz) - outdated code for moin 1.7/1.8
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 25 Mar 2013 17:09:15 +0100
parents
children 5f499a634857
comparison
equal deleted inserted replaced
621:a8d1eb833705 622:12465aa315be
1 # -*- coding: iso-8859-1 -*-
2
3 """
4 MoinMoin - submitattachment Action
5
6 Attach file into standard MoinMoin attachment folder or defined in
7 targetpage CGI parameter.
8
9 If attachment with same name already exists, numeric index
10 is added to new attachment. (file_1.txt, file_2.txt, ...)
11
12 @copyright: 2008 by Peter Bodi <petrdll@centrum.sk>
13 @license: GNU GPL, see COPYING for details.
14 """
15
16 import os, sys, copy
17
18 from MoinMoin import wikiutil
19 from MoinMoin.Page import Page
20 from MoinMoin.action import AttachFile
21
22 from submitbase import SubmitBase, SubmitValidationError
23
24 def execute(pagename, request):
25 submitattachment(pagename, request).render()
26
27 class submitattachment(SubmitBase):
28
29 def rewrite_filename(self, filename):
30 """ Rewrites filename if there already exists attachment with the same name
31 """
32 attachFile = filename
33 i = 0
34 while AttachFile.exists(self.request, self.targetpage, attachFile):
35 attachFile = self.attachFile
36 attachFileSplit = attachFile.split(".", 2)
37 ext = attachFileSplit.pop()
38 name = '.'.join(attachFileSplit)
39 attachFile = "%(name)s_%(index)d.%(extension)s" % {'name':name, 'index':i, 'extension':ext}
40 i += 1
41
42 filename = attachFile
43
44 return filename
45
46 def validate(self):
47 """ Evaluates whethere valid file was specified """
48 SubmitBase.validate(self)
49
50 if not self.attachFile:
51 self.msg = self._("File was not specified")
52 raise SubmitValidationError(self.msg)
53
54 if not self.attachContent:
55 self.msg = self._("Invalid file '%(file)s'") % {'file' : self.attachFile}
56 raise SubmitValidationError(self.msg)
57
58 def sanitize(self):
59 SubmitBase.sanitize(self)
60 self.request.form['file__filename__'] = self.attachFile = self.rewrite_filename(self.attachFile)
61
62 def submit(self):
63 AttachFile.add_attachment(self.request, self.targetpage, self.attachFile, self.attachContent, 0)
64
65
66
67