Remove django-shorturls with my own implementation, since it's incompatible with Django 2 and unmaintained
This commit is contained in:
parent
27e0cb9a34
commit
098284a617
18 changed files with 252 additions and 184 deletions
1
lemonshort/__init__.py
Normal file
1
lemonshort/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
default_app_config = 'lemonshort.apps.LemonshortConfig'
|
5
lemonshort/apps.py
Normal file
5
lemonshort/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class LemonshortConfig(AppConfig):
|
||||
name = 'lemonshort'
|
15
lemonshort/convert.py
Normal file
15
lemonshort/convert.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
from baseconv import BaseConverter
|
||||
from string import ascii_lowercase, ascii_uppercase
|
||||
|
||||
# We have to create this collection ourselves because we want uppercase then
|
||||
# lowercase, and string.ascii_letters is lowercase then uppercase.
|
||||
chars = ascii_uppercase + ascii_lowercase
|
||||
conv = BaseConverter(chars)
|
||||
|
||||
|
||||
def abc_to_id(abc):
|
||||
return int(conv.decode(abc))
|
||||
|
||||
|
||||
def id_to_abc(id):
|
||||
return conv.encode(id)
|
16
lemonshort/short_url.py
Normal file
16
lemonshort/short_url.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
|
||||
from .convert import id_to_abc
|
||||
|
||||
prefixes = {}
|
||||
|
||||
|
||||
def short_url(entity):
|
||||
if not prefixes:
|
||||
for k, m in settings.SHORTEN_MODELS.items():
|
||||
prefixes[apps.get_model(m)] = k
|
||||
base = '/'
|
||||
if hasattr(settings, 'SHORT_BASE_URL'):
|
||||
base = settings.SHORT_BASE_URL
|
||||
return base + prefixes[type(entity)] + id_to_abc(entity.id)
|
0
lemonshort/tests/__init__.py
Normal file
0
lemonshort/tests/__init__.py
Normal file
30
lemonshort/tests/convert.py
Normal file
30
lemonshort/tests/convert.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
from .. import convert
|
||||
|
||||
|
||||
def test_abc_to_id():
|
||||
samples = {
|
||||
'A': 0,
|
||||
'B': 1,
|
||||
'Y': 24,
|
||||
'a': 26,
|
||||
'b': 27,
|
||||
'y': 50,
|
||||
'BA': 52,
|
||||
'BAB': 2705,
|
||||
}
|
||||
for abc, id in samples.items():
|
||||
assert convert.abc_to_id(abc) == id
|
||||
|
||||
|
||||
def test_id_to_abc():
|
||||
samples = {
|
||||
1: 'B',
|
||||
24: 'Y',
|
||||
26: 'a',
|
||||
52: 'BA',
|
||||
78: 'Ba',
|
||||
104: 'CA',
|
||||
130: 'Ca',
|
||||
}
|
||||
for id, abc in samples.items():
|
||||
assert convert.id_to_abc(id) == abc
|
10
lemonshort/urls.py
Normal file
10
lemonshort/urls.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from django.conf import settings
|
||||
from django.conf.urls import url
|
||||
|
||||
from .views import unshort
|
||||
|
||||
app_name = 'lemonshort'
|
||||
urlpatterns = tuple(
|
||||
url(r'^{0!s}(?P<tiny>\w+)$'.format(k), unshort, kwargs={'model': m})
|
||||
for k, m in settings.SHORTEN_MODELS.items()
|
||||
)
|
9
lemonshort/views.py
Normal file
9
lemonshort/views.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from django.apps import apps
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
|
||||
from .convert import abc_to_id
|
||||
|
||||
|
||||
def unshort(request, model, tiny):
|
||||
entity = get_object_or_404(apps.get_model(model), pk=abc_to_id(tiny))
|
||||
return redirect(entity, permanent=True)
|
Loading…
Add table
Add a link
Reference in a new issue