Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle App Engine (Python) : use UserProperty with Webapp2 User Model
    text
    copied!<p>i have an application where we allow users to use Oauth2 for authentication and even Custom User Registrations. All the Users are saved into the default User entity in the datastore. If the user is logging in using Oauth2 for the first time a new record in the default User entity is created like this:</p> <pre><code> """Check if user is already logged in""" if self.logged_in: logging.info('User Already Logged In. Updating User Login Information') u = self.current_user u.auth_ids.append(auth_id) u.populate(**self._to_user_model_attrs(data, self.USER_ATTRS[provider])) u.put() else: """Create a New User""" logging.info('Creating a New User') ok, user = self.auth.store.user_model.create_user(auth_id, **self._to_user_model_attrs(data, self.USER_ATTRS[provider])) if ok: self.auth.set_session( self.auth.store.user_to_dict(user) ) self.redirect(continue_url) </code></pre> <p>for custom registrations records are inserted through the following handler.</p> <pre><code>class RegistrationHandler(TemplateHandler, SimpleAuthHandler): def get(self): self.render('register.html') def post(self): """Process registration form.""" user = 'appname:%s' % self.request.get('email') name = '%s %s' % (self.request.get('first_name'), self.request.get('last_name')) password = self.request.get('password') avatar = self.request.get('avatar') act_url = user_activation.Activate(self.request.get('first_name'), self.request.get('email')) ok, user = User.create_user(auth_id=user, name=name, password_raw=password, email=self.request.get('email')) if ok: self.auth.set_session(self.auth.store.user_to_dict(user)) acc = models.Account(display_name=self.request.get('first_name'), act_url=act_url, act_key=act_url.split('activate/')[1], user=users.User(User.get_by_auth_id(self.current_user.auth_ids[0]).email)) acc.put() if avatar: avt = models.Picture(is_avatar=True, is_approved=True, image=avatar, user=users.User(User.get_by_auth_id(self.current_user.auth_ids[0]).email)) avt.put() self.redirect('/') </code></pre> <p>Now we are using webapp2_extras.sessions for session handling. We have different models like, Comments, Images, Reviews etc in which we want to use db.UserProperty() as the author field. However, the author field shows blank or None whenever we enter a record into any of these models using 'users.get_current_user()'. I think this is because we are handling the sessions through webapp2 sessions. </p> <p>What we want to achieve is to be able to use the db.UserProperty field in various models and link appropriately to the current user using webapp2 sessions ?</p> <p>the UserProperty() has to be passed with a User Object in order for it to properly insert the records. Even though we are able to enter the records using the following code :</p> <pre><code>user = users.User(User.get_by_auth_id(self.current_user.auth_ids[0]).email) </code></pre> <p>or</p> <pre><code>user = users.User(User.get_by_auth_id(self.current_user.auth_ids[0]).name) </code></pre> <p>but then we are not able to get the whole user object by referencing to model.author</p> <p>Any ideas how we should achieve this ?</p>
 

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