def newthread(request, forum): """ Rudimentary post function - this should probably use newforms, although not sure how that goes when we're updating two models. Only allows a user to post if they're logged in. """ if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user): return HttpResponseForbidden() if request.method == 'POST': form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) t.save() Post = comments.get_model() ct = ContentType.objects.get_for_model(Thread) p = Post( content_type=ct, object_pk=t.pk, user=request.user, comment=form.cleaned_data['body'], submit_date=datetime.now(), site=Site.objects.get_current(), ) p.save() t.latest_post = p t.comment = p t.save() """ undecided if form.cleaned_data.get('subscribe', False): s = Subscription( author=request.user, thread=t ) s.save() """ return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def previewthread(request, forum): """ Renders a preview of the new post and gives the user the option to modify it before posting. Only allows a user to post if they're logged in. """ if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) f = get_object_or_404(Forum, slug=forum, site=settings.SITE_ID) if not Forum.objects.has_access(f, request.user): return HttpResponseForbidden() if request.method == "POST": if not can_post(f, request.user): return HttpResponseForbidden cache = get_forum_cache() key = make_cache_forum_key(request.user, forum, settings.SITE_ID) if cache and forum in FORUM_FLOOD_CONTROL: if cache.get(key): post_title, post_url, expiry = cache.get(key) expiry = timeuntil(datetime.fromtimestamp(expiry)) messages.error(request, "You can't post a thread in the forum %s for %s." % (f.title, expiry)) return HttpResponseRedirect(post_url) form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) Post = comments.get_model() ct = ContentType.objects.get_for_model(Thread) # If previewing, render preview and form. if "preview" in request.POST: return render_to_response('forum/previewthread.html', RequestContext(request, { 'form': form, 'forum': f, 'thread': t, 'comment': form.cleaned_data['body'], 'user': request.user, })) # No preview means we're ready to save the post. else: t.save() p = Post( content_type=ct, object_pk=t.pk, user=request.user, comment=form.cleaned_data['body'], submit_date=datetime.now(), site=Site.objects.get_current(), ) p.save() t.latest_post = p t.comment = p t.save() Thread.nonrel_objects.push_to_list('%s-latest-comments' % t.forum.slug, t, trim=30) thread_created.send(sender=Thread, instance=t, author=request.user) if cache and forum in FORUM_FLOOD_CONTROL: cache.set(key, (t.title, t.get_absolute_url(), get_forum_expire_datetime(forum)), FORUM_FLOOD_CONTROL.get(forum, FORUM_POST_EXPIRE_IN)) return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))
def previewthread(request, forum): """ Renders a preview of the new post and gives the user the option to modify it before posting. If called without a POST, redirects to newthread. Only allows a user to post if they're logged in. """ if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path)) f = get_object_or_404(Forum, slug=forum) if not Forum.objects.has_access(f, request.user): return HttpResponseForbidden() if request.method == "POST": form = CreateThreadForm(request.POST) if form.is_valid(): t = Thread( forum=f, title=form.cleaned_data['title'], ) Post = comments.get_model() ct = ContentType.objects.get_for_model(Thread) # If previewing, render preview and form. if "preview" in request.POST: return render_to_response('forum/previewthread.html', RequestContext(request, { 'form': form, 'forum': f, 'thread': t, 'comment': form.cleaned_data['body'], 'user': request.user, })) # No preview means we're ready to save the post. else: t.save() p = Post( content_type=ct, object_pk=t.pk, user=request.user, comment=form.cleaned_data['body'], submit_date=datetime.now(), site=Site.objects.get_current(), ) p.save() t.latest_post = p t.comment = p t.save() thread_created.send(sender=Thread, instance=t, author=request.user) return HttpResponseRedirect(t.get_absolute_url()) else: form = CreateThreadForm() return render_to_response('forum/newthread.html', RequestContext(request, { 'form': form, 'forum': f, }))