Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bottle : how to set a cookie inside a python decorator?

There are some operations that needs to be done before running some routes. For example :

  • check if we recognise the user,
  • check the language,
  • check the location,
  • set variables in the navbar (here after named header) of the html

and so on, then make decisions based on the outcome and lastly run the requested route.

I find it hard to use the respose.set_cookie("cookie_name", actual_cookie) inside a decorator. It seems flask has a "make_response" object that works well (see here on stack overflow issue 34543157 : Python Flask - Setting a cookie using a decorator), but I find it difficult to reproduce the same thing with bottle.

any how here is my attempt that is not working :

#python3
#/decorator_cookie.py

from bottle import request, response, redirect

from other_module import datamodel, db_pointer, secret_value #custom_module

import json

cookie_value = None
surfer_email_exist_in_db = None 
header = None 
db_pointer = instanciation_of_a_db_connexion_to_tables
surfer = db_pointer.get(request.get_cookie('surfer')) if  db_pointer.get(request.get_cookie('surfer')) != None else "empty"

def set_header(func):
    def header_manager():

        global cookie_value, surfer_email_exist_in_db, header, db_pointer                                                                                                                                   
        cookie_value = True #for stack-overflow question convenience
        surfer_email_exist_in_db = True #for stack-overflow question convenience

        if not all([cookie_value, surfer_email_exist_in_db]):
            redirect('/login')

        else:
            header = json.dumps(db_pointer.get('header_fr'))

            response.set_cookie("header", header, secret = secret_value, path = "/", httponly = True)

           return func()
    return header_manager

and the main file where the routing goes to

#python3
#/main.py

from bottle import route, request
from decorator_cookie import set_header
from other_module secret_value

@route('/lets_try')
@set_header
def lets_try():

    header = request.get_cookie('header', secret = secret_value)
    print(header) #here I get None
    return template('lets_try.tpl', headers = header)

I also tried set the cookie like that :


make_response = response(func).set_cookie("header", header, secret = secret_value, path = "/", httponly = True)

But got an error :) Here is the response doc : Response documentation

Do you have any clues ? Thanks

like image 731
Pelican Avatar asked May 08 '20 13:05

Pelican


1 Answers

There is no issue with your code, what you are missing is understanding is understanding

Request 1 [By Browser/No Cookies] -> Request has No cookies -> Response you add cookie header

Request 2 [By Browser/Header Cookies] -> Request has Header cookies -> Response

So for your first request Request.get_cookie will return None but for your second request it will actually return the value

Working fine

like image 123
Tarun Lalwani Avatar answered Nov 11 '22 19:11

Tarun Lalwani