summaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-12-28 17:12:09 +0100
committerDan McGee <dan@archlinux.org>2012-12-28 21:48:29 +0100
commit563a618e697c918c2a76c63a5217047a8d3c1489 (patch)
treeed3d5a480fc8f8b5df6412130d940de7017ed32f /main
parent0c94cc4465530866da7b6437975a287aa7f063a8 (diff)
downloadarchweb-563a618e697c918c2a76c63a5217047a8d3c1489.tar.gz
archweb-563a618e697c918c2a76c63a5217047a8d3c1489.tar.xz
Move slug creation helper to main/utils
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'main')
-rw-r--r--main/utils.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/main/utils.py b/main/utils.py
index 17ca386..cdd4ff7 100644
--- a/main/utils.py
+++ b/main/utils.py
@@ -9,6 +9,7 @@ import hashlib
from django.core.cache import cache
from django.db import connections, router
from django.utils.timezone import now
+from django.template.defaultfilters import slugify
CACHE_TIMEOUT = 1800
@@ -118,6 +119,20 @@ def set_created_field(sender, **kwargs):
obj.last_modified = time
+def find_unique_slug(model, title):
+ '''Attempt to find a unique slug for this model with given title.'''
+ existing = set(model.objects.values_list(
+ 'slug', flat=True).order_by().distinct())
+
+ suffixed = slug = slugify(title)
+ suffix = 0
+ while suffixed in existing:
+ suffix += 1
+ suffixed = "%s-%d" % (slug, suffix)
+
+ return suffixed
+
+
def database_vendor(model, mode='read'):
if mode == 'read':
database = router.db_for_read(model)