def main(request, path): ''' Search for a long link matching the `path` and redirect ''' if len(path) and path[-1] == '/': # Removing trailing slash so "/jobs/" and "/jobs" redirect identically path = path[:-1] link = Link.find_by_hash(path) if link is None: # Try to find a matching short link by removing valid "catchall" suffixes path_prefix, redirect_suffix = suffix_catchall.get_hash_from(path) if redirect_suffix is not None: # If we found a suffix, we try to find a link again with the prefix link = Link.find_by_hash(path_prefix) else: redirect_suffix = None # Instrumentation prefix_tag = 'prefix:' + link.prefix if link else 'Http404' statsd.increment('workforus.clicks', tags=[prefix_tag]) statsd.set('workforus.unique_links', link.hash if link else 'Http404', tags=[prefix_tag]) statsd.set('workforus.unique_ips', get_client_ip(request), tags=['browser:' + get_browser(request)]) # 404 if link not found or register a click if the DB is not in readonly mode if link is None: raise Http404 elif not settings.SITE_READ_ONLY: link.click() # Tweak the redirection link based on the query string, redirection suffix, etc. # FIXME: Handle multiple parameters with the same name in the `url` query = request.GET.copy() if redirect_suffix is not None: query[REDIRECT_PARAM_NAME] = redirect_suffix if bool(query) and REF_PARAM_NAME not in query: # If we specify a non empty query, indicate that the shortener tweaked the url query[REF_PARAM_NAME] = REF_PARAM_DEFAULT_VALUE target_url = url_append_parameters( link.long_url, params_to_replace=query, defaults={REF_PARAM_NAME: REF_PARAM_DEFAULT_VALUE}) # Either redirect the user, or load the target page and display it directly return (proxy if link.act_as_proxy else redirect)(target_url)
def main(request, path): ''' Search for a long link matching the `path` and redirect ''' if len(path) and path[-1] == '/': # Removing trailing slash so "/jobs/" and "/jobs" redirect identically path = path[:-1] link = Link.find_by_hash(path) if link is None: # Try to find a matching short link by removing valid "catchall" suffixes path_prefix, redirect_suffix = suffix_catchall.get_hash_from(path) if redirect_suffix is not None: # If we found a suffix, we try to find a link again with the prefix link = Link.find_by_hash(path_prefix) else: redirect_suffix = None # Instrumentation prefix_tag = 'prefix:' + link.prefix if link else 'Http404' statsd.increment('workforus.clicks', tags=[prefix_tag]) statsd.set('workforus.unique_links', link.hash if link else 'Http404', tags=[prefix_tag]) statsd.set('workforus.unique_ips', get_client_ip(request), tags=['browser:' + get_browser(request)]) # 404 if link not found or register a click if the DB is not in readonly mode if link is None: raise Http404 elif mongoengine_is_primary(): link.click() # Tweak the redirection link based on the query string, redirection suffix, etc. # FIXME: Handle multiple parameters with the same name in the `url` query = request.GET.copy() if redirect_suffix is not None: query[REDIRECT_PARAM_NAME] = redirect_suffix if bool(query) and REF_PARAM_NAME not in query: # If we specify a non empty query, indicate that the shortener tweaked the url query[REF_PARAM_NAME] = REF_PARAM_DEFAULT_VALUE target_url = url_append_parameters( link.long_url, params_to_replace=query, defaults={REF_PARAM_NAME: REF_PARAM_DEFAULT_VALUE} ) # Either redirect the user, or load the target page and display it directly return (proxy if link.act_as_proxy else redirect)(target_url)
def test__get_hash_from(self): self.assertEqual(get_hash_from('azertyuiop'), ('azertyuiop', None)) self.assertEqual(get_hash_from('azerty/uiop'), ('azerty/uiop', None)) self.assertEqual(get_hash_from('a/z/e/r/t/y/u/i/o/p'), ('a/z/e/r/t/y/u/i/o/p', None)) self.assertEqual(get_hash_from('some/hash/recruiter'), ('some/hash', 'recruiter')) self.assertEqual(get_hash_from('some/hash/share'), ('some/hash', 'share')) self.assertEqual(get_hash_from('some/hash/search'), ('some/hash', 'search')) self.assertEqual(get_hash_from('some/hashrecruiter'), ('some/hashrecruiter', None)) self.assertEqual(get_hash_from('some/hashshare'), ('some/hashshare', None)) self.assertEqual(get_hash_from('some/hashsearch'), ('some/hashsearch', None))
def test__get_hash_from(self): self.assertEqual(get_hash_from('azertyuiop'), ('azertyuiop', None)) self.assertEqual(get_hash_from('azerty/uiop'), ('azerty/uiop', None)) self.assertEqual(get_hash_from('a/z/e/r/t/y/u/i/o/p'), ('a/z/e/r/t/y/u/i/o/p', None)) self.assertEqual(get_hash_from('some/hash/referrals'), ('some/hash', 'referrals')) self.assertEqual(get_hash_from('some/hash/recruiter'), ('some/hash', 'recruiter')) self.assertEqual(get_hash_from('some/hash/share'), ('some/hash', 'share')) self.assertEqual(get_hash_from('some/hash/search'), ('some/hash', 'search')) self.assertEqual(get_hash_from('some/hashreferrals'), ('some/hashreferrals', None)) self.assertEqual(get_hash_from('some/hashrecruiter'), ('some/hashrecruiter', None)) self.assertEqual(get_hash_from('some/hashshare'), ('some/hashshare', None)) self.assertEqual(get_hash_from('some/hashsearch'), ('some/hashsearch', None))