summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2010-09-20 16:10:09 -0500
committerDan McGee <dan@archlinux.org>2010-09-21 09:10:02 -0500
commit41dc37c8126f062257ce7d5fc8fcf84286b55a88 (patch)
treec73e8f661ffb68d036f528257148243036b28a82
parent7c92ddbd3c86d6aaa34abd4b2d3f12775801b50b (diff)
Update news views to use slug instead of ID
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--news/models.py2
-rw-r--r--news/views.py19
-rw-r--r--templates/news/list.html4
-rw-r--r--templates/news/view.html4
-rw-r--r--urls.py14
5 files changed, 25 insertions, 18 deletions
diff --git a/news/models.py b/news/models.py
index 88794cbb..6c8a7039 100644
--- a/news/models.py
+++ b/news/models.py
@@ -12,7 +12,7 @@ class News(models.Model):
content = models.TextField()
def get_absolute_url(self):
- return '/news/%i/' % self.id
+ return '/news/%s/' % self.slug
def __unicode__(self):
return self.title
diff --git a/news/views.py b/news/views.py
index d7148d5b..7fa92c8a 100644
--- a/news/views.py
+++ b/news/views.py
@@ -1,7 +1,7 @@
from django import forms
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse
-from django.shortcuts import redirect
+from django.shortcuts import get_object_or_404, redirect
from django.views.decorators.cache import never_cache
from django.views.generic import list_detail, create_update
from django.views.generic.simple import direct_to_template
@@ -10,8 +10,13 @@ import markdown
from .models import News
-def view(request, newsid):
- return list_detail.object_detail(request, News.objects.all(), newsid,
+def view_redirect(request, object_id):
+ newsitem = get_object_or_404(News, pk=object_id)
+ return redirect(newsitem, permanent=True)
+
+def view(request, slug=None):
+ return list_detail.object_detail(request, News.objects.all(),
+ slug=slug,
template_name="news/view.html",
template_object_name='news')
@@ -44,19 +49,19 @@ def add(request):
@permission_required('news.delete_news')
@never_cache
-def delete(request, newsid):
+def delete(request, slug):
return create_update.delete_object(request,
News,
- object_id=newsid,
+ slug=slug,
post_delete_redirect='/news/',
template_name='news/delete.html',
template_object_name='news')
@permission_required('news.change_news')
@never_cache
-def edit(request, newsid):
+def edit(request, slug):
return create_update.update_object(request,
- object_id=newsid,
+ slug=slug,
form_class=NewsForm,
template_name="news/add.html")
diff --git a/templates/news/list.html b/templates/news/list.html
index 14ba79b6..26e8c268 100644
--- a/templates/news/list.html
+++ b/templates/news/list.html
@@ -34,11 +34,11 @@
<td>{{ item.author.get_full_name }}</td>
{% if perms.main.change_news %}
<td>
- <a href="/news/edit/{{ item.id }}/"
+ <a href="{{ item.get_absolute_url }}edit/"
title="Edit: {{ item.title }}">Edit</a>
{% endif %}
{% if perms.main.delete_news %}
- &nbsp;&nbsp;<a href="/news/delete/{{ item.id }}/"
+ &nbsp;&nbsp;<a href="{{ item.get_absolute_url }}delete/"
title="Delete: {{ item.title }}">Delete</a>
</td>
{% endif %}
diff --git a/templates/news/view.html b/templates/news/view.html
index 1c92fe78..86a412de 100644
--- a/templates/news/view.html
+++ b/templates/news/view.html
@@ -9,9 +9,9 @@
{% if perms.main.change_news %}
<ul class="admin-actions">
- <li><a href="/news/edit/{{ news.id }}/" title="Edit this article">Edit News Item</a></li>
+ <li><a href="edit/" title="Edit this article">Edit News Item</a></li>
{% if perms.main.delete_news %}
- <li><a href="/news/delete/{{ news.id }}/" title="Delete this article">Delete News Item</a></li>
+ <li><a href="delete/" title="Delete this article">Delete News Item</a></li>
{% endif %}
</ul>
{% endif %}
diff --git a/urls.py b/urls.py
index 3f394426..5ff57970 100644
--- a/urls.py
+++ b/urls.py
@@ -62,12 +62,14 @@ urlpatterns = patterns('',
(r'^todo/$', 'todolists.views.list'),
(r'^todolists/$', 'todolists.views.public_list'),
- (r'^news/(\d+)/$', 'news.views.view'),
- (r'^news/add/$', 'news.views.add'),
- (r'^news/edit/(\d+)/$', 'news.views.edit'),
- (r'^news/delete/(\d+)/$', 'news.views.delete'),
- (r'^news/preview/$', 'news.views.preview'),
- (r'^news/$', 'news.views.list', {}, 'news-list'),
+ (r'^news/add/$', 'news.views.add'),
+ (r'^news/preview/$', 'news.views.preview'),
+ # old news URLs, permanent redirect view so we don't break all links
+ (r'^news/(?P<object_id>\d+)/$', 'news.views.view_redirect'),
+ (r'^news/(?P<slug>[-\w]+)/$', 'news.views.view'),
+ (r'^news/(?P<slug>[-\w]+)/edit/$', 'news.views.edit'),
+ (r'^news/(?P<slug>[-\w]+)/delete/$', 'news.views.delete'),
+ (r'^news/$', 'news.views.list', {}, 'news-list'),
(r'^mirrors/$', 'devel.views.mirrorlist', {}, 'mirrors-list'),