summaryrefslogtreecommitdiff
path: root/news
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-04-26 22:19:02 -0500
committerDan McGee <dan@archlinux.org>2012-04-27 09:12:26 -0500
commit3f150dcfade9443b3435309cb928f330966eb749 (patch)
tree837597d97eeb2e1a64e718978a9852fc9971e5d9 /news
parent5780cf2e217ce0a60d09cd2d4bbc8fa6e5c29689 (diff)
Migrate news views to class-based generic views
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'news')
-rw-r--r--news/urls.py25
-rw-r--r--news/views.py124
2 files changed, 75 insertions, 74 deletions
diff --git a/news/urls.py b/news/urls.py
index 10020f31..0eec6d86 100644
--- a/news/urls.py
+++ b/news/urls.py
@@ -1,14 +1,25 @@
from django.conf.urls import patterns
+from django.contrib.auth.decorators import permission_required
+from .views import (NewsDetailView, NewsListView,
+ NewsCreateView, NewsEditView, NewsDeleteView)
+
urlpatterns = patterns('news.views',
- (r'^$', 'news_list', {}, 'news-list'),
- (r'^add/$', 'add'),
- (r'^preview/$', 'preview'),
+ (r'^$',
+ NewsListView.as_view(), {}, 'news-list'),
+
+ (r'^preview/$', 'preview'),
# old news URLs, permanent redirect view so we don't break all links
- (r'^(?P<object_id>\d+)/$', 'view_redirect'),
- (r'^(?P<slug>[-\w]+)/$', 'view'),
- (r'^(?P<slug>[-\w]+)/edit/$', 'edit'),
- (r'^(?P<slug>[-\w]+)/delete/$', 'delete'),
+ (r'^(?P<object_id>\d+)/$', 'view_redirect'),
+
+ (r'^add/$',
+ permission_required('news.add_news')(NewsCreateView.as_view())),
+ (r'^(?P<slug>[-\w]+)/$',
+ NewsDetailView.as_view()),
+ (r'^(?P<slug>[-\w]+)/edit/$',
+ permission_required('news.change_news')(NewsEditView.as_view())),
+ (r'^(?P<slug>[-\w]+)/delete/$',
+ permission_required('news.delete_news')(NewsDeleteView.as_view())),
)
# vim: set ts=4 sw=4 et:
diff --git a/news/views.py b/news/views.py
index 7ac009ba..268f0523 100644
--- a/news/views.py
+++ b/news/views.py
@@ -1,38 +1,15 @@
+import markdown
+
from django import forms
-from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.defaultfilters import slugify
-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
-
-import markdown
+from django.views.decorators.http import require_POST
+from django.views.generic import (DetailView, ListView,
+ CreateView, UpdateView, DeleteView)
from .models import News
-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')
-
-#TODO: May as well use a date-based list here sometime
-def news_list(request):
- return list_detail.object_list(request,
- News.objects.all().select_related('author').defer('content'),
- paginate_by=50,
- template_name="news/list.html",
- template_object_name="news")
-
-class NewsForm(forms.ModelForm):
- class Meta:
- model = News
- exclude = ('id', 'slug', 'author', 'postdate')
def find_unique_slug(newsitem):
'''Attempt to find a unique slug for this news item.'''
@@ -46,46 +23,59 @@ def find_unique_slug(newsitem):
return suffixed
-@permission_required('news.add_news')
-@never_cache
-def add(request):
- if request.POST:
- form = NewsForm(request.POST)
- if form.is_valid():
- newsitem = form.save(commit=False)
- newsitem.author = request.user
- newsitem.slug = find_unique_slug(newsitem)
- newsitem.save()
- return redirect(newsitem)
- else:
- form = NewsForm()
- return direct_to_template(request, 'news/add.html', { 'form': form })
-
-@permission_required('news.delete_news')
-@never_cache
-def delete(request, slug):
- return create_update.delete_object(request,
- News,
- 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, slug):
- return create_update.update_object(request,
- slug=slug,
- form_class=NewsForm,
- template_name="news/add.html")
-
-@permission_required('news.change_news')
-@never_cache
+
+class NewsForm(forms.ModelForm):
+ class Meta:
+ model = News
+ exclude = ('id', 'slug', 'author', 'postdate')
+
+
+class NewsDetailView(DetailView):
+ model = News
+ template_name = "news/view.html"
+
+
+class NewsListView(ListView):
+ queryset = News.objects.all().select_related('author').defer('content')
+ template_name = "news/list.html"
+ paginate_by = 50
+
+
+class NewsCreateView(CreateView):
+ model = News
+ form_class = NewsForm
+ template_name = "news/add.html"
+
+ def form_valid(self, form):
+ # 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.save()
+ return super(NewsCreateView, self).form_valid(form)
+
+
+class NewsEditView(UpdateView):
+ model = News
+ form_class = NewsForm
+ template_name = "news/add.html"
+
+
+class NewsDeleteView(DeleteView):
+ model = News
+ template_name = "news/delete.html"
+ success_url = "/news/"
+
+
+def view_redirect(request, object_id):
+ newsitem = get_object_or_404(News, pk=object_id)
+ return redirect(newsitem, permanent=True)
+
+
+@require_POST
def preview(request):
- markup = ''
- if request.POST:
- data = request.POST.get('data', '')
- markup = markdown.markdown(data)
+ data = request.POST.get('data', '')
+ markup = markdown.markdown(data)
return HttpResponse(markup)
# vim: set ts=4 sw=4 et: