oacpl/forum/views.py

57 lines
2.3 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
2018-01-11 00:58:07 -05:00
from .forms import CommentForm, CreatePostForm, EditPostForm
2017-10-24 00:57:44 -04:00
from .models import Thread, Post, Comment
def view(request, thread=None):
2018-01-11 00:58:07 -05:00
if request.method == 'POST':
form = CreatePostForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.creator = request.user
instance.save()
return redirect('post', post=instance.id)
else:
form = CreatePostForm()
my_posts = None
2017-10-24 00:57:44 -04:00
if request.user.is_authenticated():
2018-01-11 00:58:07 -05:00
my_posts = Post.objects.filter(creator=request.user, resolved=False)
2017-10-24 00:57:44 -04:00
if not thread:
threads = Thread.objects.all()
2018-01-11 00:58:07 -05:00
thread_name = None
posts = Post.objects.filter(resolved=False).order_by('-created')[:10]
2017-10-24 00:57:44 -04:00
else:
threads = None
2018-01-11 00:58:07 -05:00
thread_name = Thread.objects.get(id=thread).topic
posts = Post.objects.filter(topic=thread).order_by('-created')
return render(request, 'view.html', {'threads': threads, 'posts': posts, 'myPosts': my_posts, 'thread': thread_name, 'form': form})
2017-10-24 00:57:44 -04:00
2018-01-11 00:58:07 -05:00
def viewPost(request, post):
2018-01-02 17:11:49 -05:00
this_post = Post.objects.get(id=post)
2018-01-11 00:58:07 -05:00
if request.method == 'POST':
if request.POST.get('request') == 'comment':
form = CommentForm(request.POST)
Comment.objects.create(post_id=post,
reply=form.data.get('reply'),
creator=request.user)
elif request.POST.get('request') == 'edit':
form = EditPostForm(request.POST, instance=this_post)
form.save()
elif request.POST.get('request') == 'resolve':
if this_post.creator == request.user or request.user.has_perm('forum.change_post'):
this_post.resolved = True
this_post.save()
elif request.POST.get('request') == 'delete':
if this_post.creator == request.user or request.user.has_perm('forum.delete_post'):
this_post.delete()
return redirect('forum')
form = CommentForm()
edit_form = EditPostForm(instance=this_post)
comments = Comment.objects.filter(post=this_post)
return render(request, 'post.html', {'post': this_post, 'comments': comments, 'form': form, 'editForm': edit_form})