Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:26:00+00:00 2026-05-16T14:26:00+00:00

I’m trying to use decorators in order to manage the way users may or

  • 0

I’m trying to use decorators in order to manage the way users may or may not access resources within a web application (running on Google App Engine). Please note that I’m not allowing users to log in with their Google accounts, so setting specific access rights to specific routes within app.yaml is not an option.

I used the following resources :
– Bruce Eckel’s guide to decorators
– SO : get-class-in-python-decorator2
– SO : python-decorators-and-inheritance
– SO : get-class-in-python-decorator

However I’m still a bit confused…

Here’s my code ! In the following example, current_user is a @property method which belong to the RequestHandler class. It returns a User(db.model) object stored in the datastore, with a level IntProperty().

class FoobarController(RequestHandler):

    # Access decorator
    def requiredLevel(required_level):
        def wrap(func):
            def f(self, *args):
                if self.current_user.level >= required_level:
                    func(self, *args)
                else:
                    raise Exception('Insufficient level to access this resource') 
            return f
        return wrap

    @requiredLevel(100)
    def get(self, someparameters):
        #do stuff here...

    @requiredLevel(200)
    def post(self):
        #do something else here...

However, my application uses different controllers for different kind of resources. In order to use the @requiredLevel decorator within all subclasses, I need to move it to the parent class (RequestHandler) :

class RequestHandler(webapp.RequestHandler):

    #Access decorator
    def requiredLevel(required_level):
        #See code above

My idea is to access the decorator in all controller subclasses using the following code :

class FoobarController(RequestHandler):

    @RequestHandler.requiredLevel(100)
    def get(self):
        #do stuff here...

I think I just reached the limit of my knowledge about decorators and class inheritance :). Any thoughts ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:26:01+00:00Added an answer on May 16, 2026 at 2:26 pm

    Your original code, with two small tweaks, should also work. A class-based approach seems rather heavy-weight for such a simple decorator:

    class RequestHandler(webapp.RequestHandler):
    
        # The decorator is now a class method.
        @classmethod     # Note the 'klass' argument, similar to 'self' on an instance method
        def requiredLevel(klass, required_level):
            def wrap(func):
                def f(self, *args):
                    if self.current_user.level >= required_level:
                        func(self, *args)
                    else:
                        raise Exception('Insufficient level to access this resource') 
                return f
            return wrap
    
    
    class FoobarController(RequestHandler):
        @RequestHandler.requiredLevel(100)
        def get(self, someparameters):
            #do stuff here...
    
        @RequestHandler.requiredLevel(200)
        def post(self):
            #do something else here...
    

    Alternately, you could use a @staticmethod instead:

    class RequestHandler(webapp.RequestHandler):
    
        # The decorator is now a static method.
        @staticmethod     # No default argument required...
        def requiredLevel(required_level):
    

    The reason the original code didn’t work is that requiredLevel was assumed to be an instance method, which isn’t going to be available at class-declaration time (when you were decorating the other methods), nor will it be available from the class object (putting the decorator on your RequestHandler base class is an excellent idea, and the resulting decorator call is nicely self-documenting).

    You might be interested to read the documentation about @classmethod and @staticmethod.

    Also, a little bit of boilerplate I like to put in my decorators:

        @staticmethod
        def requiredLevel(required_level):
            def wrap(func):
                def f(self, *args):
                    if self.current_user.level >= required_level:
                        func(self, *args)
                    else:
                        raise Exception('Insufficient level to access this resource') 
                # This will maintain the function name and documentation of the wrapped function.
                # Very helpful when debugging or checking the docs from the python shell:
                wrap.__doc__ = f.__doc__
                wrap.__name__ = f.__name__
                return f
            return wrap
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.