Fixing comments not working

master
Laurent 6 years ago
parent 045657059f
commit 76a8452e2d
  1. 4
      news/urls.py
  2. 21
      news/views.py

@ -11,10 +11,10 @@ urlpatterns = [
path('about', TemplateView.as_view(template_name='news/static/about.html'), name='about'),
# Posts
path('', views.index, name='index'),
path('<int:post_id>/comment', views.comment, name='comment'),
path('<int:post_id>/comment/<int:comment_id>', views.comment_with_parent, name='comment_with_parent'),
path("<int:post_id>/<slug:post_slug>", views.post, name="post"),
# path('<int:post_id>', views.post, name='post'),
path('<int:post_id>/comment/<int:comment_id>', views.comment_with_parent, name='comment_with_parent'),
path('<int:post_id>/comment', views.comment, name='comment'),
path('comment/<int:comment_id>/delete', views.delete_comment, name='delete_comment'),
path('upvote/<int:comment_id>', views.upvote, name='upvote'),
path('submission', views.submission, name='submission'),

@ -14,10 +14,12 @@ from django.utils.html import strip_tags
from django.utils.text import slugify
from .models import Post, Comment, PostState, Tag
from .forms import PostForm, CustomUserCreationForm, SigninForm
from datetime import datetime
from django.utils import timezone
import logging
import uuid
logger = logging.getLogger(__name__)
#Users
def register(request):
if request.method == 'POST':
@ -79,7 +81,7 @@ def password_change(request):
def index(request):
filter1 = Q(state=1)
filter2 = Q(date__lte=datetime.today())
filter2 = Q(date__lte=timezone.now())
latest_post_list = Post.objects.filter(filter1 & filter2).order_by('-date')
paginator = Paginator(latest_post_list, 25)
@ -97,11 +99,13 @@ def post(request, post_id, post_slug):
@login_required
def submission(request):
logger.debug('>>> Create post')
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = Post.objects.create(author=request.user,date=datetime.today())
post = Post.objects.create(author=request.user,date=timezone.now())
post.title = form.cleaned_data['title']
post.body = form.cleaned_data['body']
post.url = form.cleaned_data['url']
@ -151,11 +155,17 @@ def handle_uploaded_file(filename, f):
def submitted(request):
return render(request, 'news/submitted.html', {})
@login_required
def comment(request, post_id):
logger.debug('$$$ comment on $post_id, parent comment = $comment_id')
return comment_with_parent(request, post_id, None)
@login_required
def comment_with_parent(request, post_id, comment_id):
comment = Comment(author=request.user,date=datetime.today())
logger.debug('>>> comment_with_parent on $post_id, parent comment = $comment_id!')
#comment = Comment.objects.create(author=request.user,date=timezone.now())
comment = Comment(author=request.user,date=timezone.now())
comment.post = get_object_or_404(Post, pk=post_id)
if comment_id is not None:
comment.parent_comment = get_object_or_404(Comment, pk=comment_id)
@ -163,7 +173,8 @@ def comment_with_parent(request, post_id, comment_id):
comment.save()
comment.voters.add(request.user)
comment.save()
return HttpResponseRedirect(reverse('news:post', args=(post_id,)))
return render(request, 'news/post.html', {'post': comment.post, 'comments': comment.post.flat_comments(request.user) })
def delete_comment(request, comment_id):
comment = get_object_or_404(Comment, pk=comment_id)

Loading…
Cancel
Save