def __init__(self, *args, **kwargs): self.parent_msg = kwargs.pop('parent_msg', None) assert self.parent_msg is not None self.sender = kwargs.pop('sender', None) assert self.sender is not None super(PrivateMessageReplyForm, self).__init__(*args, **kwargs) # Pre-fill subject and body self.fields['subject'].initial = _re_subject(self.parent_msg.subject) self.fields['body'].initial = render_quote(self.parent_msg.body, self.parent_msg.sender.username, self.parent_msg.get_absolute_url())
def forum_thread_post_reply(request, pk, template_name='forum/forum_thread_post_reply.html', template_name_closed_thread='forum/forum_thread_closed.html', forum_thread_reply_form=ForumThreadReplyForm, extra_context=None): """ Post reply view. :param request: The current request. :param pk: The quoted post PK. :param template_name: The template name to be used. :param template_name_closed_thread: The template name to be used if the thread is closed. :param forum_thread_reply_form: The form class to be used. :param extra_context: Any extra context for the template. :return: TemplateResponse or HttpResponseRedirect """ assert pk is not None # Get the post object by PK manager = ForumThreadPost.objects.published().select_related('parent_thread', 'parent_thread__parent_forum', 'author') post_obj = get_object_or_404(manager, pk=pk) parent_thread_obj = post_obj.parent_thread # Handle private forum if not parent_thread_obj.has_access(request.user): raise PermissionDenied() # Handle closed thread if parent_thread_obj.closed or parent_thread_obj.locked: # Render the "closed thread" template context = { 'thread': parent_thread_obj, 'forum': parent_thread_obj.parent_forum, 'title': _('Thread closed'), } if extra_context is not None: context.update(extra_context) return TemplateResponse(request, template_name_closed_thread, context) # Handle anti flood current_user = request.user is_flooding = False # Get default notify settings notify_of_reply_default = current_user.forum_profile.notify_of_reply_by_default # Handle form GET/POST if request.method == "POST": # Check for flood only on POST is_flooding = current_user.forum_profile.is_flooding() # Handle form and anti flood form = forum_thread_reply_form(request.POST, request.FILES) if form.is_valid() and not is_flooding: opts = { 'author': current_user, 'parent_thread': parent_thread_obj, 'request': request } new_post = form.save(**opts) return HttpResponseRedirect(new_post.get_absolute_url()) else: quote_content = post_obj.content quote_author = post_obj.author quote_author_url = quote_author.user_profile.get_absolute_url() form = forum_thread_reply_form(initial={'content': render_quote(quote_content, quote_author.username, quote_author_url), 'notify_of_reply': notify_of_reply_default}) # Render the template context = { 'related_post': post_obj, 'thread': parent_thread_obj, 'forum': parent_thread_obj.parent_forum, 'form': form, 'title': _('Reply to post'), 'is_flooding': is_flooding, 'flood_delay_sec': NB_SECONDS_BETWEEN_POSTS } if extra_context is not None: context.update(extra_context) return TemplateResponse(request, template_name, context)