Back to index

moin  1.9.0~rc2
Functions
MoinMoin.action.edit Namespace Reference

Functions

def execute

Function Documentation

def MoinMoin.action.edit.execute (   pagename,
  request 
)
edit a page 

Definition at line 15 of file edit.py.

00015 
00016 def execute(pagename, request):
00017     """ edit a page """
00018     _ = request.getText
00019 
00020     if 'button_preview' in request.form and 'button_spellcheck' in request.form:
00021         # multiple buttons pressed at once? must be some spammer/bot
00022         check_surge_protect(request, kick=True) # get rid of him
00023         return
00024 
00025     if not request.user.may.write(pagename):
00026         page = wikiutil.getLocalizedPage(request, 'PermissionDeniedPage')
00027         page.body = _('You are not allowed to edit this page.')
00028         page.page_name = pagename
00029         page.send_page(send_special=True)
00030         return
00031 
00032     valideditors = ['text', 'gui', ]
00033     editor = ''
00034     if request.user.valid:
00035         editor = request.user.editor_default
00036     if editor not in valideditors:
00037         editor = request.cfg.editor_default
00038 
00039     editorparam = request.values.get('editor', editor)
00040     if editorparam == "guipossible":
00041         lasteditor = editor
00042     elif editorparam == "textonly":
00043         editor = lasteditor = 'text'
00044     else:
00045         editor = lasteditor = editorparam
00046 
00047     if request.cfg.editor_force:
00048         editor = request.cfg.editor_default
00049 
00050     # if it is still nothing valid, we just use the text editor
00051     if editor not in valideditors:
00052         editor = 'text'
00053 
00054     rev = request.rev or 0
00055     savetext = request.form.get('savetext')
00056     comment = request.form.get('comment', u'')
00057     category = request.form.get('category')
00058     rstrip = int(request.form.get('rstrip', '0'))
00059     trivial = int(request.form.get('trivial', '0'))
00060 
00061     if 'button_switch' in request.form:
00062         if editor == 'text':
00063             editor = 'gui'
00064         else: # 'gui'
00065             editor = 'text'
00066 
00067     # load right editor class
00068     if editor == 'gui':
00069         from MoinMoin.PageGraphicalEditor import PageGraphicalEditor
00070         pg = PageGraphicalEditor(request, pagename)
00071     else: # 'text'
00072         from MoinMoin.PageEditor import PageEditor
00073         pg = PageEditor(request, pagename)
00074 
00075     # is invoked without savetext start editing
00076     if savetext is None or 'button_load_draft' in request.form:
00077         pg.sendEditor()
00078         return
00079 
00080     # did user hit cancel button?
00081     cancelled = 'button_cancel' in request.form
00082 
00083     if request.cfg.edit_ticketing:
00084         ticket = request.form.get('ticket', '')
00085         if not wikiutil.checkTicket(request, ticket):
00086             request.theme.add_msg(_('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': 'edit' }, "error")
00087             pg.send_page()
00088             return
00089 
00090     from MoinMoin.error import ConvertError
00091     try:
00092         if lasteditor == 'gui':
00093             # convert input from Graphical editor
00094             format = request.form.get('format', 'wiki')
00095             if format == 'wiki':
00096                 converter_name = 'text_html_text_moin_wiki'
00097             else:
00098                 converter_name = 'undefined' # XXX we don't have other converters yet
00099             convert = wikiutil.importPlugin(request.cfg, "converter", converter_name, 'convert')
00100             savetext = convert(request, pagename, savetext)
00101 
00102         # IMPORTANT: normalize text from the form. This should be done in
00103         # one place before we manipulate the text.
00104         savetext = pg.normalizeText(savetext, stripspaces=rstrip)
00105     except ConvertError:
00106         # we don't want to throw an exception if user cancelled anyway
00107         if not cancelled:
00108             raise
00109 
00110     if cancelled:
00111         pg.sendCancel(savetext or "", rev)
00112         pagedir = pg.getPagePath(check_create=0)
00113         import os
00114         if not os.listdir(pagedir):
00115             os.removedirs(pagedir)
00116         return
00117 
00118     comment = wikiutil.clean_input(comment)
00119 
00120     # Add category
00121 
00122     # TODO: this code does not work with extended links, and is doing
00123     # things behind your back, and in general not needed. Either we have
00124     # a full interface for categories (add, delete) or just add them by
00125     # markup.
00126 
00127     if category and category != _('<No addition>'): # opera 8.5 needs this
00128         # strip trailing whitespace
00129         savetext = savetext.rstrip()
00130 
00131         # Add category separator if last non-empty line contains
00132         # non-categories.
00133         lines = [line for line in savetext.splitlines() if line]
00134         if lines:
00135 
00136             #TODO: this code is broken, will not work for extended links
00137             #categories, e.g ["category hebrew"]
00138             categories = lines[-1].split()
00139 
00140             if categories:
00141                 confirmed = wikiutil.filterCategoryPages(request, categories)
00142                 if len(confirmed) < len(categories):
00143                     # This was not a categories line, add separator
00144                     savetext += u'\n----\n'
00145 
00146         # Add new category
00147         if savetext and savetext[-1] != u'\n':
00148             savetext += ' '
00149         savetext += category + u'\n' # Should end with newline!
00150 
00151     # Preview, spellcheck or spellcheck add new words
00152     if ('button_preview' in request.form or
00153         'button_spellcheck' in request.form or
00154         'button_newwords' in request.form):
00155         pg.sendEditor(preview=savetext, comment=comment)
00156 
00157     # Preview with mode switch
00158     elif 'button_switch' in request.form:
00159         pg.sendEditor(preview=savetext, comment=comment, staytop=1)
00160 
00161     # Save new text
00162     else:
00163         try:
00164             from MoinMoin.security.textcha import TextCha
00165             if not TextCha(request).check_answer_from_form():
00166                 raise pg.SaveError(_('TextCha: Wrong answer! Go back and try again...'))
00167             savemsg = pg.saveText(savetext, rev, trivial=trivial, comment=comment)
00168         except pg.EditConflict, e:
00169             msg = e.message
00170 
00171             # Handle conflict and send editor
00172             pg.set_raw_body(savetext, modified=1)
00173 
00174             pg.mergeEditConflict(rev)
00175             # We don't send preview when we do merge conflict
00176             pg.sendEditor(msg=msg, comment=comment)
00177             return
00178 
00179         except pg.SaveError, msg:
00180             # msg contains a unicode string
00181             savemsg = unicode(msg)
00182 
00183         # Send new page after save or after unsuccessful conflict merge.
00184         request.reset()
00185         pg = Page(request, pagename)
00186 
00187         # sets revision number to default for further actions
00188         request.rev = 0
00189         request.theme.add_msg(savemsg, "info")
00190         pg.send_page()

Here is the call graph for this function: