Beispiel #1
0
def edit_comment(request, category_name, thread_id, comment_id):
    if not request.user.is_authenticated():
        #return HttpResponseRedirect('%slogin/?next=%s' % (site_root, request.path,))
        return HttpResponseRedirect(reverse('forum.views.login_page',))
    else:
    
        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                comment = Comment.objects.filter(id=comment_id)[0]
                
                update_edit_date = datetime.datetime.now()
                
                comment.body=cd['body'] 
                comment.edit_date=update_edit_date 
                comment.author=comment.author
                
                if(request.user.is_staff or str(request.user.username) == str(comment.author)):
                    comment.save()
                    request.user.message_set.create(message="Your edit was successful.")
                else:
                    request.user.message_set.create(message="I'm sorry, you don't have permision to edit that.")
                return HttpResponseRedirect(reverse('forum.views.show_thread',
                        kwargs=({'category_name': category_name, 'thread_id': thread_id,}),))
        
        else:
            
            comment = Comment.objects.filter(id=comment_id)[0]
            thread = Thread.objects.filter(id=thread_id)[0]
            
            default_data = {'body': comment.body,}
            
            form = CommentForm(default_data)
            
            return render_to_response('edit_comment.html', {'form': form, 'comment': comment, 'thread': thread, 'site_root': reverse('forum.views.main_index'),},
                            context_instance=RequestContext(request))
Beispiel #2
0
def show_thread(request, category_name, thread_id, page = 1):
    if not request.user.is_authenticated():
        #return HttpResponseRedirect('%slogin/?next=%s' % (site_root, request.path,))
        return HttpResponseRedirect(reverse('forum.views.login_page',))
    else:
    
        
    
        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                thread = get_object_or_404(Thread, id=thread_id)
                post = Comment(body=cd['body'],
                                author=request.user,
                                parent_thread_id=thread_id,
                                parent_thread_fk=thread,)
                post.save()
                
                # update the last user post, and time of last post for all the parent
                # categories and threads
                update_time_thread = Thread.objects.filter(id=thread_id)[0]
                update_time_thread.last_post = datetime.datetime.now()
                update_time_thread.save()
                
                update_user_thread = Thread.objects.filter(id=thread_id)[0]
                update_user_thread.last_post_user = request.user.username
                update_user_thread.save()
                
                update_time_cat = Category.objects.filter(slug=category_name)[0]
                update_time_cat.last_post = datetime.datetime.now()
                update_time_cat.save()
                
                update_user_cat = Category.objects.filter(slug=category_name)[0]
                update_user_cat.last_post_user = request.user.username
                update_user_cat.save()
                
                #just_posted = Thread.objects.filter(body=cd['body'])[0]
                
                request.user.message_set.create(message="Your reply was successful.")
                #return HttpResponseRedirect('%s%s/' % (just_posted.parent_category.get_absolute_url, just_posted.id))
                
                #return HttpResponseRedirect(reverse('forum.views.show_thread', 
                #       kwargs=({'category_name': category_name, 'thread_id': thread_id,}),))
                return redirect(post)
        else:
            form = CommentForm()
            
        # i just want to see a specific thread and it's replies
        thread_id = int(thread_id)
        thread = get_object_or_404(Thread, id=thread_id)
        post = Thread.objects.get(id=thread_id)
        category = Category.objects.filter(slug=category_name)[0]
        comments = Comment.objects.filter(parent_thread_fk=thread).order_by("pub_date")
        
        # pagination stuff
        paginator = Paginator(comments, 25)
        
        # make sure request is an int
        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1
            
        # make sure it is not out of range
        try:
            p_comments = paginator.page(page)
        except (EmptyPage, InvalidPage):
            p_comments = paginator.page(paginator.num_pages)
        
        return render_to_response('view_thread.html', {'post': post,
                            'category': category,
                            'comments': p_comments,
                            'category_name': category_name, 
                            'thread_id': thread_id,
                            'form' : form,
                            'site_root': reverse('forum.views.main_index',),},
                            context_instance=RequestContext(request))