1

I'm trying to add a before_first_request functionality to a specific Blueprint of my Flask application. Below you can see that I have two Blueprints, public and admin.

I have tried this but had no success: https://stackoverflow.com/a/27401269/7077556

This only works for the the first request that is made to the application, any other request from other device and ip after that first one is not handled by this "custom" before_first_request.

I want to run a function just for the first request that a client makes to the public Blueprint.

How can I do this? Thanks in advance

This is the code that I'm using:

from flask import Flask, Blueprint

application = Flask(__name__)

# PUBLIC Blueprint
public = Blueprint('public', __name__, static_url_path='/public', static_folder='static', template_folder='templates')

# ADMIN Blueprint
admin = Blueprint('admin', __name__, static_url_path='/admin', static_folder='static', template_folder='templates')


# Before First Request To Public BP
from threading import Lock
public._before_request_lock = Lock()
public._got_first_request = False

@public.before_request
def init_public_bp():
    if public._got_first_request:
        return
    else:
        with public._before_request_lock:
            public._got_first_request = True
            print('THIS IS THE FIRST REQUEST!')
            # Do more stuff here...


# PUBLIC ROUTE
@public.route("/")
def public_index():
    return 'Hello World!'

# ADMIN ROUTE
@admin.route('/')
def admin_index():
    return 'Admin Area!'

# Register PUBLIC Blueprint
application.register_blueprint(public)
# Register ADMIN Blueprint
application.register_blueprint(admin, url_prefix='/admin')

if __name__ == "__main__":
    application.run(host='0.0.0.0')
Community
  • 1
  • 1
RMM
  • 153
  • 1
  • 2
  • 8

1 Answers1

1

before_request of the blueprint instance isn't a decorator. It's just an instance method that takes a function to be called before a request. You should use it in this manner:

from __future__ import print_function
from flask import Flask, Blueprint

application = Flask(__name__)

# PUBLIC Blueprint
public = Blueprint('public', __name__, static_url_path='/public', static_folder='static', template_folder='templates')

# ADMIN Blueprint
admin = Blueprint('admin', __name__, static_url_path='/admin', static_folder='static', template_folder='templates')


# Before First Request To Public BP
from threading import Lock
public._before_request_lock = Lock()
public._got_first_request = False

def init_public_bp():
    if public._got_first_request:
        return  # or pass
    with public._before_request_lock:
        public._got_first_request = True
        print('THIS IS THE FIRST REQUEST!')
        # Do more stuff here...
public.before_request(init_public_bp)


# PUBLIC ROUTE
@public.route("/")
def public_index():
    return 'Hello World!'

# ADMIN ROUTE
@admin.route('/')
def admin_index():
    return 'Admin Area!'

# Register PUBLIC Blueprint
application.register_blueprint(public)
# Register ADMIN Blueprint
application.register_blueprint(admin, url_prefix='/admin')

if __name__ == "__main__":
    application.run(host='0.0.0.0')
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • It's not working unfortunately. I made first request and second request from different devices with different IP. Here is my terminal: `(env) root@vps123:~/test$ python test.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) - - THIS IS THE FIRST REQUEST! 111.111.111.111 - - [19/May/2017 23:54:26] "GET / HTTP/1.1" 200 - 222.222.222.222 - - [19/May/2017 23:54:33] "GET / HTTP/1.1" 200 -` You can see that the second request didn't print anything. – RMM May 19 '17 at 22:15
  • The answer was missing a block after `if public._got_first_request:`. It should only want to ever print for the first request, shouldn't it? – Oluwafemi Sule May 19 '17 at 22:25
  • Yes it should print only for the first request, but not just one time. If user 1 makes request it should print, then user 2 makes another request it should print again and so on... So for every new user that makes first request to the website it should print, and any next request that comes from same user should not print. Maybe has to do with sessions? I really don't know – RMM May 19 '17 at 22:31