port to new django, AI automated
This commit is contained in:
@@ -1,34 +1,55 @@
|
||||
from datetime import datetime
|
||||
from django.shortcuts import render, redirect
|
||||
from simpleforum.models import Message
|
||||
|
||||
from django.shortcuts import redirect, render
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import Message
|
||||
|
||||
|
||||
def message_view(request):
|
||||
if request.method == 'POST':
|
||||
if 'titel' in request.POST and 'text' in request.POST:
|
||||
titel = request.POST.get('titel')
|
||||
text = request.POST.get('text')
|
||||
"""View for displaying and creating forum messages."""
|
||||
|
||||
if len(titel) > 0 and len(text) > 0:
|
||||
Message.objects.create(titel=titel, text=text, author=request.user, creation_time=datetime.now())
|
||||
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 = dict()
|
||||
context = {}
|
||||
|
||||
if request.method == 'GET':
|
||||
if 'month' in request.GET and 'year' in request.GET:
|
||||
year = int(request.GET['year'])
|
||||
month = int(request.GET['month'])
|
||||
until_date = datetime(year, month + 1, 1)
|
||||
from_date = datetime(year, month, 1)
|
||||
context['archiveMode'] = True
|
||||
context['year'] = year
|
||||
context['month'] = month
|
||||
context['messages'] = Message.objects.filter(creation_time__lt=until_date).filter(
|
||||
creation_time__gte=from_date).order_by('-creation_time')
|
||||
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
|
||||
context["messages"] = Message.objects.order_by("-creation_time")[:20]
|
||||
context["archiveMode"] = False
|
||||
|
||||
return render(request, 'simpleforum/simpleforum.html', context)
|
||||
return render(request, "simpleforum/simpleforum.html", context)
|
||||
|
||||
Reference in New Issue
Block a user