# HG changeset patch # User Brian Neal # Date 1251579256 0 # Node ID 5b4c812b448e81aa78990a9929fa5caa47576c62 # Parent bc3978f023c235022430b9c8b7580aef38f0dcce Forums: Added the ability to add a new topic. This is very much a work in progress. diff -r bc3978f023c2 -r 5b4c812b448e gpp/forums/forms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/forums/forms.py Sat Aug 29 20:54:16 2009 +0000 @@ -0,0 +1,47 @@ +""" +Forms for the forums application. +""" +from django import forms + +from forums.models import Topic +from forums.models import Post + + +class TopicForm(forms.ModelForm): + """Form for creating a new topic.""" + + class Meta: + model = Topic + fields = ('name', ) + + +class PostForm(forms.ModelForm): + """Form for creating a new post.""" + + class Meta: + model = Post + fields = ('body', ) + + +class NewTopicForm(forms.Form): + """Form for creating a new topic and 1st post to that topic.""" + name = forms.CharField(label='Subject', max_length=255) + body = forms.CharField(label='', widget=forms.Textarea) + + def save(self, forum, user, ip=None): + """ + Creates the new Topic and first Post from the form data and supplied + forum and user objects. + """ + topic = Topic(forum=forum, + name=self.cleaned_data['name'], + user=user) + topic.save() + + post = Post(topic=topic, + user=user, + body=self.cleaned_data['body'], + user_ip=ip) + post.save() + + return topic diff -r bc3978f023c2 -r 5b4c812b448e gpp/forums/models.py --- a/gpp/forums/models.py Sun Aug 23 04:04:29 2009 +0000 +++ b/gpp/forums/models.py Sat Aug 29 20:54:16 2009 +0000 @@ -99,6 +99,15 @@ self.last_post = None self.update_date = self.creation_date + def reply_count(self): + """ + Returns the number of replies to a topic. The first post + doesn't count as a reply. + """ + if self.post_count > 1: + return self.post_count - 1 + return 0 + class Post(models.Model): topic = models.ForeignKey(Topic, related_name='posts') @@ -107,7 +116,7 @@ update_date = models.DateTimeField(auto_now=True) body = models.TextField() html = models.TextField() - user_ip = models.IPAddressField(blank=True, default='') + user_ip = models.IPAddressField(blank=True, default='', null=True) class Meta: ordering = ('creation_date', ) diff -r bc3978f023c2 -r 5b4c812b448e gpp/forums/urls.py --- a/gpp/forums/urls.py Sun Aug 23 04:04:29 2009 +0000 +++ b/gpp/forums/urls.py Sat Aug 29 20:54:16 2009 +0000 @@ -5,6 +5,9 @@ urlpatterns = patterns('forums.views', url(r'^$', 'index', name='forums-index'), + url(r'^new-topic-success/(?P\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'), url(r'^topic/(?P\d+)/$', 'topic_index', name='forums-topic_index'), url(r'^(?P[\w\d-]+)/$', 'forum_index', name='forums-forum_index'), + url(r'^(?P[\w\d-]+)/topic/$', 'new_topic', name='forums-new_topic'), ) + diff -r bc3978f023c2 -r 5b4c812b448e gpp/forums/views.py --- a/gpp/forums/views.py Sun Aug 23 04:04:29 2009 +0000 +++ b/gpp/forums/views.py Sat Aug 29 20:54:16 2009 +0000 @@ -1,12 +1,17 @@ """ Views for the forums application. """ +from django.contrib.auth.decorators import login_required from django.http import Http404 +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse from django.shortcuts import get_object_or_404 from django.shortcuts import render_to_response from django.template import RequestContext from forums.models import Forum +from forums.models import Topic +from forums.forms import NewTopicForm def index(request): @@ -50,3 +55,38 @@ Displays all the posts in a topic. """ raise Http404 + + +@login_required +def new_topic(request, slug): + """ + This view handles the creation of new topics. + """ + forum = get_object_or_404(Forum, slug=slug) + if request.method == 'POST': + form = NewTopicForm(request.POST) + if form.is_valid(): + topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR")) + return HttpResponseRedirect(reverse('forums-new_topic_thanks', + kwargs={'tid': topic.pk})) + else: + form = NewTopicForm() + + return render_to_response('forums/new_topic.html', { + 'forum': forum, + 'form': form, + }, + context_instance=RequestContext(request)) + + +@login_required +def new_topic_thanks(request, tid): + """ + This view displays the success page for a newly created topic. + """ + topic = get_object_or_404(Topic, pk=tid) + return render_to_response('forums/new_topic_thanks.html', { + 'forum': topic.forum, + 'topic': topic, + }, + context_instance=RequestContext(request)) diff -r bc3978f023c2 -r 5b4c812b448e gpp/templates/forums/forum_index.html --- a/gpp/templates/forums/forum_index.html Sun Aug 23 04:04:29 2009 +0000 +++ b/gpp/templates/forums/forum_index.html Sat Aug 29 20:54:16 2009 +0000 @@ -9,6 +9,7 @@
+New Post @@ -23,7 +24,7 @@ {% for topic in topics %} - +

{{ topic.name }}

{{ topic.post_count }}{{ topic.reply_count }} {{ topic.user.username }} {{ topic.view_count }} diff -r bc3978f023c2 -r 5b4c812b448e gpp/templates/forums/new_topic.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/new_topic.html Sat Aug 29 20:54:16 2009 +0000 @@ -0,0 +1,16 @@ +{% extends 'base.html' %} +{% block title %}Forums: New Topic{% endblock %} +{% block content %} +

{{ forum.name }} - New Topic

+ +

+ SurfGuitar101 Forum Index » + {{ forum.name }} +

+ +
+{{ form.as_p }} + +
+ +{% endblock %} diff -r bc3978f023c2 -r 5b4c812b448e gpp/templates/forums/new_topic_thanks.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/new_topic_thanks.html Sat Aug 29 20:54:16 2009 +0000 @@ -0,0 +1,16 @@ +{% extends 'base.html' %} +{% block title %}Forums: New Topic Created{% endblock %} +{% block content %} +

Forums: {{ forum.name }} - New Topic Created

+ +

+ SurfGuitar101 Forum Index » + {{ forum.name }} +

+ +

+New topic created successfully. +Click here to view your new topic. +

+ +{% endblock %} diff -r bc3978f023c2 -r 5b4c812b448e media/css/base.css --- a/media/css/base.css Sun Aug 23 04:04:29 2009 +0000 +++ b/media/css/base.css Sat Aug 29 20:54:16 2009 +0000 @@ -159,30 +159,38 @@ table.forum-index-table thead th { background:teal; } -table.forum-index-table th.forum-title { +table.forum-index-table .forum-title { width:65%; } -table.forum-index-table th.forum-topics { +table.forum-index-table .forum-topics { width:10%; text-align:center; } -table.forum-index-table th.forum-posts { +table.forum-index-table .forum-posts { width:10%; text-align:center; } -table.forum-index-table th.forum-last_post { +table.forum-index-table .forum-last_post { width:15%; text-align:center; } -table.forum-index-table td.forum-topics { + +table.forum-index-table .forum-index_title { + width:55%; +} +table.forum-index-table .forum-index_replies { width:10%; text-align:center; } -table.forum-index-table td.forum-posts { +table.forum-index-table .forum-index_author { width:10%; text-align:center; } -table.forum-index-table td.forum-last_post { +table.forum-index-table .forum-index_views { + width:10%; + text-align:center; +} +table.forum-index-table .forum-index_last_post { width:15%; text-align:center; }