How to redefine project URL?

I use flask-mwoauth to create a simple application in Flask using OAuth authentication in Mediawiki (and in particular on Wikipedia).

flask-mwoauth is a project that provides some convenient methods for interacting with the Mediawiki: OAuth extensions and adds the following URIs:

  • /login - handshakes OAuth and returns the user /
    • /login?next=/someurl will return the user to /someurl
  • /logout - clears user access tokens
    • /logout?next=/someurl will return the user to /someurl
  • /oauth-callback - callback from MW to end handshake

The key and secret of the OAuth user are stored in the session.

I would like to be able to create custom responses for some of these custom URIs. Take, for example, /logout , defining an answer is very simple ( __init__.py#L56 ):

 @self.bp.route('/logout') def logout(): session['mwo_token'] = None session['username'] = None if 'next' in request.args: return redirect(request.args['next']) return "Logged out!" 

I would like to define a /logout route in my application with a custom response (for example, rendering a template), however, if I use a project, the @app.route("/logout") route is ignored.

What would I like to know if you can "expand" the plan in the sense that I can define the /logout route in my application, call the original method from the project, and then execute a custom response.

+6
source share
1 answer

If you want to completely redefine the behavior of the route, the best way is to override the MWOAuth class. Here is an example that works:

 import os from flask import Flask, Blueprint from flask_mwoauth import MWOAuth app = Flask(__name__) app.secret_key = os.urandom(24) class MyMWOAuth(MWOAuth): def __init__(self, base_url='https://www.mediawiki.org/w', clean_url="Deprecated", default_return_to='index', consumer_key=None, consumer_secret=None, name="Deprecated"): # I skipped other rows. It just an example self.bp = Blueprint('mwoauth', __name__) # By the way you can customize here login and oauth-callback @self.bp.route('/logout') def logout(): # your custom logic here... return "My custom logout" mwoauth = MyMWOAuth(consumer_key='test', consumer_secret='test') app.register_blueprint(mwoauth.bp) if __name__ == "__main__": app.run(debug=True, threaded=True) 

Let open /logout . You will see My custom logout . As you can see, BluePrint route registration takes place in the MWOAuth init method.

The second way is to use request callbacks . Here is an example that demonstrates a change in the response text after logging out.

 from flask import g, request def after_this_request(f): if not hasattr(g, 'after_request_callbacks'): g.after_request_callbacks = [] g.after_request_callbacks.append(f) return f @app.after_request def call_after_request_callbacks(r): for callback in getattr(g, 'after_request_callbacks', ()): callback(r) return r @app.before_request def before_logout(): @after_this_request def after_logout(response): # check if called route == '/logout' # in our case response.data == 'Logged out!' # see: https://github.com/valhallasw/flask-mwoauth/blob/master/flask_mwoauth/__init__.py#L48 if request.url_rule.endpoint == 'mwoauth.logout': # custom logic here... # for example I change data in request response.data = 'Data from after_logout' 

Let open /logout . You will see Data from after_logout .

Hope this helps.

+1
source

All Articles