oacpl/forum/views.py

41 lines
1.2 KiB
Python
Raw Normal View History

2018-01-02 17:11:49 -05:00
from django.shortcuts import render, redirect
2017-08-26 11:40:35 -04:00
2017-10-24 00:57:44 -04:00
from .models import Thread, Post, Comment
def view(request, thread=None):
myPosts = None
if request.user.is_authenticated():
myPosts = Post.objects.filter(creator=request.user)
if not thread:
threads = Thread.objects.all()
posts = Post.objects.order_by('created')[:10]
else:
threads = None
posts = Post.objects.filter(topic=thread)
return render(request, 'view.html', {'threads': threads, 'posts': posts, 'myPosts': myPosts})
2018-01-02 17:11:49 -05:00
def post(request, post):
this_post = Post.objects.get(id=post)
comments = Comment.objects.filter(post=post)
return render(request, 'post.html', {'post': this_post, 'comments': comments})
2017-10-24 00:57:44 -04:00
def create(request):
pass
def comment(request):
2018-01-02 17:11:49 -05:00
success = False
post = Post.objects.get(id=request.POST.get('post'))
comments = Comment.objects.filter(post=post)
try:
success = Comment.objects.create(post=post,
reply=request.POST.get('comment'),
creator=request.user)
success.save()
finally:
return redirect('forum', post.id)