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
.utils
import render_template
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_template('news/view.html', request
, {'news':news
})
15 news
= News
.objects
.order_by('-postdate', '-id')
16 return render_template('news/list.html', request
, {'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_template('error_page.html', request
,
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_template('news/add.html', request
, {'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_template('error_page.html', request, {'errmsg': 'You do not own this news item'})
50 return HttpResponseRedirect('/news/')
51 return render_template('news/delete.html', request
)
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_template('error_page.html', request
,
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_template('error_page.html', request, {'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_template('news/add.html', request
, {'form': form
, 'news':news
})