I think you could extend webapp2_extras.appengine.auth.models.User
and add username property, e.g.
from webapp2_extras.appengine.auth.models import User as Webapp2User
class User(Webapp2User):
username = ndb.StringProperty(required=True)
Then, to create a webapp2 app you'd need a config which includes this:
APP_CFG = {
'webapp2_extras.auth': {
'user_model': User, # default is webapp2_extras.appengine.auth.models.User
'user_attributes': ['username'] # list of User model properties
}
}
app = webapp2.WSGIApplication(config=APP_CFG)
Havign the above, creating a new user using the following code will ensure username is unique (ensured by Unique model):
auth_id = 'some-auth-id' # e.g. 'google:123456789', see simpleauth example.
ok, props = User.create_user(auth_id, unique_properties=['username'],
username='some-username',
...)
Problem is, with this configuration you are bound to set username
during the creation time.
If you wanted to make username optional, or let users set/change it later on, you would probably want to change the above code to something like this:
class User(Webapp2User):
username = ndb.StringProperty(required=True)
# when creating a new user:
auth_id = 'some-auth-id' # e.g. 'google:123456789', see simpleauth example.
ok, props = User.create_user(auth_id, unique_properties=[], ...)
Basically, unique_properties
will be empty list (or you can just skip it).
Then, in a "change/set username" form handler, you could check if a username already exists:
def handle_change_username(self):
user = ... # get the user who wants to change their username
username = self.request.get('username')
uniq = 'User.username:%s' % username
ok = User.unique_model.create(uniq)
if ok:
user.username = username
user.put()
else:
# notify them that this username
# is already taken
...
Hope this helps!
--
You received this message because you are subscribed to the Google Groups "SimpleAuth for GAE" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gae-simpleaut...@googlegroups.com.
Visit this group at http://groups.google.com/group/gae-simpleauth.
For more options, visit https://groups.google.com/groups/opt_out.
If you wanted to make username optional, or let users set/change it later on, you would probably want to change the above code to something like this:
class User(Webapp2User):
username = ndb.StringProperty(required=True)
If you wanted to make username optional, or let users set/change it later on, you would probably want to change the above code to something like this (no required=True):
class User(Webapp2User):
username = ndb.StringProperty() # note, there's no required=True