summaryrefslogtreecommitdiffstats
path: root/news
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 /news
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 'news')
-rw-r--r--news/views.py18
1 files changed, 2 insertions, 16 deletions
diff --git a/news/views.py b/news/views.py
index 0e22ac3..62d30fd 100644
--- a/news/views.py
+++ b/news/views.py
@@ -3,26 +3,12 @@ import markdown
from django import forms
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
-from django.template.defaultfilters import slugify
from django.views.decorators.http import require_POST
from django.views.generic import (DetailView, ListView,
CreateView, UpdateView, DeleteView)
from .models import News
-
-
-def find_unique_slug(newsitem):
- '''Attempt to find a unique slug for this news item.'''
- existing = list(News.objects.values_list(
- 'slug', flat=True).order_by().distinct())
-
- suffixed = slug = slugify(newsitem.title)
- suffix = 0
- while suffixed in existing:
- suffix += 1
- suffixed = "%s-%d" % (slug, suffix)
-
- return suffixed
+from main.utils import find_unique_slug
class NewsForm(forms.ModelForm):
@@ -51,7 +37,7 @@ class NewsCreateView(CreateView):
# special logic, we auto-fill the author and slug fields
newsitem = form.save(commit=False)
newsitem.author = self.request.user
- newsitem.slug = find_unique_slug(newsitem)
+ newsitem.slug = find_unique_slug(News, newsitem.title)
newsitem.save()
return super(NewsCreateView, self).form_valid(form)