def on_edit(request, page_name): """Edit the current revision of a page.""" change_note = error = '' revision = Revision.query.filter((Page.name == page_name) & ( Page.page_id == Revision.page_id)).order_by( Revision.revision_id.desc()).first() if revision is None: page = None else: page = revision.page if request.method == 'POST': text = request.form.get('text') if request.form.get('cancel') or \ revision and revision.text == text: return redirect(href(page.name)) elif not text: error = 'You cannot save empty revisions.' else: change_note = request.form.get('change_note', '') if page is None: page = Page(page_name) revision = Revision(page, text, change_note) session.commit() return redirect(href(page.name)) return Response( generate_template('action_edit.html', revision=revision, page=page, new=page is None, page_name=page_name, change_note=change_note, error=error))
def on_revert(request, page_name): """Revert an old revision.""" rev_id = request.args.get("rev", type=int) old_revision = page = None error = "No such revision" if request.method == "POST" and request.form.get("cancel"): return redirect(href(page_name)) if rev_id: old_revision = Revision.query.filter( (Revision.revision_id == rev_id) & (Revision.page_id == Page.page_id) & (Page.name == page_name) ).first() if old_revision: new_revision = ( Revision.query.filter((Revision.page_id == Page.page_id) & (Page.name == page_name)) .order_by(Revision.revision_id.desc()) .first() ) if old_revision == new_revision: error = "You tried to revert the current active " "revision." elif old_revision.text == new_revision.text: error = "There are no changes between the current " "revision and the revision you want to " "restore." else: error = "" page = old_revision.page if request.method == "POST": change_note = request.form.get("change_note", "") change_note = "revert" + (change_note and ": " + change_note or "") session.add(Revision(page, old_revision.text, change_note)) session.commit() return redirect(href(page_name)) return Response(generate_template("action_revert.html", error=error, old_revision=old_revision, page=page))
def on_edit(request, page_name): """Edit the current revision of a page.""" change_note = error = '' revision = Revision.query.filter( (Page.name == page_name) & (Page.page_id == Revision.page_id) ).order_by(Revision.revision_id.desc()).first() if revision is None: page = None else: page = revision.page if request.method == 'POST': text = request.form.get('text') if request.form.get('cancel') or \ revision and revision.text == text: return redirect(href(page.name)) elif not text: error = 'You cannot save empty revisions.' else: change_note = request.form.get('change_note', '') if page is None: page = Page(page_name) revision = Revision(page, text, change_note) session.commit() return redirect(href(page.name)) return Response(generate_template('action_edit.html', revision=revision, page=page, new=page is None, page_name=page_name, change_note=change_note, error=error ))
def on_revert(request, page_name): """Revert an old revision.""" rev_id = request.args.get('rev', type=int) old_revision = page = None error = 'No such revision' if request.method == 'POST' and request.form.get('cancel'): return redirect(href(page_name)) if rev_id: old_revision = Revision.query.filter( (Revision.revision_id == rev_id) & (Revision.page_id == Page.page_id) & (Page.name == page_name) ).first() if old_revision: new_revision = Revision.query.filter( (Revision.page_id == Page.page_id) & (Page.name == page_name) ).order_by(Revision.revision_id.desc()).first() if old_revision == new_revision: error = 'You tried to revert the current active ' \ 'revision.' elif old_revision.text == new_revision.text: error = 'There are no changes between the current ' \ 'revision and the revision you want to ' \ 'restore.' else: error = '' page = old_revision.page if request.method == 'POST': change_note = request.form.get('change_note', '') change_note = 'revert' + (change_note and ': ' + change_note or '') revision = Revision(page, old_revision.text, change_note) session.commit() return redirect(href(page_name)) return Response(generate_template('action_revert.html', error=error, old_revision=old_revision, page=page ))
def on_revert(request, page_name): """Revert an old revision.""" rev_id = request.args.get('rev', type=int) old_revision = page = None error = 'No such revision' if request.method == 'POST' and request.form.get('cancel'): return redirect(href(page_name)) if rev_id: old_revision = Revision.query.filter( (Revision.revision_id == rev_id) & (Revision.page_id == Page.page_id) & (Page.name == page_name)).first() if old_revision: new_revision = Revision.query.filter( (Revision.page_id == Page.page_id) & (Page.name == page_name)).order_by( Revision.revision_id.desc()).first() if old_revision == new_revision: error = 'You tried to revert the current active ' \ 'revision.' elif old_revision.text == new_revision.text: error = 'There are no changes between the current ' \ 'revision and the revision you want to ' \ 'restore.' else: error = '' page = old_revision.page if request.method == 'POST': change_note = request.form.get('change_note', '') change_note = 'revert' + (change_note and ': ' + change_note or '') revision = Revision(page, old_revision.text, change_note) session.commit() return redirect(href(page_name)) return Response( generate_template('action_revert.html', error=error, old_revision=old_revision, page=page))
def on_edit(request, page_name): """Edit the current revision of a page.""" change_note = error = "" revision = ( Revision.query.filter((Page.name == page_name) & (Page.page_id == Revision.page_id)) .order_by(Revision.revision_id.desc()) .first() ) if revision is None: page = None else: page = revision.page if request.method == "POST": text = request.form.get("text") if request.form.get("cancel") or revision and revision.text == text: return redirect(href(page.name)) elif not text: error = "You cannot save empty revisions." else: change_note = request.form.get("change_note", "") if page is None: page = Page(page_name) session.add(page) session.add(Revision(page, text, change_note)) session.commit() return redirect(href(page.name)) return Response( generate_template( "action_edit.html", revision=revision, page=page, new=page is None, page_name=page_name, change_note=change_note, error=error, ) )
def dispatch_request(self, environ, start_response): """Dispatch an incoming request.""" # set up all the stuff we want to have for this request. That is # creating a request object, propagating the application to the # current context and instanciating the database session. self.bind_to_context() request = Request(environ) request.db_session = Session() request.bind_to_context() # get the current action from the url and normalize the page name # which is just the request path action_name = request.args.get('action') or 'show' page_name = u'_'.join( [x for x in request.path.strip('/').split() if x]) # redirect to the Main_Page if the user requested the index if not page_name: response = redirect(href('Main_Page')) # check special pages elif page_name.startswith('Special:'): if page_name[8:] not in pages: response = page_not_found(request, page_name) else: response = pages[page_name[8:]](request) # get the callback function for the requested action from the # action module. It's "on_" + the action name. If it doesn't # exists call the missing_action method from the same module. else: action = getattr(actions, 'on_' + action_name, None) if action is None: response = actions.missing_action(request, action_name) else: response = action(request, page_name) # make sure the session is removed properly return ClosingIterator(response(environ, start_response), Session.remove)
def dispatch_request(self, environ, start_response): """Dispatch an incoming request.""" # set up all the stuff we want to have for this request. That is # creating a request object, propagating the application to the # current context and instanciating the database session. self.bind_to_context() request = Request(environ) request.bind_to_context() # get the current action from the url and normalize the page name # which is just the request path action_name = request.args.get('action') or 'show' page_name = u'_'.join([x for x in request.path.strip('/') .split() if x]) # redirect to the Main_Page if the user requested the index if not page_name: response = redirect(href('Main_Page')) # check special pages elif page_name.startswith('Special:'): if page_name[8:] not in pages: response = page_not_found(request, page_name) else: response = pages[page_name[8:]](request) # get the callback function for the requested action from the # action module. It's "on_" + the action name. If it doesn't # exists call the missing_action method from the same module. else: action = getattr(actions, 'on_' + action_name, None) if action is None: response = actions.missing_action(request, action_name) else: response = action(request, page_name) # make sure the session is removed properly return ClosingIterator(response(environ, start_response), session.remove)