def new_thread(request): ''' Start a new discussion. ''' if request.user.is_authenticated() and request.POST: threadform= ThreadForm(request.POST.copy()) if threadform.is_valid(): # create the thread thread = Thread( subject = threadform.cleaned_data['subject'], category = Category.objects.get(pk= threadform.cleaned_data['category']), ) thread.save() # create the post post = Post( user = request.user, thread = thread, text = threadform.cleaned_data['post'], ) post.save() # redirect to new thread return HttpResponseRedirect(SNAP_PREFIX + '/threads/id/' + str(thread.id) + '/') else: threadform = ThreadForm() return render_to_response('snapboard/newthread.html', { 'form': threadform, }, context_instance=RequestContext(request, processors=[login_context,]))
def board(request, board_slug): try: board = Board.objects.get(slug__iexact=board_slug) except Board.DoesNotExist: raise Http404 if request.method == 'POST': thread_form = ThreadForm(request.POST) if thread_form.is_valid(): thread = thread_form.save(commit=False) thread.poster_ip = request.META['REMOTE_ADDR'] thread.board = board try: thread.save() except ValueError: return HttpResponse('500', status=500) else: return HttpResponseRedirect( reverse('thread_view', kwargs={ 'board_slug': board.slug, 'thread_id': thread.id})) latest_threads = Thread.objects.filter(board=board).order_by('-last_updated')[:10] tpl_vars = { 'navbar_links': Board.objects.values('slug', 'name'), 'board': board, 'latest_threads': latest_threads, 'form': ThreadForm(), } tpl_vars.update(csrf(request)) return render_to_response('board.tpl', tpl_vars)
def new_thread(request, subject_id): subject = get_object_or_404(Subject, pk=subject_id) if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) if thread_form.is_valid() and post_form.is_valid(): thread = thread_form.save(False) thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread post.save() messages.success(request, "You have create a new thread!") return redirect(reverse('thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject, } args.update(csrf(request)) return render(request, 'forum/thread_form.html', args)
def new_thread(request, forum, user): """ Given a POST dict, create a new thread, then pass whats left to the new_post function and then create the op. This function returns any errors, it returns None if there are no errors. """ threadform = ThreadForm(request.POST) result = Result() result.threadform = threadform captcha_error = get_captcha_error(request) if captcha_error: result.captcha_error = captcha_error result.postform = PostForm(request.POST) return result else: result.captcha_success = True if threadform.is_valid(): data = threadform.cleaned_data thread = Thread(title=data['title'], forum=forum) thread.save() result.thread = thread else: # error occured, return the form and the captcha error # (already set to result object) don't bother to try to add the post return result # all is valid, now make the op, skip captcha part # because we already checked it return new_post(request, thread, user, result=result)
def new_thread(request): if request.method == "POST": form = ThreadForm(request.POST) if form.is_valid(): data = form.cleaned_data body = data["body"] thread = Thread.objects.create(title=data["title"], category=data["category"]) post = Post.objects.create( author=request.user, thread=thread, body=body, pub_date=datetime.now(), update=datetime.now(), markdown=data["markdown"], ) post.save() thread.author = request.user thread.latest_post = post thread.save() # Mark thread a read for user. read = UserRead.objects.get_or_create(user=request.user, thread=thread)[0] read.read = True read.save() return HttpResponseRedirect(reverse("threads")) else: form = ThreadForm() return render_to_response("dsf/thread_new.html", {"form": form})
def add_thread(request, template="forum/add_thread.html"): "Create new thread and first post" def get_slug(text, numb=0): "Create unique slug" text = text[:110] if numb: text = text.rsplit("_", 1)[0] + "_%d" % numb s = slugify(text) if Thread.objects.filter(slug=s).count(): return get_slug(text, numb + 1) return s u = request.user tf = ThreadForm() pf = PostForm() if request.POST: t = Thread(author=u, latest_post_author=u, slug=get_slug(request.POST['title'])) tf = ThreadForm(request.POST, instance=t) if tf.is_valid(): tfins = tf.save() p = Post(thread=tfins, author=u) pf = PostForm(request.POST, instance=p) if pf.is_valid(): pfins = pf.save() return HttpResponseRedirect(tfins.get_absolute_url()) else: tfins.delete() return render_to_response(template, { "t_form": tf, "p_form": pf, }, context_instance=RequestContext(request))
def new_thread(request, subject_id): """ Allows logged in users to create a new thread and post at the same time. Also, gives the option of creating a poll when creating a new thread. """ subject = get_object_or_404(Subject, pk=subject_id) poll_subject_formset = formset_factory(PollSubjectForm, extra=3) # makes a set of 3 forms in this case. if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) poll_form = PollForm(request.POST) poll_subject_formset = poll_subject_formset(request.POST) # When calling the is_valid, the formset will validate all forms in # one go, so you can effectively treat them like they are one form. if thread_form.is_valid() and post_form.is_valid() and poll_form.is_valid() and poll_subject_formset.is_valid(): thread = thread_form.save(False) # get memory only version of the model thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread # newly created thread id post.save() if request.POST.get('is_a_poll', None): poll = poll_form.save(False) poll.thread = thread poll.save() # To pull out the values from each form, loop through each one. # It is the same when rendering. for subject_form in poll_subject_formset: subject = subject_form.save(False) subject.poll = poll subject.save() messages.success(request, "You have created a new thread!") return redirect(reverse('thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) poll_form = PollForm() poll_subject_formset = poll_subject_formset() args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject, 'poll_form': poll_form, 'poll_subject_formset': poll_subject_formset, # loop through the formset of 3 forms when rendering. } args.update(csrf(request)) return render(request, 'forum/thread_form.html', args)
def new_thread(request, subject_id): subject = get_object_or_404(Subject, pk=subject_id) poll_subject_formset = formset_factory(PollSubjectForm, extra=3) if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) poll_form = PollForm(request.POST) poll_subject_formset = poll_subject_formset(request.POST) if thread_form.is_valid() and post_form.is_valid(): thread = thread_form.save(False) thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread post.save() if request.POST.get('is_a_poll', None) and poll_form.is_valid() and poll_subject_formset.is_valid(): poll = poll_form.save(False) poll.thread = thread poll.save() for subject_form in poll_subject_formset: subject = subject_form.save(False) subject.poll = poll subject.save() messages.success(request, "You have create a new thread!") return redirect(reverse('thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) poll_form = PollForm() poll_subject_formset = poll_subject_formset() args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject, 'poll_form': poll_form, 'poll_subject_formset': poll_subject_formset, } args.update(csrf(request)) return render(request, 'forum/thread_form.html', args)
def test_subject_required_robert(self): tf = ThreadForm({'subject': 'yay'}) self.assertTrue(tf.is_valid()) tf = ThreadForm({'subject': ''}) self.assertFalse(tf.is_valid()) tf = ThreadForm({'subject': ' \t \t \n\n\n'}) self.assertFalse(tf.is_valid())
def create_thread_submit(request, forumid): forum = Forum.get_by_id(int(forumid)) thread = Thread(forum=forum, user=request._user, content="Default", title="Default") data = ThreadForm(request.POST, instance=thread) if data.is_valid(): entity = data.save(commit=False) entity.put() forum.increment_thread_count() forum.set_last_thread(entity) return HttpResponseRedirect('/forum/{0}'.format(forum.key().id())) return render_to_response('home/create_thread.html', {'thread_form': data})
def create_thread_submit(request, forumid): forum = Forum.get_by_id(int(forumid)) thread = Thread(forum=forum,user=request._user, content="Default", title="Default") data = ThreadForm(request.POST, instance=thread) if data.is_valid(): entity = data.save(commit=False) entity.put() forum.increment_thread_count() forum.set_last_thread(entity) return HttpResponseRedirect('/forum/{0}'.format(forum.key().id())) return render_to_response('home/create_thread.html', { 'thread_form' : data })
def create_thread(request, forumid): forum = Forum.get_by_id(int(forumid)) return render_to_response('home/create_thread.html', { 'forum': forum, 'thread_form': ThreadForm() })
def thread_category_detail(request, slug, pk=None): if pk is not None: edit_thread = Thread.objects.get(pk=pk) else: edit_thread = None try: thread_category = ThreadCategory.objects.get(slug=slug) thread_list = Thread.objects.filter(category=thread_category.pk) except ThreadCategory.DoesNotExist: raise Http404("Thread category does not exist!") data = { 'thread_category': thread_category, 'thread_list': thread_list, 'form': ThreadForm(), 'slug': slug, } if request.method == 'POST': if 'new-thread' in request.POST: form = ThreadForm(request.POST) if form.is_valid(): thread = form.save(commit=False) thread.category = thread_category thread.save() return redirect(thread_category.get_absolute_url()) else: data.update({'form': form}) return render(request, 'thread_category.html', data) elif 'edit-thread' in request.POST: edit_thread_form = ThreadForm(request.POST, instance=edit_thread) if edit_thread_form.is_valid(): edit_thread_form.save() return redirect(thread_category.get_absolute_url()) else: data.update({ 'pk': edit_thread.pk, 'edit_thread_form': edit_thread_form, }) return render(request, 'thread_category.html', data) if edit_thread is not None: edit_thread_form = ThreadForm(instance=edit_thread) data['pk'] = edit_thread.pk data['edit_thread_form'] = edit_thread_form data['edit_thread'] = edit_thread return render(request, 'thread_category.html', data)
def test_subject_required_robert(self): tf = ThreadForm({ 'subject' : 'yay' }) self.assertTrue(tf.is_valid()) tf = ThreadForm({ 'subject' : '' }) self.assertFalse(tf.is_valid()) tf = ThreadForm({ 'subject' : ' \t \t \n\n\n' }) self.assertFalse(tf.is_valid())
def new_thread(request, subject_id): subject = get_object_or_404(Subject, pk=subject_id) poll_subject_formset = formset_factory(PollSubjectForm, extra=3) if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) poll_form = PollForm(request.POST) poll_subject_formset = poll_subject_formset(request.POST) if thread_form.is_valid() and post_form.is_valid(): thread = thread_form.save(False) thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread post.save() if request.POST.get('is_a_poll', None) and poll_form.is_valid( ) and poll_subject_formset.is_valid(): poll = poll_form.save(False) poll.thread = thread poll.save() for subject_form in poll_subject_formset: subject = subject_form.save(False) subject.poll = poll subject.save() messages.success(request, "You have create a new thread!") return redirect(reverse('thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) poll_form = PollForm() poll_subject_formset = poll_subject_formset() args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject, 'poll_form': poll_form, 'poll_subject_formset': poll_subject_formset, } args.update(csrf(request)) return render(request, 'forum/thread_form.html', args)
def new_thread(request, subject_id): """ Allows logged in users to create a new thread and post at the same time. Also, gives the option of creating a poll when creating a new thread. """ subject = get_object_or_404(Subject, pk=subject_id) poll_subject_formset = formset_factory( PollSubjectForm, extra=3) # makes a set of 3 forms in this case. if request.method == "POST": thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) poll_form = PollForm(request.POST) poll_subject_formset = poll_subject_formset(request.POST) # When calling the is_valid, the formset will validate all forms in # one go, so you can effectively treat them like they are one form. if thread_form.is_valid() and post_form.is_valid( ) and poll_form.is_valid() and poll_subject_formset.is_valid(): thread = thread_form.save( False) # get memory only version of the model thread.subject = subject thread.user = request.user thread.save() post = post_form.save(False) post.user = request.user post.thread = thread # newly created thread id post.save() if request.POST.get('is_a_poll', None): poll = poll_form.save(False) poll.thread = thread poll.save() # To pull out the values from each form, loop through each one. # It is the same when rendering. for subject_form in poll_subject_formset: subject = subject_form.save(False) subject.poll = poll subject.save() messages.success(request, "You have created a new thread!") return redirect(reverse('thread', args={thread.pk})) else: thread_form = ThreadForm() post_form = PostForm(request.POST) poll_form = PollForm() poll_subject_formset = poll_subject_formset() args = { 'thread_form': thread_form, 'post_form': post_form, 'subject': subject, 'poll_form': poll_form, 'poll_subject_formset': poll_subject_formset, # loop through the formset of 3 forms when rendering. } args.update(csrf(request)) return render(request, 'forum/thread_form.html', args)