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
})
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'))
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()
28 data
= request
.POST
.copy()
29 # add in the author ID
31 errors
= manipulator
.get_validation_errors(data
)
33 manipulator
.do_html2python(data
)
34 manipulator
.save(data
)
35 return HttpResponseRedirect('/news/')
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'})
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
):
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.'})
61 manipulator
= News
.ChangeManipulator(newsid
)
62 except News
.DoesNotExist
:
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'})
69 data
= request
.POST
.copy()
70 # add in the author ID
71 data
['author'] = news
.author
.id
72 errors
= manipulator
.get_validation_errors(data
)
74 manipulator
.do_html2python(data
)
75 manipulator
.save(data
)
76 return HttpResponseRedirect('/news/')
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: