Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def new_post(request, thread, user, result=None):
    """
    Handle the new post form data when it gets posted
    """

    if not result:
        result = Result()
        
    postform = PostForm(request.POST)
    result.postform = postform
    
    if not result.captcha_success:
        captcha_error = get_captcha_error(request)
        if captcha_error:
            result.captcha_error = captcha_error
            return result
    
    if postform.is_valid():
        data = postform.cleaned_data
        post = Post(body=data['body'], thread=thread)
        
        #######
        
        if data['spam_prevent']:
            # if the spam prevent field is not blank, assume the post was made
            # by a spambot
            return HttpResponse('plz go away spambot thx')
        
        #######
        
        if not user.is_authenticated() or data['as_anon']:
            # anonymously posted messages are annotated with the poster's
            # IP address so they can edit them later
            post.ip = request.META['REMOTE_ADDR']
            post.as_anon = True
        else:
            #post.ip = "0.0.0.0"
            post.user = user
            post.as_anon = False
        
        ########
        
        if data['as_admin']:
            # this post was made by an admin and the post will be displayed
            # ina  way that lets everyone know that this person is an admin
            post.as_admin = True
        
        ########
        
        post.save()
        result.post = post
        

    return result