summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDusty Phillips <buchuki@gmail.com>2008-10-05 21:06:00 -0400
committerDusty Phillips <buchuki@gmail.com>2008-10-05 21:06:00 -0400
commita00f85e1332ca973cec807693e383accd9cdc864 (patch)
tree6c99f0ccf8177a0c88f67c5689d837d85aeb633c
parent06963130c85f8632b898a2d98e7931951c7735a4 (diff)
port news to django 1.0 using generic views
-rw-r--r--main/models.py4
-rw-r--r--news/views.py93
-rw-r--r--templates/news/add.html9
-rw-r--r--templates/news/delete.html11
-rw-r--r--templates/news/list.html2
5 files changed, 39 insertions, 80 deletions
diff --git a/main/models.py b/main/models.py
index f988996a..a2753bc3 100644
--- a/main/models.py
+++ b/main/models.py
@@ -1,6 +1,7 @@
from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
+from archweb_dev.main.middleware import get_user
import re
###########################
@@ -135,7 +136,8 @@ class Donor(models.Model):
class News(models.Model):
id = models.AutoField(primary_key=True)
- author = models.ForeignKey(User, related_name='news_author')
+ author = models.ForeignKey(User, related_name='news_author',
+ default=get_user)
postdate = models.DateField(auto_now_add=True)
title = models.CharField(max_length=255)
content = models.TextField()
diff --git a/news/views.py b/news/views.py
index 05b5a38e..6fa0d2b5 100644
--- a/news/views.py
+++ b/news/views.py
@@ -1,85 +1,48 @@
-from django.http import HttpResponse, HttpResponseRedirect, Http404
+from django import forms
+from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import permission_required
-from django.contrib.auth.models import User
-from django import forms
from archweb_dev.main.utils import render_response
from archweb_dev.main.models import News
-from datetime import date
+
+from django.views.generic import list_detail, create_update
def view(request, newsid):
- news = get_object_or_404(News, id=newsid)
- return render_response(request, 'news/view.html', {'news':news})
+ return list_detail.object_detail(request, News.objects.all(), newsid,
+ template_name="news/view.html",
+ template_object_name='news')
+#TODO: May as well use a date-based list here sometime
def list(request):
- news = News.objects.order_by('-postdate', '-id')
- return render_response(request, 'news/list.html', {'news':news})
+ return list_detail.object_list(request, News.objects.all(),
+ template_name="news/list.html",
+ template_object_name="news")
+
+class NewsForm(forms.ModelForm):
+ class Meta:
+ model = News
+ exclude=('id', 'author', 'postdate')
@permission_required('main.add_news')
def add(request):
- try:
- m = User.objects.get(username=request.user.username)
- except User.DoesNotExist:
- return render_response(request, 'error_page.html',
- {'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_response(request, 'news/add.html', {'form': form})
+ return create_update.create_object(request,
+ form_class=NewsForm,
+ template_name='news/add.html')
@permission_required('main.delete_news')
def delete(request, newsid):
- news = get_object_or_404(News, id=newsid)
- #if news.author.id != request.user.id:
- # return render_response(request, 'error_page.html', {'errmsg': 'You do not own this news item'})
- if request.POST:
- news.delete()
- return HttpResponseRedirect('/news/')
- return render_response(request, 'news/delete.html')
+ return create_update.delete_object(request,
+ News,
+ object_id=newsid,
+ post_delete_redirect='/news/',
+ template_name='news/delete.html',
+ template_object_name='news')
@permission_required('main.change_news')
def edit(request, newsid):
- try:
- m = User.objects.get(username=request.user.username)
- except User.DoesNotExist:
- return render_response(request, 'error_page.html',
- {'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_response(request, 'error_page.html', {'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_response(request, 'news/add.html', {'form': form, 'news':news})
+ return create_update.update_object(request, object_id=newsid,
+ form_class=NewsForm,
+ template_name="news/add.html")
# vim: set ts=4 sw=4 et:
diff --git a/templates/news/add.html b/templates/news/add.html
index 702d7ab7..80fccc3d 100644
--- a/templates/news/add.html
+++ b/templates/news/add.html
@@ -2,20 +2,15 @@
{% block content %}
<div class="greybox">
- {% if news %}
+ {% if form.instance.id %}
<h2 class="title">Edit News</h2>
{% else %}
<h2 class="title">Add News</h2>
{% endif %}
<form method="post" action=".">
<table>
+ {{form}}
<tr>
- <td>Title:</td>
- <td>{{ form.title }}</td>
- </tr><tr>
- <td style="vertical-align:top">Content:</td>
- <td>{{ form.content }}</td>
- </tr><tr>
<td colspan="2" align="right">
<input type="submit" value=" Save " />
</td>
diff --git a/templates/news/delete.html b/templates/news/delete.html
index 7ac5e250..0f48f8bb 100644
--- a/templates/news/delete.html
+++ b/templates/news/delete.html
@@ -4,13 +4,12 @@
<div class="greybox">
<h2>Confirm Delete</h2>
<hr />
+ You are about to delete the news item '{{news}}'.
+ <br /><br />
+ Are you sure?
+ <br /><br />
<form method="post" action=".">
- <table>
- <tr>
- <td>Are You Sure?</td>
- <td>&nbsp; &nbsp; <input name="delete" type="submit" value=" Yes " /></td>
- </tr>
- </table>
+ <td>&nbsp; &nbsp; <input name="delete" type="submit" value=" Yes " />
</form>
</div>
{% endblock %}
diff --git a/templates/news/list.html b/templates/news/list.html
index 86277666..0edaae1d 100644
--- a/templates/news/list.html
+++ b/templates/news/list.html
@@ -9,7 +9,7 @@
{% endif %}
<h2 class="title">News Updates</h2>
<table class="results" width="100%">
- {% for item in news %}
+ {% for item in news_list %}
<tr class="{% cycle pkgr2,pkgr1 %}">
<td>{{ item.postdate }}</td>
<td><a href="{{ item.get_absolute_url }}">{{ item.title }}</a></td>