def signup(cls, handler, email, name, password, website, areas, profession, bio): auth_id = '%s: %s' % ('name', name) url = WhysaurusUser.constructURL(name) gaid = WhysaurusUser.generateUniqueUserGaid(True) unique_properties = ['email', 'url', 'name'] if email else ['url', 'name'] if password is None: raise WhysaurusException('Unable to create user. No password supplied.') privateAreas = [] existingPrivateArea = handler.session.get('currentArea') if existingPrivateArea: privateAreas = [existingPrivateArea] result, creationData = WhysaurusUser.create_user(auth_id, unique_properties, url=url, email=email, name=name, password_raw=password, gaId=gaid, websiteURL=website, areasOfExpertise=areas, currentProfession=profession, bio=bio, verified=False, privateAreas=privateAreas) if not result: #user_data is a tuple if 'name' in creationData: raise WhysaurusException('Unable to create user because the username %s is already in use' % name) elif 'email' in creationData: raise WhysaurusException('Unable to create user because the email %s is already in use' % email) else: raise WhysaurusException('Unable to create user for email %s because of \ duplicate keys %s' % (auth_id, user_data[1])) else: user = creationData if email: user_id = user.get_id() user.auth_ids.append('%s: %s' % ('email', email)) user.put() WhysaurusUser.send_verification_email(handler, user_id, email, "signing up for Whysaurus") else: logging.info('Created a username only user. Name: %s.' % name) if existingPrivateArea: areaUser = AreaUser(userKey=user.key.urlsafe(), privateArea=existingPrivateArea) areaUser.putUnique() ReportEvent.queueEventRecord(user.key.urlsafe(), None, None, "New User") user.addToSearchIndex() return user # SUCCESS
def updateUserSettings(self, pas, role = None): oldPas = AreaUser.query(AreaUser.userKey==self.key.urlsafe()).fetch() for y in oldPas: if y.privateArea not in pas: y.deleteRelationship() for x in pas: areaUser = AreaUser(userKey=self.key.urlsafe(), privateArea=x) areaUser.putUnique() if role: self.role = role self.privateAreas = pas self.put()
def get_pa(self): userNamespace = namespace_manager.get_namespace() namespace_manager.set_namespace('') user = self.current_user if user is None: self.response.out.write('Need to login.') return userIds = [] queryUsrIds = AreaUser.query().filter( AreaUser.privateArea == userNamespace) for userArea in queryUsrIds.iter(): userIds.append(ndb.Key(urlsafe=userArea.userKey)) queryUsr = ndb.get_multi(userIds) users = [] i = 0 for yUser in queryUsr: if yUser.isAdmin != True: users = users + [{ 'u': yUser, 'index': i, 'userKey': yUser.key.urlsafe() }] i = i + 1 paginator = Paginator(users, 35) page = self.request.get('page') try: paginatedUsers = paginator.page(page) except PageNotAnInteger: paginatedUsers = paginator.page(1) except EmptyPage: paginatedUsers = paginator.page(paginator.num_pages) template_values = { 'user': user, 'users': paginatedUsers, 'currentArea': userNamespace, 'currentAreaDisplayName': self.session.get('currentAreaDisplayName') } namespace_manager.set_namespace(userNamespace) self.response.out.write( self.template_render('adminPrivateArea.html', template_values))
def get_pa(self): userNamespace = namespace_manager.get_namespace() namespace_manager.set_namespace('') user = self.current_user if user is None: self.response.out.write('Need to login.') return userIds = [] queryUsrIds = AreaUser.query().filter(AreaUser.privateArea == userNamespace) for userArea in queryUsrIds.iter(): userIds.append(ndb.Key(urlsafe=userArea.userKey)) queryUsr = ndb.get_multi(userIds) users = [] i = 0 for yUser in queryUsr: if yUser.isAdmin != True: users = users + [{'u':yUser, 'index':i, 'userKey': yUser.key.urlsafe()}] i = i+1 paginator = Paginator(users, 35) page = self.request.get('page') try: paginatedUsers = paginator.page(page) except PageNotAnInteger: paginatedUsers = paginator.page(1) except EmptyPage: paginatedUsers = paginator.page(paginator.num_pages) template_values = { 'user': user, 'users': paginatedUsers, 'currentArea':userNamespace, 'currentAreaDisplayName': self.session.get('currentAreaDisplayName') } namespace_manager.set_namespace(userNamespace) self.response.out.write( self.template_render('adminPrivateArea.html', template_values))
def _on_signin(self, data, auth_info, provider): auth_id = '%s: %s' % (provider, data['id']) logging.info('Looking for a user with id %s', auth_id) user = self.auth.store.user_model.get_by_auth_id(auth_id) _attrs = self._to_user_model_attrs(data, self.USER_ATTRS[provider]) if user: logging.info('Found existing user to log in: ' + str(_attrs)) # Existing users might've changed their profile data so we update our # local model anyway. This might result in quite inefficient usage # of the Datastore, but we do this anyway for demo purposes. # # In a real app you could compare _attrs with user's properties fetched # from the datastore and update local user in case something's changed. self.auth.set_session(self.auth.store.user_to_dict(user)) self.current_user = user user.login() if 'postloginaction' in self.session: logging.info('There was a post login action, so the user is not logged into the private area.') elif len(user.privateAreas) > 0: self.setUserArea(user.privateAreas[0]) else: # check whether there's a user currently logged in # then, create a new user if nobody's signed in, # otherwise add this auth_id to currently logged in user. if self.logged_in and self.current_user: # This code is currently not triggered, # there is no way to log in again once logged in logging.info('Updating currently logged in user') u = self.current_user u.populate(**_attrs) # The following will also do u.put(). Though, in a real app # you might want to check the result, which is # (boolean, info) tuple where boolean == True indicates success # See webapp2_extras.appengine.auth.models.User for details. u.add_auth_id(auth_id) u.login() else: logging.info('Creating a brand new user. Auth_id: %s ', str(auth_id)) _attrs['url'] = WhysaurusUser.constructURL(_attrs['name']) currentArea = self.session.get('currentArea') currentAreaDisplayName = self.session.get('currentAreaDisplayName') if currentArea: _attrs['privateAreas'] = [currentArea] ok, user = self.auth.store.user_model.create_user(auth_id, **_attrs) if ok: if currentArea: areaUser = AreaUser(userKey=user.key.urlsafe(), privateArea=currentArea) areaUser.putUnique() user.login() self.current_user = user self.auth.set_session(self.auth.store.user_to_dict(user)) ReportEvent.queueEventRecord(user.key.urlsafe(), None, None, "New User") user.addToSearchIndex() else: logging.info('Creation failed: ' + str(ok)) # Remember auth data during redirect, just for this demo. You wouldn't # normally do this. # self.session.add_flash(data, 'data - from _on_signin(...)') # self.session.add_flash(auth_info, 'auth_info - from _on_signin(...)') if 'postloginaction' in self.session: postLoginAction = str(self.session['postloginaction']) logging.info('Doing post login action: ' + postLoginAction) self.doPostLoginAction(postLoginAction, self.session) else: target = str(self.session['original_url']) currentArea = self.session.get('currentArea') currentAreaDisplayName = self.session.get('currentAreaDisplayName') if target.find("/login") != -1 or currentArea: target = "/" logging.info('_ON_SIGNIN: Redirecting to %s' % target) self.redirect(target)
def _on_signin(self, data, auth_info, provider): auth_id = '%s: %s' % (provider, data['id']) logging.info('Looking for a user with id %s', auth_id) user = self.auth.store.user_model.get_by_auth_id(auth_id) _attrs = self._to_user_model_attrs(data, self.USER_ATTRS[provider]) if user: logging.info('Found existing user to log in: ' + str(_attrs)) # Existing users might've changed their profile data so we update our # local model anyway. This might result in quite inefficient usage # of the Datastore, but we do this anyway for demo purposes. # # In a real app you could compare _attrs with user's properties fetched # from the datastore and update local user in case something's changed. self.auth.set_session(self.auth.store.user_to_dict(user)) self.current_user = user user.login() if 'postloginaction' in self.session: logging.info( 'There was a post login action, so the user is not logged into the private area.' ) elif len(user.privateAreas) > 0 and not user.admin: self.setUserArea(user.privateAreas[0]) else: # check whether there's a user currently logged in # then, create a new user if nobody's signed in, # otherwise add this auth_id to currently logged in user. if self.logged_in and self.current_user: # This code is currently not triggered, # there is no way to log in again once logged in logging.info('Updating currently logged in user') u = self.current_user u.populate(**_attrs) # The following will also do u.put(). Though, in a real app # you might want to check the result, which is # (boolean, info) tuple where boolean == True indicates success # See webapp2_extras.appengine.auth.models.User for details. u.add_auth_id(auth_id) u.login() else: logging.info('Creating a brand new user. Auth_id: %s ', str(auth_id)) _attrs['url'] = WhysaurusUser.constructURL(_attrs['name']) _attrs['gaId'] = WhysaurusUser.generateUniqueUserGaid(True) currentArea = self.session.get('currentArea') currentAreaDisplayName = self.session.get( 'currentAreaDisplayName') if currentArea: _attrs['privateAreas'] = [currentArea] ok, user = self.auth.store.user_model.create_user( auth_id, **_attrs) if ok: if currentArea: areaUser = AreaUser(userKey=user.key.urlsafe(), privateArea=currentArea) areaUser.putUnique() user.login() self.current_user = user self.auth.set_session(self.auth.store.user_to_dict(user)) ReportEvent.queueEventRecord(user.key.urlsafe(), None, None, "New User") user.addToSearchIndex() else: logging.info('Creation failed: ' + str(ok)) # Remember auth data during redirect, just for this demo. You wouldn't # normally do this. # self.session.add_flash(data, 'data - from _on_signin(...)') # self.session.add_flash(auth_info, 'auth_info - from _on_signin(...)') if 'postloginaction' in self.session: postLoginAction = str(self.session['postloginaction']) logging.info('Doing post login action: ' + postLoginAction) self.doPostLoginAction(postLoginAction, self.session) else: target = str(self.session['original_url']) currentArea = self.session.get('currentArea') currentAreaDisplayName = self.session.get('currentAreaDisplayName') if target.find("/login") != -1 or currentArea: target = "/" logging.info('_ON_SIGNIN: Redirecting to %s' % target) self.redirect(target)