Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you could extend <code>webapp2_extras.appengine.auth.models.User</code> and add username property, e.g.</p> <pre><code>from webapp2_extras.appengine.auth.models import User as Webapp2User class User(Webapp2User): username = ndb.StringProperty(required=True) </code></pre> <p>Then, to create a webapp2 app you'd need a config which includes this:</p> <pre><code>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) </code></pre> <p>Havign the above, creating a new user using the following code will ensure username is unique (ensured by Unique model):</p> <pre><code>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', ...) if not ok: # props list will contain 'username', indicating that # another entity with the same username already exists ... </code></pre> <p>Problem is, with this configuration you are bound to set <code>username</code> during the creation time.</p> <p>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:</p> <pre><code>class User(Webapp2User): username = ndb.StringProperty() # note, there's no 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=[], ...) </code></pre> <p>Basically, <code>unique_properties</code> will be empty list (or you can just skip it). Also, you could temporarily assign <code>username</code> property to something like <code>user.key.id()</code> until the user decides to change their username to something more meaningful. Take, for instance, Google+ profile links: mine is currently <a href="https://plus.google.com/114517983826182834234" rel="nofollow">https://plus.google.com/114517983826182834234</a>, but if they let me change it, I would try something like <a href="https://plus.google.com/+IamNotANumberAnymore" rel="nofollow">https://plus.google.com/+IamNotANumberAnymore</a></p> <p>Then, in a "change/set username" form handler, you could check if a username already exists and update User entity (if it doesn't):</p> <pre><code>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 ... </code></pre> <p><code>User.unique_model.create(uniq)</code> will create a <code>Unique</code> entity with the given value if it didn't exist. In this case <code>ok</code> will be <code>True</code>. Otherwise, <code>ok</code> will be <code>False</code> which indicates that an entity with that value (a unique username in this case) already exists.</p> <p>Also, you might want to put <code>User.unique_model.create()</code> and <code>user.put()</code> in the same transaction (it'll be XG because they are in different entity groups).</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload