def test_not_in_whitelist(self): """ If the given locale is not in the whitelist, default back to en-us. """ request = self.factory.get('/') activate_locale(request, 'de') eq_(request.locale, 'en-us')
def test_not_in_whitelist(self): """ If the given locale is not in the whitelist, default back to en-us. """ request = self.factory.get("/") activate_locale(request, "de") eq_(request.locale, "en-us")
def test_dev_dont_limit_locales(self): """ If settings.DEV is True, do not verify that a locale is in the FACEBOOK_LOCALES list. """ request = self.factory.get('/') activate_locale(request, 'es') eq_(request.locale, 'es')
def _app_context(self, in_facebook_app, path="/", user=None, in_app=True, locale="en-US"): in_facebook_app.return_value = in_app request = self.factory.get(path) request.user = user activate_locale(request, locale) if request.user is None: request.user = AnonymousFacebookUser() return app_context(request)
def _app_context(self, in_facebook_app, path='/', user=None, in_app=True, locale='en-US'): in_facebook_app.return_value = in_app request = self.factory.get(path) request.user = user activate_locale(request, locale) if request.user is None: request.user = AnonymousFacebookUser() return app_context(request)
def process_request(self, request): # Exit early if we are not in the Facebook app section of the site to # avoid clashing with the Django auth middleware. if not in_facebook_app(request): return None # Default to an anonymous user. request.user = AnonymousFacebookUser() if SESSION_KEY in request.session: try: user = FacebookUser.objects.get(id=request.session[SESSION_KEY]) except FacebookUser.DoesNotExist: return None request.user = user # Activate locale now that we know who the user is. activate_locale(request, user.locale)
def load_app(request): """ Create or authenticate the Facebook user and direct them to the correct area of the app upon their entry. """ # Temporary measure to handle when Facebook does a GET to the main URL when # a logged-out user views the app. In the future we should show a promo # page instead. if request.method != "POST": return request_authorization(request) signed_request = request.POST.get("signed_request", None) if signed_request is None: # App wasn't loaded within a canvas, redirect to the home page. return redirect("home") decoded_request = decode_signed_request(signed_request, settings.FACEBOOK_APP_SECRET) if decoded_request is None: return redirect("home") # If user is using Safari, we need to apply the cookie workaround. useragent = request.META.get("HTTP_USER_AGENT", "") using_safari = "Safari" in useragent and not "Chrome" in useragent workaround_applied = SAFARI_WORKAROUND_KEY in request.COOKIES if using_safari and not workaround_applied: return fb_redirect(request, absolutify(reverse("facebook.safari_workaround"))) user, created = FacebookUser.objects.get_or_create_user_from_decoded_request(decoded_request) if user is None: # User has yet to authorize the app, offer authorization. return request_authorization(request) # Attach country data to the user object. This can only be retrieved from # the decoded request, so we add it here and login saves it. user.country = decoded_request["user"].get("country", user.country) # User has been authed, let's log them in. login(request, user) # Normally the FacebookAuthenticationMiddleware activates the locale for # the user, but since it does not run for this view, we need to activate it # manually. activate_locale(request, user.locale) return banner_list(request)
def process_request(self, request): # Exit early if we are not in the Facebook app section of the site to # avoid clashing with the Django auth middleware. if not in_facebook_app(request): return None # Default to an anonymous user. request.user = AnonymousFacebookUser() locale = None try: user = FacebookUser.objects.get(id=request.session[SESSION_KEY]) request.user = user locale = user.locale except (FacebookUser.DoesNotExist, KeyError): pass if locale is None: # Since we can't get their locale from their user data, we'll use # funfactory's prefixer instead. prefixer = Prefixer(request) locale = prefixer.get_language() activate_locale(request, locale)
def test_language_code_in_whitelist(self): """If only a locale's language code is in the whitelist, use it.""" request = self.factory.get('/') activate_locale(request, 'de-de') eq_(get_language(), 'de') eq_(request.locale, 'de')
def test_locale_in_whitelist(self): """If a locale is in the whitelist, use it.""" request = self.factory.get('/') activate_locale(request, 'en-us') eq_(request.locale, 'en-us')
def test_testing_dont_set_request_locale(self): """If settings.TEST is True, do not set the locale on the request.""" request = self.factory.get('/') activate_locale(request, 'en-us') eq_(getattr(request, 'locale', None), None)