Skip to content

Commit

Permalink
WIP Redireciona por conta de querystring
Browse files Browse the repository at this point in the history
  • Loading branch information
berinhard committed Oct 16, 2020
1 parent c800f07 commit cf9ecf1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
36 changes: 32 additions & 4 deletions core/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import hashlib
import random
import string
from copy import deepcopy
from collections import OrderedDict, namedtuple
from textwrap import dedent
from urllib.parse import urlparse
from urllib.parse import urlparse, urlencode

import django.contrib.postgres.indexes as pg_indexes
import django.db.models.indexes as django_indexes
Expand Down Expand Up @@ -724,12 +725,39 @@ def redirect_map(self):
def redirect_from(cls, request):
path = request.path
redirects = {}
fields_map = {}

for data_url_redirect in cls.objects.all().iterator():
redirects.update(**data_url_redirect.redirect_map)
for r in cls.objects.all().iterator():
redirects.update(**r.redirect_map)

key = (r.dataset_dest, r.tablename_prev, r.field_prev)
fields_map[key] = r.field_dest

# Order prefixes begining by the most complex ones
redirect_url = ""
for url_prefix in sorted(redirects, reverse=True):
if path.startswith(url_prefix):
redirect_url_prefix = redirects[url_prefix]
return path.replace(url_prefix, redirect_url_prefix)
redirect_url = path.replace(url_prefix, redirect_url_prefix)
break


qs = ''
query_params = getattr(request, "query_params", {})
if redirect_url and query_params:
redirect_qs = deepcopy(request.query_params)
for key in fields_map:
dataset, tablename, field_prev = key
has_field_update = all([
f"/{dataset}/" in redirect_url,
f"/{tablename}/" in redirect_url,
field_prev in redirect_qs
])
if has_field_update:
value = redirect_qs[field_prev]
redirect_qs.pop(field_prev)
redirect_qs[fields_map[key]] = value

qs = urlencode(redirect_qs)

return redirect_url + (f'?{qs}' if qs else '')
14 changes: 14 additions & 0 deletions core/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,17 @@ def test_avoid_infinite_redirect_if_same_dataset_config_in_table(self):
response = self.client.get(url)

assert 404 == response.status_code

def test_redirect_dataset_plus_field_name_redirect(self):
ds_redirect = self.dataset_redirects[0]
ds_redirect.tablename_prev = "caso2020"
ds_redirect.tablename_dest = "caso2020"
ds_redirect.field_prev = "query1"
ds_redirect.field_dest = "newquery"
ds_redirect.save()

url = reverse("api-v1:dataset-table-data", args=[ds_redirect.dataset_prev, "caso2020"])
redirect_url = reverse("api-v1:dataset-table-data", args=[ds_redirect.dataset_dest, "caso2020"]) + "?newquery=foo&query2=bar"
response = self.client.get(url + "?query1=foo&query2=bar")

self.assertRedirects(response, redirect_url, fetch_redirect_response=False)

0 comments on commit cf9ecf1

Please sign in to comment.