Some tweaks for the dev navigation
[archweb_dev-nj.git] / news / views.py
blobb618e2f90d3216482ef226771c0a6ad41ee4a59a
1 from django.http import HttpResponse, HttpResponseRedirect, Http404
2 from django.shortcuts import get_object_or_404
3 from django.contrib.auth.decorators import user_passes_test
4 from django.contrib.auth.models import User
5 from django import forms
6 from archweb_dev.lib.utils import render_response
7 from archweb_dev.news.models import News
8 from datetime import date
10 def view(request, newsid):
11 news = get_object_or_404(News, id=newsid)
12 return render_response(request, 'news/view.html', {'news':news})
14 def list(request):
15 news = News.objects.order_by('-postdate', '-id')
16 return render_response(request, 'news/list.html', {'news':news})
18 @user_passes_test(lambda u: u.has_perm('news.add_news'))
19 def add(request):
20 try:
21 m = User.objects.get(username=request.user.username)
22 except User.DoesNotExist:
23 return render_response(request, 'error_page.html',
24 {'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'})
26 manipulator = News.AddManipulator()
27 if request.POST:
28 data = request.POST.copy()
29 # add in the author ID
30 data['author'] = m.id
31 errors = manipulator.get_validation_errors(data)
32 if not errors:
33 manipulator.do_html2python(data)
34 manipulator.save(data)
35 return HttpResponseRedirect('/news/')
36 else:
37 errors = {}
38 data = {}
40 form = forms.FormWrapper(manipulator, data, errors)
41 return render_response(request, 'news/add.html', {'form': form})
43 @user_passes_test(lambda u: u.has_perm('news.delete_news'))
44 def delete(request, newsid):
45 news = get_object_or_404(News, id=newsid)
46 #if news.author.id != request.user.id:
47 # return render_response(request, 'error_page.html', {'errmsg': 'You do not own this news item'})
48 if request.POST:
49 news.delete()
50 return HttpResponseRedirect('/news/')
51 return render_response(request, 'news/delete.html')
53 @user_passes_test(lambda u: u.has_perm('news.change_news'))
54 def edit(request, newsid):
55 try:
56 m = User.objects.get(username=request.user.username)
57 except User.DoesNotExist:
58 return render_response(request, 'error_page.html',
59 {'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'})
60 try:
61 manipulator = News.ChangeManipulator(newsid)
62 except News.DoesNotExist:
63 raise Http404
65 news = manipulator.original_object
66 # if news.author != m:
67 # return render_response(request, 'error_page.html', {'errmsg': 'You do not own this news item'})
68 if request.POST:
69 data = request.POST.copy()
70 # add in the author ID
71 data['author'] = news.author.id
72 errors = manipulator.get_validation_errors(data)
73 if not errors:
74 manipulator.do_html2python(data)
75 manipulator.save(data)
76 return HttpResponseRedirect('/news/')
77 else:
78 errors = {}
79 data = news.__dict__
81 form = forms.FormWrapper(manipulator, data, errors)
82 return render_response(request, 'news/add.html', {'form': form, 'news':news})
84 # vim: set ts=4 sw=4 et: