changeset 83:5b4c812b448e

Forums: Added the ability to add a new topic. This is very much a work in progress.
author Brian Neal <bgneal@gmail.com>
date Sat, 29 Aug 2009 20:54:16 +0000
parents bc3978f023c2
children 9e5e52556d5b
files gpp/forums/forms.py gpp/forums/models.py gpp/forums/urls.py gpp/forums/views.py gpp/templates/forums/forum_index.html gpp/templates/forums/new_topic.html gpp/templates/forums/new_topic_thanks.html media/css/base.css
diffstat 8 files changed, 149 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- /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
--- 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', )
--- 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<tid>\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'),
     url(r'^topic/(?P<id>\d+)/$', 'topic_index', name='forums-topic_index'),
     url(r'^(?P<slug>[\w\d-]+)/$', 'forum_index', name='forums-forum_index'),
+    url(r'^(?P<slug>[\w\d-]+)/topic/$', 'new_topic', name='forums-new_topic'),
 )
+
--- 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))
--- 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 @@
 </h3>
 
 <div class="forum-block">
+<a href="{% url forums-new_topic slug=forum.slug %}">New Post</a>
 <table class="forum-index-table">
    <thead>
       <tr>
@@ -23,7 +24,7 @@
    {% for topic in topics %}
       <tr>
          <td><h4><a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a></h4></td>
-         <td class="forum-index_replies">{{ topic.post_count }}</td>
+         <td class="forum-index_replies">{{ topic.reply_count }}</td>
          <td class="forum-index_author">{{ topic.user.username }}</td>
          <td class="forum-index_views">{{ topic.view_count }}</td>
          <td class="forum-index_last_post">
--- /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 %}
+<h2>{{ forum.name }} - New Topic</h2>
+
+<h3>
+   <a href="{% url forums-index %}">SurfGuitar101 Forum Index</a> &raquo;
+   <a href="{% url forums-forum_index slug=forum.slug %}">{{ forum.name }}</a>
+</h3>
+
+<form action="." method="post">
+{{ form.as_p }}
+<input type="submit" name="post" value="Submit" />
+</form>
+
+{% endblock %}
--- /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 %}
+<h2>Forums: {{ forum.name }} - New Topic Created</h2>
+
+<h3>
+   <a href="{% url forums-index %}">SurfGuitar101 Forum Index</a> &raquo;
+   <a href="{% url forums-forum_index slug=forum.slug %}">{{ forum.name }}</a>
+</h3>
+
+<p>
+New topic created successfully. 
+Click <a href="{{ topic.get_absolute_url }}">here</a> to view your new topic.
+</p>
+
+{% endblock %}
--- 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;
 }