Flask-security: How to override a method of flask-security (register_user)?

Created on 11 Apr 2014  路  5Comments  路  Source: mattupstate/flask-security

Hello,

I'm trying without success to override a behavior of Flask-Security: sending confirmation emails. I would like them to be sent to a default e-mail address, whatever the candidate user submitted. So that the administrator can review and filter who's registering.

It seems the confirmation email is sent by flask.ext.security.registerable.register_user()

def new_register_user(**kwargs):
    # Define a method with new behavior
    ...

import sys

# Override registerable module method
import flask.ext.security.registerable
sys.modules['flask.ext.security.registerable'].register_user = new_register_user
sys.modules['flask.ext.security'].registerable.register_user = new_register_user

# When printing sys.modules, it seems that registerable module
# appears also there:
import flask_security
sys.modules['flask_security.registerable'].register_user = new_register_user
sys.modules['flask_security'].registerable.register_user = new_register_user

Unfortunately, it does not work. The original method is still used. Do you have any hints?

Most helpful comment

The feature mentioned by @Trii is documented (a little) here: https://flask-security.readthedocs.io/en/latest/customizing.html#emails.

All 5 comments

There's a much easier way to do this.

  1. Turn off the register email by setting SEND_REGISTER_EMAIL = False
  2. Register a handler for the flask_security.signals.user_registered signal
  3. Send an email wherever you like in the signal handler

Otherwise I would not support overriding methods the way you have described.

Thanks for the prompt reply!

Excellent, it worked well :-) Great design!

It isn't very well documented but you can also attach your own callback to the send_mail_task function on the flask_security.core.Security object and it will execute instead of the flask_mail version

security = Security(app, user_datastore)
security.send_mail_task(my_send_email)

Thank you for posting the override functionality! Was just what I needed! Would be great to have this in the docs

The feature mentioned by @Trii is documented (a little) here: https://flask-security.readthedocs.io/en/latest/customizing.html#emails.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tim-hub picture tim-hub  路  4Comments

swedishmike picture swedishmike  路  5Comments

Ben095 picture Ben095  路  6Comments

lashex picture lashex  路  3Comments

adamlwgriffiths picture adamlwgriffiths  路  5Comments