From 683adc1b464f5e2fabfaa968da59179d9b7474f0 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Sat, 12 Mar 2022 15:04:05 +1100 Subject: [PATCH] Use proper path converter for lemonshort --- lemonshort/convert.py | 11 ++++++----- lemonshort/short_url.py | 4 ++-- lemonshort/tests/convert.py | 8 +++++--- lemonshort/urls.py | 7 +++++-- lemonshort/views.py | 4 ++-- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lemonshort/convert.py b/lemonshort/convert.py index 8aeec0d..936df44 100644 --- a/lemonshort/convert.py +++ b/lemonshort/convert.py @@ -6,10 +6,11 @@ from string import ascii_lowercase, ascii_uppercase chars = ascii_uppercase + ascii_lowercase conv = BaseConverter(chars) +class AbcIdConverter: + regex = '[a-zA-Z]+' -def abc_to_id(abc): - return int(conv.decode(abc)) + def to_python(self, value: str) -> int: + return int(conv.decode(value)) - -def id_to_abc(id): - return conv.encode(id) + def to_url(self, value: int) -> str: + return conv.encode(value) diff --git a/lemonshort/short_url.py b/lemonshort/short_url.py index 7e1d5c0..76d7c62 100644 --- a/lemonshort/short_url.py +++ b/lemonshort/short_url.py @@ -2,7 +2,7 @@ from django.apps import apps from django.conf import settings from typing import Any, Dict, Type -from .convert import id_to_abc +from .convert import AbcIdConverter prefixes = {} # type: Dict[Type[Any], str] @@ -14,4 +14,4 @@ def short_url(entity): base = '/' if hasattr(settings, 'SHORT_BASE_URL'): base = settings.SHORT_BASE_URL - return base + prefixes[type(entity)] + id_to_abc(entity.id) + return base + prefixes[type(entity)] + AbcIdConverter().to_url(entity.id) diff --git a/lemonshort/tests/convert.py b/lemonshort/tests/convert.py index 76363dd..80d6299 100644 --- a/lemonshort/tests/convert.py +++ b/lemonshort/tests/convert.py @@ -1,7 +1,7 @@ from .. import convert -def test_abc_to_id(): +def test_to_python(): samples = { 'A': 0, 'B': 1, @@ -12,8 +12,9 @@ def test_abc_to_id(): 'BA': 52, 'BAB': 2705, } + converter = convert.AbcIdConverter() for abc, id in samples.items(): - assert convert.abc_to_id(abc) == id + assert converter.to_python(abc) == id def test_id_to_abc(): @@ -26,5 +27,6 @@ def test_id_to_abc(): 104: 'CA', 130: 'Ca', } + converter = convert.AbcIdConverter() for id, abc in samples.items(): - assert convert.id_to_abc(id) == abc + assert converter.to_url(id) == abc diff --git a/lemonshort/urls.py b/lemonshort/urls.py index 4f03b26..7fcf49c 100644 --- a/lemonshort/urls.py +++ b/lemonshort/urls.py @@ -1,10 +1,13 @@ from django.conf import settings -from django.urls import path +from django.urls import path, register_converter +from .convert import AbcIdConverter from .views import unshort +register_converter(AbcIdConverter, 'abc_id') + app_name = 'lemonshort' urlpatterns = tuple( - path('{0!s}'.format(k), unshort, name=m, kwargs={'model': m}) + path('{0!s}'.format(k), unshort, name=m, kwargs={'model': m}) for k, m in settings.SHORTEN_MODELS.items() ) diff --git a/lemonshort/views.py b/lemonshort/views.py index ba0b6f3..e33b7dc 100644 --- a/lemonshort/views.py +++ b/lemonshort/views.py @@ -1,9 +1,9 @@ from django.apps import apps from django.shortcuts import get_object_or_404, redirect -from .convert import abc_to_id +from .convert import AbcIdConverter def unshort(request, model, tiny): - entity = get_object_or_404(apps.get_model(model), pk=abc_to_id(tiny)) + entity = get_object_or_404(apps.get_model(model), pk=tiny) return redirect(entity, permanent=True)