94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.contrib.auth import login, logout
|
|
from django.urls import reverse
|
|
from web.forms import SignupForm, ThreadPostForm
|
|
from web.models.forum_subcategory import ForumSubcategory
|
|
from web.models.forum_post import ForumPost
|
|
|
|
from markdownx.utils import markdownify
|
|
|
|
|
|
def index(request):
|
|
return render(request, 'index.html')
|
|
|
|
|
|
# FORUM ###################################################
|
|
def forum_threads(request):
|
|
parsed_forum_threads = {}
|
|
forum_subcategories = ForumSubcategory.objects.filter(active=True).order_by('-created_at').order_by('sticky').all()
|
|
|
|
for forum_subcategory in forum_subcategories:
|
|
if forum_subcategory.forum_category.title not in parsed_forum_threads:
|
|
parsed_forum_threads[forum_subcategory.forum_category.title] = []
|
|
|
|
data = {
|
|
'id': forum_subcategory.id,
|
|
'title': forum_subcategory.title,
|
|
'description': forum_subcategory.description,
|
|
'post_count': len(forum_subcategory.posts.all()),
|
|
'most_recent_poster': forum_subcategory.posts.first().created_by if forum_subcategory.posts.first() else 'admin',
|
|
'most_recent_post_date': forum_subcategory.posts.first().created_at if forum_subcategory.posts.first() else forum_subcategory.created_at,
|
|
}
|
|
|
|
parsed_forum_threads[forum_subcategory.forum_category.title].append(data)
|
|
|
|
context = {
|
|
'threads': parsed_forum_threads
|
|
}
|
|
return render(request, 'forum_threads.html', context)
|
|
|
|
|
|
def thread(request, thread_id):
|
|
thread = get_object_or_404(ForumSubcategory, pk=thread_id)
|
|
|
|
if request.method == 'POST':
|
|
form = ThreadPostForm(request.POST)
|
|
if form.is_valid():
|
|
post = ForumPost(
|
|
content=markdownify(form.data['content']),
|
|
forum_subcategory=thread,
|
|
created_by=request.user,
|
|
edited=False,
|
|
sticky=False,
|
|
)
|
|
post.save()
|
|
|
|
form = ThreadPostForm()
|
|
posts = ForumPost.objects.filter(forum_subcategory=thread).all()
|
|
|
|
context = {
|
|
'thread_name': thread.title,
|
|
'thread_category': thread.forum_category.title,
|
|
'posts': posts,
|
|
'form': form
|
|
}
|
|
return render(request, 'thread.html', context)
|
|
|
|
|
|
# ACCOUNT MANAGEMENT ######################################
|
|
def signup(request):
|
|
if request.method == 'POST':
|
|
form = SignupForm(request.POST)
|
|
if form.is_valid():
|
|
user = form.save()
|
|
login(request, user)
|
|
return redirect('index')
|
|
else:
|
|
form = SignupForm()
|
|
|
|
context = {
|
|
'form': form
|
|
}
|
|
return render(request, 'signup.html', context)
|
|
|
|
|
|
def custom_logout(request):
|
|
logout(request)
|
|
return redirect(reverse('login'))
|
|
|
|
|
|
def profile(request):
|
|
context = {
|
|
'user': request.user
|
|
}
|
|
return render(request, 'profile.html', context)
|