blechreiz-website/messages/views.py

43 lines
1.5 KiB
Python

from django.contrib.auth.decorators import login_required
from django.shortcuts import render,redirect
from datetime import datetime
from models import Message
@login_required
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:
print "create"
Message.objects.create( titel = titel, text = text, author = request.user, creation_time = datetime.now() )
else:
print "Titel " + titel + " - text " + text
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, 01 )
from_date = datetime( year, month , 01 )
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, 'messages/messages.html', context )