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 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 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 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): """ 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 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 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 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)