# HG changeset patch # User Brian Neal # Date 1253937350 0 # Node ID 80ab249d1adc1f3cd755a3ccc5fcc40a2e19b09c # Parent e94398f5e027a39db8125aace8975f319a014d0a Forums: quoting existing posts. diff -r e94398f5e027 -r 80ab249d1adc gpp/forums/forms.py --- a/gpp/forums/forms.py Tue Sep 22 03:36:39 2009 +0000 +++ b/gpp/forums/forms.py Sat Sep 26 03:55:50 2009 +0000 @@ -8,15 +8,6 @@ from forums.models import Post -def bump_post_count(user): - """ - Increments the forum_post_count for the given user. - """ - profile = user.get_profile() - profile.forum_post_count += 1 - profile.save() - - class NewPostForm(forms.Form): """Form for creating a new post.""" body = forms.CharField(label='', widget=forms.Textarea) @@ -45,7 +36,6 @@ post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'], user_ip=ip) post.save() - bump_post_count(user) return post @@ -96,14 +86,12 @@ body=self.cleaned_data['body'], user_ip=ip) post.save() - - bump_post_count(self.user) return topic class PostForm(forms.ModelForm): """ - Form for editing an existing post. + Form for editing an existing post or a new, non-quick post. """ body = forms.CharField(label='', widget=forms.Textarea) diff -r e94398f5e027 -r 80ab249d1adc gpp/forums/urls.py --- a/gpp/forums/urls.py Tue Sep 22 03:36:39 2009 +0000 +++ b/gpp/forums/urls.py Sat Sep 26 03:55:50 2009 +0000 @@ -13,6 +13,7 @@ url(r'^forum/(?P[\w\d-]+)/$', 'forum_index', name='forums-forum_index'), url(r'^forum/(?P[\w\d-]+)/new-topic/$', 'new_topic', name='forums-new_topic'), url(r'^post/(\d+)/$', 'goto_post', name='forums-goto_post'), + url(r'^post/new/(?P\d+)/$', 'new_post', name='forums-new_post'), url(r'^quick-reply/$', 'quick_reply_ajax', name='forums-quick_reply'), ) diff -r e94398f5e027 -r 80ab249d1adc gpp/forums/views.py --- a/gpp/forums/views.py Tue Sep 22 03:36:39 2009 +0000 +++ b/gpp/forums/views.py Sat Sep 26 03:55:50 2009 +0000 @@ -14,6 +14,7 @@ from django.template.loader import render_to_string from django.template import RequestContext from django.views.decorators.http import require_POST +from django.utils.text import wrap from core.paginator import DiggPaginator from core.functions import email_admins @@ -145,6 +146,7 @@ form = NewTopicForm(request.user, forum, request.POST) if form.is_valid(): topic = form.save(request.META.get("REMOTE_ADDR")) + _bump_post_count(request.user) return HttpResponseRedirect(reverse('forums-new_topic_thanks', kwargs={'tid': topic.pk})) else: @@ -179,20 +181,21 @@ to the document. """ if not request.user.is_authenticated(): - return HttpResponseForbidden() + return HttpResponseForbidden('Please login or register to post.') form = NewPostForm(request.POST) if form.is_valid(): - if form.topic.locked or not form.topic.forum.category.can_access(request.user): - return HttpResponseForbidden() + if not _can_post_in_topic(form.topic, request.user): + return HttpResponseForbidden("You don't have permission to post in this topic.") - post = form.save(request.user, request.META.get("REMOTE_ADDR")) + post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) + _bump_post_count(request.user) return render_to_response('forums/display_post.html', { 'post': post, }, context_instance=RequestContext(request)) - return HttpResponseBadRequest(); + return HttpResponseBadRequest("Invalid post."); def goto_post(request, post_id): @@ -244,9 +247,11 @@ The superuser, forum moderators, and original author can edit posts. """ post = get_object_or_404(Post.objects.select_related(), pk=id) - can_edit = request.user == post.user or \ - request.user.is_superuser or \ - request.user in post.topic.forum.moderators.all() + + can_moderate = request.user.is_superuser or \ + request.user in post.topic.forum.moderators.all() + + can_edit = can_moderate or request.user == post.user if not can_edit: return HttpResponseForbidden("You don't have permission to edit that post.") @@ -264,7 +269,7 @@ 'topic': post.topic, 'post': post, 'form': form, - 'can_moderate': True, + 'can_moderate': can_moderate, }, context_instance=RequestContext(request)) @@ -343,3 +348,74 @@ # It should be safe to just delete the topic now. This will # automatically delete all posts in the topic. topic.delete() + + +@login_required +def new_post(request, topic_id): + """ + This function is the view for creating a normal, non-quick reply + to a topic. + """ + topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) + can_post = _can_post_in_topic(topic, request.user) + + if can_post: + if request.method == 'POST': + form = PostForm(request.POST) + if form.is_valid(): + post = form.save(commit=False) + post.topic = topic + post.user = request.user + post.user_ip = request.META.get("REMOTE_ADDR", "") + post.save() + _bump_post_count(request.user) + return HttpResponseRedirect(post.get_absolute_url()) + else: + quote_id = request.GET.get('quote') + if quote_id: + quote_post = get_object_or_404(Post.objects.select_related(), + pk=quote_id) + form = PostForm(initial={'body': _quote_message(quote_post.user.username, + quote_post.body)}) + else: + form = PostForm() + else: + form = None + + return render_to_response('forums/new_post.html', { + 'forum': topic.forum, + 'topic': topic, + 'form': form, + 'can_post': can_post, + }, + context_instance=RequestContext(request)) + + +def _can_post_in_topic(topic, user): + """ + This function returns true if the given user can post in the given topic + and false otherwise. + """ + return (not topic.locked and topic.forum.category.can_access(user)) or \ + (user.is_superuser or user in topic.forum.moderators.all()) + + +def _bump_post_count(user): + """ + Increments the forum_post_count for the given user. + """ + profile = user.get_profile() + profile.forum_post_count += 1 + profile.save() + + +def _quote_message(who, message): + """ + Builds a message reply by quoting the existing message in a + typical email-like fashion. The quoting is compatible with Markdown. + """ + header = '*%s wrote:*\n\n' % (who, ) + lines = wrap(message, 55).split('\n') + for i, line in enumerate(lines): + lines[i] = '> ' + line + return header + '\n'.join(lines) diff -r e94398f5e027 -r 80ab249d1adc gpp/templates/forums/display_post.html --- a/gpp/templates/forums/display_post.html Tue Sep 22 03:36:39 2009 +0000 +++ b/gpp/templates/forums/display_post.html Sat Sep 26 03:55:50 2009 +0000 @@ -22,7 +22,9 @@ {% endifnotequal %}