Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    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())
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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()
    })
Ejemplo n.º 6
0
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})
Ejemplo n.º 7
0
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)