diff options
Diffstat (limited to 'news')
-rw-r--r-- | news/__init__.py | 0 | ||||
-rw-r--r-- | news/models.py | 19 | ||||
-rw-r--r-- | news/views.py | 82 |
3 files changed, 101 insertions, 0 deletions
diff --git a/news/__init__.py b/news/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/news/__init__.py diff --git a/news/models.py b/news/models.py new file mode 100644 index 00000000..acd9667c --- /dev/null +++ b/news/models.py @@ -0,0 +1,19 @@ +from django.db import models +from django.contrib.auth.models import User +import re +from archlinux.utils import Stripper + +class News(models.Model): + id = models.AutoField(primary_key=True) + author = models.ForeignKey(User) + postdate = models.DateField(auto_now_add=True) + title = models.CharField(maxlength=255) + content = models.TextField() + class Meta: + db_table = 'news' + verbose_name_plural = 'news' + get_latest_by = 'postdate' + ordering = ['-postdate', '-id'] + + def get_absolute_url(self): + return '/news/%i/' % self.id diff --git a/news/views.py b/news/views.py new file mode 100644 index 00000000..29f20445 --- /dev/null +++ b/news/views.py @@ -0,0 +1,82 @@ +from django.http import HttpResponse, HttpResponseRedirect, Http404 +from django.shortcuts import get_object_or_404 +from django.contrib.auth.decorators import user_passes_test +from django.contrib.auth.models import User +from django import forms +from archlinux.utils import render_template +from archlinux.news.models import News +from datetime import date + +def view(request, newsid): + news = get_object_or_404(News, id=newsid) + return render_template('news/view.html', request, {'news':news}) + +def list(request): + news = News.objects.order_by('-postdate', '-id') + return render_template('news/list.html', request, {'news':news}) + +@user_passes_test(lambda u: u.has_perm('news.add_news')) +def add(request): + try: + m = User.objects.get(username=request.user.username) + except User.DoesNotExist: + return render_template('error_page.html', request, + {'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'}) + + manipulator = News.AddManipulator() + if request.POST: + data = request.POST.copy() + # add in the author ID + data['author'] = m.id + errors = manipulator.get_validation_errors(data) + if not errors: + manipulator.do_html2python(data) + manipulator.save(data) + return HttpResponseRedirect('/news/') + else: + errors = {} + data = {} + + form = forms.FormWrapper(manipulator, data, errors) + return render_template('news/add.html', request, {'form': form}) + +@user_passes_test(lambda u: u.has_perm('news.delete_news')) +def delete(request, newsid): + news = get_object_or_404(News, id=newsid) + #if news.author.id != request.user.id: + # return render_template('error_page.html', request, {'errmsg': 'You do not own this news item'}) + if request.POST: + news.delete() + return HttpResponseRedirect('/news/') + return render_template('news/delete.html', request) + +@user_passes_test(lambda u: u.has_perm('news.change_news')) +def edit(request, newsid): + try: + m = User.objects.get(username=request.user.username) + except User.DoesNotExist: + return render_template('error_page.html', request, + {'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'}) + try: + manipulator = News.ChangeManipulator(newsid) + except News.DoesNotExist: + raise Http404 + + news = manipulator.original_object +# if news.author != m: +# return render_template('error_page.html', request, {'errmsg': 'You do not own this news item'}) + if request.POST: + data = request.POST.copy() + # add in the author ID + data['author'] = news.author.id + errors = manipulator.get_validation_errors(data) + if not errors: + manipulator.do_html2python(data) + manipulator.save(data) + return HttpResponseRedirect('/news/') + else: + errors = {} + data = news.__dict__ + + form = forms.FormWrapper(manipulator, data, errors) + return render_template('news/add.html', request, {'form': form, 'news':news}) |