Tweaking forum app from comments.

This commit is contained in:
James Socol 2010-04-28 16:10:12 -07:00
Родитель f65c9fd527
Коммит d7d75b41b5
6 изменённых файлов: 13 добавлений и 12 удалений

Просмотреть файл

@ -22,6 +22,7 @@ class Thread(ModelBase):
creator = models.ForeignKey(User)
last_post = models.ForeignKey('Post', related_name='last_post_in',
null=True)
replies = models.IntegerField(default=0)
is_locked = models.BooleanField(default=False)
class Meta:
@ -30,10 +31,6 @@ class Thread(ModelBase):
def __unicode__(self):
return self.title
@property
def replies(self):
return len(self.post_set.all()) - 1
class Post(ModelBase):
id = models.AutoField(primary_key=True)

Просмотреть файл

@ -1,6 +1,6 @@
{# vim: set ts=2 et sts=2 sw=2: #}
{% extends "common/base.html" %}
{% set title = _('Start a New Topic') %}
{% set title = _('Start a New Thread') %}
{% block content %}
<h2>{{ title }}</h2>

Просмотреть файл

@ -17,7 +17,7 @@
<p>Oh, no! Looks like there are no posts!</p>
{% endif %}
<form action="{{ url('forums.reply') }}" method="post">
<form action="{{ url('forums.reply', forum_slug=forum.slug, thread_id=thread.id) }}" method="post">
{{ csrf() }}
{% for field in form %}
<div class="fieldWrapper">

Просмотреть файл

@ -9,7 +9,7 @@
<ul>
{% for thread in threads %}
<li><a href="{{ url('forums.posts', forum_slug=forum.slug, thread_id=thread.id) }}">{{ thread.title }}</a><br/>
Last post by {{ thread.last_post.author.username }}</li>
Replies: {{ thread.replies }} | Last post by {{ thread.last_post.author.username }}</li>
{% endfor %}
</ul>
{% else %}

Просмотреть файл

@ -2,10 +2,10 @@ from django.conf.urls.defaults import *
urlpatterns = patterns('forums.views',
url(r'^$', 'forums', name='forums.forums'),
url(r'^/reply$', 'reply', name='forums.reply'),
url(r'^/(?P<forum_slug>[\w\-]+)/(?P<thread_id>\d+)$',
'posts', name='forums.posts'),
url(r'^/(?P<forum_slug>[\w\-]+)$', 'threads', name='forums.threads'),
url(r'^/(?P<forum_slug>[\w\-]+)/new$', 'new_thread',
name='forums.new_thread'),
url(r'^/(?P<forum_slug>[\w\-]+)$', 'threads', name='forums.threads'),
url(r'^/(?P<forum_slug>[\w\-]+)/(?P<thread_id>\d+)$',
'posts', name='forums.posts'),
url(r'^/(?P<forum_slug>[\w\-]+)/(?P<thread_id>\d+)/reply$', 'reply', name='forums.reply'),
)

Просмотреть файл

@ -58,13 +58,17 @@ def posts(request, forum_slug, thread_id):
'posts': posts, 'form': form})
def reply(request):
def reply(request, forum_slug, thread_id):
form = ReplyForm(request.POST)
if form.is_valid():
post = form.save()
thread = Thread.objects.get(pk=request.POST.get('thread'))
# TODO: This should not need to be in the view.
thread.replies += 1
thread.save()
return HttpResponseRedirect(
reverse('forums.posts',
kwargs={'forum_slug': thread.forum.slug,