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

  • SEARCH
  • Home
  • 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 6989477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:13:01+00:00 2026-05-27T19:13:01+00:00

I successfully with webapp2 can authenticate and login/logout users and I have a decorator

  • 0

I successfully with webapp2 can authenticate and login/logout users and I have a decorator to know whether a user is logged in and this works:

class MyPageHandler(NewBaseHandler):
    """
    Only accessible to users that are logged in
    """
    @user_required
    def get(self):
        user = self.auth.get_user_by_session()
        self.render_template('mypage.htm', {'user': self.auth.get_user_by_session()})

Now I want to add something basic as a username and I suspect that I might not have to subclass the User model provided with webapp2_extras since it is an Expando model. Could you tell me how to add a username or update a user with a username? I didn’t try but I believe I can display an auth_id but it’s not the same thing as the username?

Thanks

Update

It seems as the User model is an expando model you “just add proprties” like this for example when creating a user:

    username = self.request.POST.get('username')
    password = self.request.POST.get('password')
    # Passing password_raw=password so password will be hashed
    # Returns a tuple, where first value is BOOL. If True ok, If False no new user is created
    user = self.auth.store.user_model.create_user(username, password_raw=password)
    user.name = username

I didn’t thoroughly test the code above but it seems that with webapp2 we won’t need to subclass the User class to get the functionality.

Solution / Workaround

class SecureRequestHandler(BaseHandler):
    """
        Only accessible to users that are logged in
    """
    @user_required
    def get(self, **kwargs):
        a = self.app.config.get('foo')
        auser = self.auth.get_user_by_session() 
        userid = auser['user_id']
        user = auth_models.User.get_by_id(auser['user_id'])
        try:
            return "Secure zone %s <a href='%s'>Logout</a>" % (user.name, self.auth_config['logout_url'])
        except (AttributeError, KeyError), e:
            return "Secure zone"
  • 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-27T19:13:02+00:00Added an answer on May 27, 2026 at 7:13 pm

    hope this help

    from webapp2_extras.appengine.auth.models import User
    from ndb import key, model
    
    class MUser(User): # child class of User
        displayname = model.StringProperty() # show on screen, auto create 'user-{id}'
        lastaccess = model.DateTimeProperty(auto_now=True)
    
        @classmethod
        def update_or_insert_user(cls, user):
            """update some user's prop if exited else create new muser
            return : MUser object or None
            """
            u = MUser.get_by_auth_id(user.email())
            if u is None:                               
                ok, u = MUser.create_user(user.email(), id=newid)
                if ok == True:
                    u.displayname = 'user-'+str(u.get_id())
                else:
                    log.error('create new user error : ' + user.email())
                    u = None
            else :          
                u.put() # Update last access (auto fill def in model class)
    
            return u
    
    class MyPageHandler(NewBaseHandler):
        """
        Only accessible to users that are logged in
        """
        @user_required
        def get(self):
            user = self.get_current_muser()
            self.render_template('mypage.htm', {'user': user.displayname })
    
        def get_current_muser(self):
            # come from gae login page, convert current gae user to muser
            user = users.get_current_user() # google users
            if user:
                # user have login, try to create or update
                log.info('create user')
                mu = MUser.update_or_insert_user(user)
                if mu:
                    # save to session
                    self.session['muserid'] = mu.key.id()
                    return mu
    
            return None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully implemented this from android to a java httpservlet on google app
I have a webapp that uses Forms Authentication using ActiveDirectoryMembershipProvider. That works fine... users
I successfully installed the latest QuantumGrid from DevExpress, but I've never worked with this
I have successfully connected to an Oracle database (10g) from C# (Visual Studio 2008)
I have installed Red5 server successfully and also am able to run the demos
I have been trying to solve this for 2 weeks and I have not
So for my webapp, if I remove a user that is currently logged in,
In the dev_appserver we can import ipaddr successfully, while when I deploy the codes
I have successfully implemented rest webservice using apache wink. I am using ant to
This is my first test for Asp.Net Web Application. We have an Engine consisting

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.