119 lines
3.8 KiB
Python
119 lines
3.8 KiB
Python
import base64
|
|
from io import BytesIO
|
|
from PIL import Image
|
|
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, EditProfileForm
|
|
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):
|
|
if request.method == 'POST':
|
|
form = EditProfileForm(request.POST)
|
|
if form.is_valid():
|
|
if request.FILES:
|
|
avatar_size = 200, 200
|
|
image = Image.open(request.FILES['avatar'])
|
|
image.thumbnail(avatar_size, Image.Resampling.LANCZOS)
|
|
|
|
buffered = BytesIO()
|
|
image.save(buffered, format="png")
|
|
img_str = base64.b64encode(buffered.getvalue())
|
|
img_base64 = bytes("data:image/png;base64,", encoding='utf-8') + img_str
|
|
request.user.avatar = img_base64.decode('utf-8')
|
|
|
|
request.user.description = request.POST['description']
|
|
request.user.url = request.POST['url']
|
|
request.user.save()
|
|
|
|
form = EditProfileForm()
|
|
|
|
context = {
|
|
'user': request.user,
|
|
'form': form,
|
|
}
|
|
return render(request, 'profile.html', context)
|