comparison MoinMoin/action/AttachFile.py @ 4201:40acd13fb3d6

Changed form access to MultiDict forms
author Florian Krupicka <florian.krupicka@googlemail.com>
date Thu, 03 Jul 2008 23:36:48 +0200
parents fc20a076aad0
children d9de4fa12f23
comparison
equal deleted inserted replaced
4200:c21991eed9c9 4201:40acd13fb3d6
249 Return `(pagename, None, None)` if an error occurs. 249 Return `(pagename, None, None)` if an error occurs.
250 """ 250 """
251 _ = request.getText 251 _ = request.getText
252 252
253 error = None 253 error = None
254 if not request.form.get('target', [''])[0]: 254 if not request.form.get('target'):
255 error = _("Filename of attachment not specified!") 255 error = _("Filename of attachment not specified!")
256 else: 256 else:
257 filename = wikiutil.taintfilename(request.form['target'][0]) 257 filename = wikiutil.taintfilename(request.form['target'])
258 fpath = getFilename(request, pagename, filename) 258 fpath = getFilename(request, pagename, filename)
259 259
260 if os.path.isfile(fpath): 260 if os.path.isfile(fpath):
261 return (pagename, filename, fpath) 261 return (pagename, filename, fpath)
262 error = _("Attachment '%(filename)s' does not exist!") % {'filename': filename} 262 error = _("Attachment '%(filename)s' does not exist!") % {'filename': filename}
418 def send_hotdraw(pagename, request): 418 def send_hotdraw(pagename, request):
419 _ = request.getText 419 _ = request.getText
420 420
421 now = time.time() 421 now = time.time()
422 pubpath = request.cfg.url_prefix_static + "/applets/TWikiDrawPlugin" 422 pubpath = request.cfg.url_prefix_static + "/applets/TWikiDrawPlugin"
423 basename = request.form['drawing'][0] 423 basename = request.form['drawing']
424 drawpath = getAttachUrl(pagename, basename + '.draw', request, escaped=1) 424 drawpath = getAttachUrl(pagename, basename + '.draw', request, escaped=1)
425 pngpath = getAttachUrl(pagename, basename + '.png', request, escaped=1) 425 pngpath = getAttachUrl(pagename, basename + '.png', request, escaped=1)
426 pagelink = attachUrl(request, pagename, '', action=action_name, ts=now) 426 pagelink = attachUrl(request, pagename, '', action=action_name, ts=now)
427 helplink = Page(request, "HelpOnActions/AttachFile").url(request) 427 helplink = Page(request, "HelpOnActions/AttachFile").url(request)
428 savelink = attachUrl(request, pagename, '', action=action_name, do='savedrawing') 428 savelink = attachUrl(request, pagename, '', action=action_name, do='savedrawing')
429 #savelink = Page(request, pagename).url(request) # XXX include target filename param here for twisted 429 #savelink = Page(request, pagename).url(request) # XXX include target filename param here for twisted
430 # request, {'savename': request.form['drawing'][0]+'.draw'} 430 # request, {'savename': request.form['drawing']+'.draw'}
431 #savelink = '/cgi-bin/dumpform.bat' 431 #savelink = '/cgi-bin/dumpform.bat'
432 432
433 timestamp = '&amp;ts=%s' % now 433 timestamp = '&amp;ts=%s' % now
434 434
435 request.write('<h2>' + _("Edit drawing") + '</h2>') 435 request.write('<h2>' + _("Edit drawing") + '</h2>')
492 'baseurl': request.script_root, 492 'baseurl': request.script_root,
493 'pagename': wikiutil.quoteWikinameURL(pagename), 493 'pagename': wikiutil.quoteWikinameURL(pagename),
494 'action_name': action_name, 494 'action_name': action_name,
495 'upload_label_file': _('File to upload'), 495 'upload_label_file': _('File to upload'),
496 'upload_label_rename': _('Rename to'), 496 'upload_label_rename': _('Rename to'),
497 'rename': request.form.get('rename', [''])[0], 497 'rename': request.form.get('rename', ''),
498 'upload_label_overwrite': _('Overwrite existing attachment of same name'), 498 'upload_label_overwrite': _('Overwrite existing attachment of same name'),
499 'overwrite_checked': ('', 'checked')[request.form.get('overwrite', ['0'])[0] == '1'], 499 'overwrite_checked': ('', 'checked')[request.form.get('overwrite', '0') == '1'],
500 'upload_button': _('Upload'), 500 'upload_button': _('Upload'),
501 'textcha': TextCha(request).render(), 501 'textcha': TextCha(request).render(),
502 }) 502 })
503 503
504 request.write('<h2>' + _("Attached Files") + '</h2>') 504 request.write('<h2>' + _("Attached Files") + '</h2>')
505 request.write(_get_filelist(request, pagename)) 505 request.write(_get_filelist(request, pagename))
506 506
507 if not writeable: 507 if not writeable:
508 request.write('<p>%s</p>' % _('You are not allowed to attach a file to this page.')) 508 request.write('<p>%s</p>' % _('You are not allowed to attach a file to this page.'))
509 509
510 if writeable and request.form.get('drawing', [None])[0]: 510 if writeable and request.form.get('drawing'):
511 send_hotdraw(pagename, request) 511 send_hotdraw(pagename, request)
512 512
513 513
514 ############################################################################# 514 #############################################################################
515 ### Web interface for file upload, viewing and deletion 515 ### Web interface for file upload, viewing and deletion
517 517
518 def execute(pagename, request): 518 def execute(pagename, request):
519 """ Main dispatcher for the 'AttachFile' action. """ 519 """ Main dispatcher for the 'AttachFile' action. """
520 _ = request.getText 520 _ = request.getText
521 521
522 do = request.form.get('do', ['upload_form']) 522 do = request.form.get('do', 'upload_form')
523 handler = globals().get('_do_%s' % do[0]) 523 handler = globals().get('_do_%s' % do)
524 if handler: 524 if handler:
525 msg = handler(pagename, request) 525 msg = handler(pagename, request)
526 else: 526 else:
527 msg = _('Unsupported AttachFile sub-action: %s') % (wikiutil.escape(do[0]), ) 527 msg = _('Unsupported AttachFile sub-action: %s') % (wikiutil.escape(do[0]), )
528 if msg: 528 if msg:
564 # but it could be extended to more/all attachment write access 564 # but it could be extended to more/all attachment write access
565 if not TextCha(request).check_answer_from_form(): 565 if not TextCha(request).check_answer_from_form():
566 return _('TextCha: Wrong answer! Go back and try again...') 566 return _('TextCha: Wrong answer! Go back and try again...')
567 567
568 form = request.form 568 form = request.form
569 overwrite = form.get('overwrite', [u'0'])[0] 569 overwrite = form.get('overwrite', u'0')
570 try: 570 try:
571 overwrite = int(overwrite) 571 overwrite = int(overwrite)
572 except: 572 except:
573 overwrite = 0 573 overwrite = 0
574 574
577 577
578 if overwrite and not request.user.may.delete(pagename): 578 if overwrite and not request.user.may.delete(pagename):
579 return _('You are not allowed to overwrite a file attachment of this page.') 579 return _('You are not allowed to overwrite a file attachment of this page.')
580 580
581 filename = form.get('file__filename__') 581 filename = form.get('file__filename__')
582 rename = form.get('rename', [u''])[0].strip() 582 rename = form.get('rename', u'').strip()
583 if rename: 583 if rename:
584 target = rename 584 target = rename
585 else: 585 else:
586 target = filename 586 target = filename
587 587
590 590
591 if not target: 591 if not target:
592 return _("Filename of attachment not specified!") 592 return _("Filename of attachment not specified!")
593 593
594 # get file content 594 # get file content
595 filecontent = request.form.get('file', [None])[0] 595 filecontent = request.form.get('file')
596 if filecontent is None: 596 if filecontent is None:
597 # This might happen when trying to upload file names 597 # This might happen when trying to upload file names
598 # with non-ascii characters on Safari. 598 # with non-ascii characters on Safari.
599 return _("No file content. Delete non ASCII characters from the file name and try again.") 599 return _("No file content. Delete non ASCII characters from the file name and try again.")
600 600
616 _ = request.getText 616 _ = request.getText
617 617
618 if not request.user.may.write(pagename): 618 if not request.user.may.write(pagename):
619 return _('You are not allowed to save a drawing on this page.') 619 return _('You are not allowed to save a drawing on this page.')
620 620
621 filename = request.form['filename'][0] 621 filename = request.form['filename']
622 filecontent = request.form['filepath'][0] 622 filecontent = request.form['filepath']
623 623
624 basepath, basename = os.path.split(filename) 624 basepath, basename = os.path.split(filename)
625 basename, ext = os.path.splitext(basename) 625 basename, ext = os.path.splitext(basename)
626 626
627 # get directory, and possibly create it 627 # get directory, and possibly create it
718 def _do_attachment_move(pagename, request): 718 def _do_attachment_move(pagename, request):
719 _ = request.getText 719 _ = request.getText
720 720
721 if 'cancel' in request.form: 721 if 'cancel' in request.form:
722 return _('Move aborted!') 722 return _('Move aborted!')
723 if not wikiutil.checkTicket(request, request.form['ticket'][0]): 723 if not wikiutil.checkTicket(request, request.form['ticket']):
724 return _('Please use the interactive user interface to move attachments!') 724 return _('Please use the interactive user interface to move attachments!')
725 if not request.user.may.delete(pagename): 725 if not request.user.may.delete(pagename):
726 return _('You are not allowed to move attachments from this page.') 726 return _('You are not allowed to move attachments from this page.')
727 727
728 if 'newpagename' in request.form: 728 if 'newpagename' in request.form:
729 new_pagename = request.form.get('newpagename')[0] 729 new_pagename = request.form.get('newpagename')
730 else: 730 else:
731 upload_form(pagename, request, msg=_("Move aborted because new page name is empty.")) 731 upload_form(pagename, request, msg=_("Move aborted because new page name is empty."))
732 if 'newattachmentname' in request.form: 732 if 'newattachmentname' in request.form:
733 new_attachment = request.form.get('newattachmentname')[0] 733 new_attachment = request.form.get('newattachmentname')
734 if new_attachment != wikiutil.taintfilename(new_attachment): 734 if new_attachment != wikiutil.taintfilename(new_attachment):
735 upload_form(pagename, request, msg=_("Please use a valid filename for attachment '%(filename)s'.") % { 735 upload_form(pagename, request, msg=_("Please use a valid filename for attachment '%(filename)s'.") % {
736 'filename': new_attachment}) 736 'filename': new_attachment})
737 return 737 return
738 else: 738 else:
739 upload_form(pagename, request, msg=_("Move aborted because new attachment name is empty.")) 739 upload_form(pagename, request, msg=_("Move aborted because new attachment name is empty."))
740 740
741 attachment = request.form.get('oldattachmentname')[0] 741 attachment = request.form.get('oldattachmentname')
742 move_file(request, pagename, new_pagename, attachment, new_attachment) 742 move_file(request, pagename, new_pagename, attachment, new_attachment)
743 743
744 744
745 def _do_move(pagename, request): 745 def _do_move(pagename, request):
746 _ = request.getText 746 _ = request.getText