def set_locale(cls, force=None): """ retrieve locale from a prioritized list of sources and then set locale and save it cls: self object force: a locale to force set (ie 'en_US') return: locale """ # 1. force locale if provided locale = force if locale not in AVAILABLE_LOCALES: # 2. retrieve locale from url query string locale = cls.request.get("hl", None) if locale not in AVAILABLE_LOCALES: # 3. retrieve locale from cookie locale = utils.read_cookie(cls, "hl") if locale not in AVAILABLE_LOCALES: # 4. retrieve locale from accept language header locale = get_locale_from_accept_header(cls) if locale not in AVAILABLE_LOCALES: # 5. detect locale from IP address location locale = get_locale_from_territory(get_territory_from_ip(cls)) if locale not in AVAILABLE_LOCALES: # 6. use default locale locale = AVAILABLE_LOCALES[0] # convert unicode locale to string for headers locale = str(locale) i18n.get_i18n().set_locale(locale) # save locale in cookie with 26 weeks expiration (in seconds) utils.write_cookie(cls, "hl", locale, "/", 15724800) return locale
def set_lang_cookie_and_return_dict(cls, force=""): """ set language returning a dict and set cookie """ if force != "": lang_cookie = force elif cls.request.get("hl") == "": # ask for cookie lang_cookie = utils.read_cookie(cls, "hl") arrLang = str(get_languages(cls)[0]).lower() if not lang_cookie: if arrLang.find("es") >= 0: lang_cookie = "es" else: lang_cookie = "en" else: # set cookie to param 'hl' value lang_cookie = cls.request.get("hl") # Two weeks for hl cookie utils.write_cookie(cls, "hl", str(lang_cookie), "/", 1209600) lang = { 'en': languages.en, 'es': languages.es, 'it': languages.it, 'cn': languages.cn, }[lang_cookie] return lang
def get(self): # lookup user's auth info user_info = User.get_by_id(long(self.user_id)) # if we came in from a stream create, redirect back to it next = utils.read_cookie(self, "next") if next > "": utils.write_cookie(self, "next", "", '/', expires=7200) return self.redirect(next) # params build out params = {} return self.render_template('user/dashboard.html', **params)
def get(self, name): # lookup user's auth info user_info = User.get_by_id(long(self.user_id)) # look up user's instances instance = Instance.get_by_name(name) if not instance: params = {} return self.redirect_to('instances-list', **params) if instance.renamed == None: instance.renamed = "" # remap so template can address if instance.created < (datetime.datetime.now() - datetime.timedelta(0, 600)): instance.expired = True else: instance.expired = False stream = Stream.get_by_id(instance.stream.id()) if utils.read_cookie(self, "guide") == "closed": guide = False else: guide = True if instance.size == 1: instance_cores = 8 instance_memory = 30 elif instance.size == 2: instance_cores = 16 instance_memory = 60 else: instance_cores = 4 instance_memory = 15 params = { 'guide': guide, 'instance': instance, 'stream': stream, 'user_info': user_info, 'instance_cores': instance_cores, 'instance_memory': instance_memory } return self.render_template('instance/detail.html', **params)
def get(self, provider_name): if not config.enable_federated_login: message = _('Federated login is disabled.') self.add_message(message, 'warning') return self.redirect_to('login') # callback handler for twitter oauth if provider_name == "twitter": oauth_token = self.request.get('oauth_token') oauth_verifier = self.request.get('oauth_verifier') twitter_helper = twitter.TwitterAuth(self) user_data = twitter_helper.auth_complete(oauth_token, oauth_verifier) screen_name = user_data['screen_name'] if self.user: # user is already logged in so we set a new association with twitter user_info = models.User.get_by_id(long(self.user_id)) if models.SocialUser.check_unique(user_info.key, 'twitter', str(user_data['id'])): social_user = models.SocialUser( user=user_info.key, provider='twitter', uid=str(user_data['id']), extra_data=user_data, screen_name=screen_name, ) social_user.put() message = _('Twitter association added.') self.add_message(message, 'success') else: message = _('This Twitter account is already in use.') self.add_message(message, 'error') self.redirect_to('edit-profile') else: # user is not logged in, but is trying to log in via twitter social_user = models.SocialUser.get_by_provider_and_uid( 'twitter', str(user_data['id'])) if social_user: # Social user exists. Need authenticate related site account user = social_user.user.get() self.auth.set_session(self.auth.store.user_to_dict(user), remember=True) logVisit = models.LogVisit( user=user.key, uastring=self.request.user_agent, ip=self.request.remote_addr, timestamp=utils.get_date_time()) logVisit.put() self.redirect_to('home') else: # Social user does not exists. Need show login and registration forms! twitter_helper.save_association_data(user_data) message = _( 'This Twitter account is not associated with a StackGeek account. ' 'Please sign in or create a StackGeek account before continuing.' ) self.add_message(message, 'warning') self.redirect_to('login') # callback handler for github oauth elif provider_name == "github": # get our request code back from the social login handler above code = self.request.get('code') # create our github auth object (again) scope = 'gist' github_helper = github.GithubAuth(scope) # retrieve the access token using the code and auth object try: access_token = github_helper.get_access_token(code) user_data = github_helper.get_user_info(access_token) except: message = _( 'An error was encountered while exchanging tokens with Github.' ) self.add_message(message, 'error') self.redirect_to('edit-profile') return if self.user: # user is already logged in so we set a new association with github user_info = models.User.get_by_id(long(self.user_id)) if models.SocialUser.check_unique(user_info.key, 'github', str(user_data['login'])): social_user = models.SocialUser(user=user_info.key, provider='github', uid=str( user_data['login']), access_token=access_token, extra_data=user_data) social_user.put() message = _( 'The StackGeek application has been added to your Github account.' ) self.add_message(message, 'success') else: message = _( 'The currently logged in Github account is already in use with another account.' ) self.add_message(message, 'error') self.redirect_to('edit-profile') return # check to see if we are headed anywhere else besides the profile page next_page = utils.read_cookie(self, 'oauth_return_url') utils.write_cookie(self, 'oauth_return_url', '', '/', 15) # try out what we found or redirect to profile if it's a bad value if next_page: try: self.redirect_to(next_page) except: self.redirect_to('edit-profile') else: self.redirect_to('edit-profile') else: # user is not logged in, but is trying to log in via github social_user = models.SocialUser.get_by_provider_and_uid( 'github', str(user_data['login'])) if social_user: # Social user exists. Need authenticate related site account user = social_user.user.get() self.auth.set_session(self.auth.store.user_to_dict(user), remember=True) logVisit = models.LogVisit( user=user.key, uastring=self.request.user_agent, ip=self.request.remote_addr, timestamp=utils.get_date_time()) logVisit.put() self.redirect_to('home') else: # Social user does not exists. Need show login and registration forms! message = _( 'This Github account is not associated with a StackGeek account. ' 'Please sign in or create a StackGeek account before continuing.' ) self.add_message(message, 'warning') self.redirect_to('login') # google, myopenid, yahoo OpenID Providers elif provider_name in models.SocialUser.open_id_providers(): provider_display_name = models.SocialUser.PROVIDERS_INFO[ provider_name]['label'] # get info passed from OpenId Provider from google.appengine.api import users current_user = users.get_current_user() if current_user: if current_user.federated_identity(): uid = current_user.federated_identity() else: uid = current_user.user_id() email = current_user.email() else: message = _( 'No user authentication information received from %s. ' 'Please ensure you are logging in from an authorized OpenID Provider (OP).' % provider_display_name) self.add_message(message, 'error') return self.redirect_to('login') if self.user: # add social account to user user_info = models.User.get_by_id(long(self.user_id)) if models.SocialUser.check_unique(user_info.key, provider_name, uid): social_user = models.SocialUser(user=user_info.key, provider=provider_name, uid=uid) social_user.put() message = _('%s association successfully added.' % provider_display_name) self.add_message(message, 'success') else: message = _('This %s account is already in use.' % provider_display_name) self.add_message(message, 'error') self.redirect_to('edit-profile') else: # login with OpenId Provider social_user = models.SocialUser.get_by_provider_and_uid( provider_name, uid) if social_user: # Social user found. Authenticate the user user = social_user.user.get() self.auth.set_session(self.auth.store.user_to_dict(user), remember=True) logVisit = models.LogVisit( user=user.key, uastring=self.request.user_agent, ip=self.request.remote_addr, timestamp=utils.get_date_time()) logVisit.put() self.redirect_to('home') else: message = _( 'This OpenID based account is not associated with a StackGeek account. ' 'Please sign in or create a StackGeek account before continuing.' ) self.add_message(message, 'warning') self.redirect_to('login') else: message = _('This authentication method is not yet implemented!') self.add_message(message, 'warning') self.redirect_to('login')
def get(self, provider_name): if not config.enable_federated_login: message = _('Federated login is disabled.') self.add_message(message, 'warning') return self.redirect_to('login') # callback handler for twitter oauth if provider_name == "twitter": oauth_token = self.request.get('oauth_token') oauth_verifier = self.request.get('oauth_verifier') twitter_helper = twitter.TwitterAuth(self) user_data = twitter_helper.auth_complete(oauth_token, oauth_verifier) screen_name = user_data['screen_name'] if self.user: # user is already logged in so we set a new association with twitter user_info = models.User.get_by_id(long(self.user_id)) if models.SocialUser.check_unique(user_info.key, 'twitter', str(user_data['id'])): social_user = models.SocialUser( user = user_info.key, provider = 'twitter', uid = str(user_data['id']), extra_data = user_data, screen_name = screen_name, ) social_user.put() message = _('Twitter association added.') self.add_message(message, 'success') else: message = _('This Twitter account is already in use.') self.add_message(message, 'error') self.redirect_to('edit-profile') else: # user is not logged in, but is trying to log in via twitter social_user = models.SocialUser.get_by_provider_and_uid('twitter', str(user_data['id'])) if social_user: # Social user exists. Need authenticate related site account user = social_user.user.get() self.auth.set_session(self.auth.store.user_to_dict(user), remember=True) logVisit = models.LogVisit( user = user.key, uastring = self.request.user_agent, ip = self.request.remote_addr, timestamp = utils.get_date_time() ) logVisit.put() self.redirect_to('home') else: # Social user does not exists. Need show login and registration forms! twitter_helper.save_association_data(user_data) message = _('This Twitter account is not associated with a StackGeek account. ' 'Please sign in or create a StackGeek account before continuing.') self.add_message(message, 'warning') self.redirect_to('login') # callback handler for github oauth elif provider_name == "github": # get our request code back from the social login handler above code = self.request.get('code') # create our github auth object (again) scope = 'gist' github_helper = github.GithubAuth(scope) # retrieve the access token using the code and auth object try: access_token = github_helper.get_access_token(code) user_data = github_helper.get_user_info(access_token) except: message = _('An error was encountered while exchanging tokens with Github.') self.add_message(message, 'error') self.redirect_to('edit-profile') return if self.user: # user is already logged in so we set a new association with github user_info = models.User.get_by_id(long(self.user_id)) if models.SocialUser.check_unique(user_info.key, 'github', str(user_data['login'])): social_user = models.SocialUser( user = user_info.key, provider = 'github', uid = str(user_data['login']), access_token = access_token, extra_data = user_data ) social_user.put() message = _('The StackGeek application has been added to your Github account.') self.add_message(message, 'success') else: message = _('The currently logged in Github account is already in use with another account.') self.add_message(message, 'error') self.redirect_to('edit-profile') return # check to see if we are headed anywhere else besides the profile page next_page = utils.read_cookie(self, 'oauth_return_url') utils.write_cookie(self, 'oauth_return_url', '', '/', 15) # try out what we found or redirect to profile if it's a bad value if next_page: try: self.redirect_to(next_page) except: self.redirect_to('edit-profile') else: self.redirect_to('edit-profile') else: # user is not logged in, but is trying to log in via github social_user = models.SocialUser.get_by_provider_and_uid('github', str(user_data['login'])) if social_user: # Social user exists. Need authenticate related site account user = social_user.user.get() self.auth.set_session(self.auth.store.user_to_dict(user), remember=True) logVisit = models.LogVisit( user = user.key, uastring = self.request.user_agent, ip = self.request.remote_addr, timestamp = utils.get_date_time() ) logVisit.put() self.redirect_to('home') else: # Social user does not exists. Need show login and registration forms! message = _('This Github account is not associated with a StackGeek account. ' 'Please sign in or create a StackGeek account before continuing.') self.add_message(message, 'warning') self.redirect_to('login') # google, myopenid, yahoo OpenID Providers elif provider_name in models.SocialUser.open_id_providers(): provider_display_name = models.SocialUser.PROVIDERS_INFO[provider_name]['label'] # get info passed from OpenId Provider from google.appengine.api import users current_user = users.get_current_user() if current_user: if current_user.federated_identity(): uid = current_user.federated_identity() else: uid = current_user.user_id() email = current_user.email() else: message = _('No user authentication information received from %s. ' 'Please ensure you are logging in from an authorized OpenID Provider (OP).' % provider_display_name) self.add_message(message, 'error') return self.redirect_to('login') if self.user: # add social account to user user_info = models.User.get_by_id(long(self.user_id)) if models.SocialUser.check_unique(user_info.key, provider_name, uid): social_user = models.SocialUser( user = user_info.key, provider = provider_name, uid = uid ) social_user.put() message = _('%s association successfully added.' % provider_display_name) self.add_message(message, 'success') else: message = _('This %s account is already in use.' % provider_display_name) self.add_message(message, 'error') self.redirect_to('edit-profile') else: # login with OpenId Provider social_user = models.SocialUser.get_by_provider_and_uid(provider_name, uid) if social_user: # Social user found. Authenticate the user user = social_user.user.get() self.auth.set_session(self.auth.store.user_to_dict(user), remember=True) logVisit = models.LogVisit( user = user.key, uastring = self.request.user_agent, ip = self.request.remote_addr, timestamp = utils.get_date_time() ) logVisit.put() self.redirect_to('home') else: message = _('This OpenID based account is not associated with a StackGeek account. ' 'Please sign in or create a StackGeek account before continuing.') self.add_message(message, 'warning') self.redirect_to('login') else: message = _('This authentication method is not yet implemented!') self.add_message(message, 'warning') self.redirect_to('login')