summaryrefslogtreecommitdiff
path: root/news/views.py
blob: 7fa92c8aa146ba51e65a896741c176f3a9332de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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.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 .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 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', 'author', 'postdate')

@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.save()
            return redirect(newsitem.get_absolute_url())
    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
def preview(request):
    markup = ''
    if request.POST:
        data = request.POST.get('data', '')
        markup = markdown.markdown(data)
    return HttpResponse(markup)

# vim: set ts=4 sw=4 et: