blechreiz-website/simpleforum/views.py

35 lines
1.3 KiB
Python

from datetime import datetime
from django.shortcuts import render, redirect
from simpleforum.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')
if len(titel) > 0 and len(text) > 0:
Message.objects.create(titel=titel, text=text, author=request.user, creation_time=datetime.now())
return redirect(message_view)
context = dict()
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')
else:
context['messages'] = Message.objects.order_by('-creation_time')[:20]
context['archiveMode'] = False
return render(request, 'simpleforum/simpleforum.html', context)