def _decorated_view(request, *args, **kwargs): # Get locale from Facebook's `signed_request` signed_request = utils.unwrap_signed_request(request) try: facebook_locale = signed_request['user']['locale'] except KeyError: pass else: # If user's locale isn't supported, get the next best one. # Defaults to en-US if no locale in same language as the # user's is found. best_locale = utils.get_best_locale(request, facebook_locale) prefix = urlresolvers.get_url_prefix() # Compare locales in lowercase just in case. Heh. # If we aren't using the best locale, redirect to it if prefix.locale.lower() != best_locale.lower(): prefix.locale = best_locale locale_url = prefix.fix(request.path_info) query_string = urllib.urlencode(request.GET) final_url = '?'.join([locale_url, query_string]) return redirect(final_url) return view_fn(request, *args, **kwargs)
def test_change_locale_to_hyphen(self): """ Should convert Facebook's underscore locales to hyphen locales. """ payload = tests.create_payload(locale='en_GB') signed_request = tests.create_signed_request(payload) self.request.REQUEST['signed_request'] = signed_request unwrapped_payload = utils.unwrap_signed_request(self.request) eq_(unwrapped_payload['user']['locale'], 'en-GB')
def test_normal_unwrap(self): """ Should unwrap and return the encoded dictionary. """ payload = tests.create_payload(locale='en_GB') signed_request = tests.create_signed_request(payload) self.request.REQUEST['signed_request'] = signed_request # Use hyphen in payload's locale to match util's transformation payload['user']['locale'] = 'en-GB' eq_(utils.unwrap_signed_request(self.request), payload)
def _decorated_view(request, *args, **kwargs): # Get custom data from Facebook's `signed_request` signed_request = utils.unwrap_signed_request(request) try: app_data = signed_request['app_data'] except KeyError: pass else: # Add it to the GET dictionary new_get = request.GET.copy() new_get.update(app_data) request.GET = new_get return view_fn(request, *args, **kwargs)
def test_empty_signed_request(self): """ If signed_request isn't set, should return empty dict. """ eq_(utils.unwrap_signed_request(self.request), {})