9

I have a class for Flask:

class Likes(object):
    def __init__(self, model, table_id):
        self.model = model
        self.table_id = table_id

        if request.form["likes"] == 'like':
            query = self.model.query.filter_by(id=table_id).first()
            query.likes += 1
            db.session.commit()
            flash(u'Like =)) ' + query.title, 'info')
        elif request.form["likes"] == 'dislike':
            query = self.model.query.filter_by(id=table_id).first()
            query.likes -= 1
            db.session.commit()
            flash(u"Don't like =(" + query.title, 'info')

and I want to call this class every time user sent POST request, but every time I create an instance of my class I need add check request type:

# ...
if request.method == 'POST':
    Likes(Post, request.form["post_id"])
# ...

How can I improve my class and add inside it this check:

if request.method == 'POST':
    # ...

Solution: Use decorator @app.before_request

@app.before_request
def before_req():
    if request.method == 'POST':
        flash(u'Before request', 'success')
Denis
  • 659
  • 1
  • 9
  • 21
  • Why are you using a class instead of a function? It sound like a bad idea to call that everytime a post request is done. Please explain what you try to achieve otherwise it looks like [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem#) – amirouche Sep 10 '15 at 09:22
  • i'm trying to create class for "likes" sytem which can be used for defferent content types in my app. This class should handle post request and looking for "like" or "dislike" values on it, depending on request it should add 1 like or remove it form data base. Using different functions for every content type is not a good idea. – Denis Sep 10 '15 at 09:37
  • you don't necessarly need a class. – amirouche Sep 10 '15 at 09:54
  • ok but this question is still actual for non-class based likes system, is there any way to avoid testing for request type when i call a function. Or as i can see second argument mention to call this check before function start do their job. – Denis Sep 10 '15 at 10:06

1 Answers1

17

You can use Flask.request_started signal to run something everytime a request arrive and then execute the code you require.

flask.request_started

This signal is sent before any request processing started but when the request context was set up. Because the request context is already bound, the subscriber can access the request with the standard global proxies such as request.

Have a look at the Flask's Signals chapter to learn more.

Use something like that in your code:

def create_like(sender, **extra):
    if request.method == 'POST':
        Likes(Post, request.form["post_id"])


from flask import request_started
request_started.connect(create_like, app)

This was adapted from the example for the documentation of Core Signals.

amirouche
  • 7,682
  • 6
  • 40
  • 94
  • 2
    thank you for your help =) you give me a good idea and right info, but instead your function variant with import use a decorator @app.before_request i found here http://stackoverflow.com/questions/14367991/ – Denis Sep 10 '15 at 20:41