0

i want to protect page on python-flask, can go on this page only limited users.

I'm write this code :

def im_seller():
    if not g.user.seller_fee_paid:
        return redirect(url_for('account.seller_fee'))
    return


@account.route('/account/seller/products.html')
@login_required
@im_seller
def seller_products():

the script not working, give this error :

TypeError: im_seller() takes no arguments (1 given)

where I'm wrong ? thanks all.

AutoSoft
  • 191
  • 1
  • 1
  • 11

2 Answers2

1

Decorators take a function and need to return a function:

from functools import wraps

def require_seller(f):
    @wraps(f)
    def require_seller_wrapper(*args, **kwargs):
        if not g.user.seller_fee_paid:
            return redirect(url_for('account.seller_fee'))
        return f(*args, **kwargs)
    return require_seller_wrapper

You will also want to reverse the order of require_seller and login_required so you can be sure that g.user is set:

@account.route('/account/seller/products.html')
@require_seller
@login_required
def seller_products():
    return "All the seller's products"

See this answer about decorators for all the details as to the whys.

Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
0
from functools import wraps

def require_seller(f):
    @wraps(f)
    def require_seller_wrapper(*args, **kwargs):
        if not g.user.seller_fee_paid:
           return redirect(url_for('account.seller_fee'))
         return f(*args, **kwargs)
 return require_seller_wrapper

You should use the python decorator.Link

aman kumar
  • 3,086
  • 1
  • 17
  • 24