56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
from datetime import datetime
|
|
|
|
from django.shortcuts import redirect, render
|
|
from django.utils import timezone
|
|
|
|
from .models import Message
|
|
|
|
|
|
def message_view(request):
|
|
"""View for displaying and creating forum messages."""
|
|
|
|
if request.method == "POST":
|
|
titel = request.POST.get("titel", "")
|
|
text = request.POST.get("text", "")
|
|
|
|
if titel and text:
|
|
Message.objects.create(
|
|
titel=titel,
|
|
text=text,
|
|
author=request.user,
|
|
creation_time=timezone.now(),
|
|
)
|
|
|
|
return redirect(message_view)
|
|
|
|
context = {}
|
|
|
|
if request.method == "GET":
|
|
if "month" in request.GET and "year" in request.GET:
|
|
try:
|
|
year = int(request.GET["year"])
|
|
month = int(request.GET["month"])
|
|
|
|
from_date = datetime(year, month, 1)
|
|
# Handle December -> January transition
|
|
if month == 12:
|
|
until_date = datetime(year + 1, 1, 1)
|
|
else:
|
|
until_date = datetime(year, month + 1, 1)
|
|
|
|
context["archiveMode"] = True
|
|
context["year"] = year
|
|
context["month"] = month
|
|
context["messages"] = Message.objects.filter(
|
|
creation_time__lt=until_date, creation_time__gte=from_date
|
|
).order_by("-creation_time")
|
|
except (ValueError, TypeError):
|
|
# Invalid date parameters, fall back to default view
|
|
context["messages"] = Message.objects.order_by("-creation_time")[:20]
|
|
context["archiveMode"] = False
|
|
else:
|
|
context["messages"] = Message.objects.order_by("-creation_time")[:20]
|
|
context["archiveMode"] = False
|
|
|
|
return render(request, "simpleforum/simpleforum.html", context)
|