Add slugify

This commit is contained in:
László Károlyi 2023-05-21 17:55:58 +02:00
parent 98d72043ab
commit 4dd581abea
Signed by: karolyi
GPG Key ID: 2DCAF25E55735BFE
3 changed files with 23 additions and 2 deletions

View File

@ -6,7 +6,7 @@ This module contains boilerplate for python projects, mostly based on django.
### Django
#### PositiveAutoField
#### `ktools.django.db.models.fields.PositiveAutoField`
A mysql-based `AutoField` that will use an `UNSIGNED INT` for a primary field.
@ -22,3 +22,6 @@ DEFAULT_AUTO_FIELD = 'ktools.django.db.models.fields.PositiveAutoField'
A decorator to cache return values of a method (of a class) until the class is used. Parameters are that of `lru_cache()` since it uses that function under the hood.
#### `ktools.django.utils.text.slugify`
Function to return an ASCII-safe slugified string from any utf-8 string.

View File

@ -6,7 +6,10 @@ setup(
name='py-ktools',
version='1.0.0',
# packages=['ktools'],
install_requires=['typing_extensions~=4.4'],
install_requires=[
'typing_extensions~=4.4',
'Unidecode~=1.3.6'
],
author='László Károlyi',
author_email='laszlo@karolyi.hu',
description='Boilerplate tools for python based projects.',

View File

@ -0,0 +1,15 @@
from django.utils.encoding import force_str
from django.utils.safestring import mark_safe
from django.utils.text import slugify as django_slugify
from unidecode import unidecode
_SLUG_TRANSTABLE = str.maketrans('./_', '---')
def slugify(input_data: str):
'Powerup version of the original django slugify.'
pass_one = unidecode(force_str(input_data))\
.translate(_SLUG_TRANSTABLE)\
.strip('-')
pass_two = django_slugify(value=pass_one)
return mark_safe(pass_two)