def add_post(request, t_slug, t_id, p_id = False):  # topic slug, topic id, post id
    """
    Creates a new post and attaches it to a topic
    """
    topic     = get_object_or_404(Topic, id=t_id)
    topic_url = '{0}page{1}/'.format(topic.get_short_url(), topic.page_count)
    user      = request.user
    current_time = time.time()
    form_title = 'Add a post'

    if topic.is_locked:  # If we mistakenly allowed reply on locked topic, bail with error msg.
        messages.error(request, 'Sorry, but this topic is closed')
        return HttpResponseRedirect(topic_url)

    q = None
    if p_id:  # if there's a post id, it's a quote
        q = Post.objects.get(id=p_id)
        form_title = "Respond to post"

    form = PostForm(request.POST or None, request.FILES or None)

    if form.is_valid():
        # we're going to save this inital data now,
        # rather than on the model save()
        # because we only want to bother with this stuff one time
        # and it will never update or change.

        instance = form.save(commit=False)
        instance.topic  = topic
        instance.user = user
        instance.author_name = user.display_name
        instance.avatar = user.avatar
        instance.post_date_int = current_time
        instance.quote = q
        instance.save()

        update_post_relations(user, topic)

        return HttpResponseRedirect('%s?new_post=%s#post-%s' % (topic_url, t_id, instance.id))
    return render(request, 'fretboard/add_edit.html', {
        'form': form,
        'form_title': form_title,
        'quote': q,
        'FORUM_BASE_NAME': FORUM_BASE_NAME
    })
Creates a new post and attaches it to a topic