|
|
@@ -1,13 +1,109 @@ |
|
|
|
from posts.models import Notice |
|
|
|
from django.utils import timezone |
|
|
|
from django.shortcuts import render |
|
|
|
from django.http import HttpResponse |
|
|
|
|
|
|
|
# Create your views here. |
|
|
|
from django.http import HttpResponse |
|
|
|
|
|
|
|
def index(request): |
|
|
|
#return render(request, 'posts/index.html') |
|
|
|
|
|
|
|
#context = # hier muss Datenbank ausgelesen werden: |
|
|
|
# for curr_msg in Notice.objects.all(): |
|
|
|
# if (curr_msg.pub_start > datetime.now() and curr_msg.pub_end < date...... |
|
|
|
# context += |
|
|
|
return render(request, 'posts/allMessages.html') |
|
|
|
#-------------------- eigener Versuch --------------------- |
|
|
|
|
|
|
|
# relevant_notices = Notice.objects.filter(pub_start__lt = timezone.datetime.now()) |
|
|
|
# relevant_notices = Notice.objects.filter(pub_end__gt = timezone.datetime.now()) |
|
|
|
|
|
|
|
# for curr_msg in relevant_notices: |
|
|
|
# context['now'] += "TITEL DER NACHRICHT: " + curr_msg.notice_title + "TEXT DER NACHRICHT: " + curr_msg.notice_text |
|
|
|
# # unschöne Lösung, da hier ein Fließtext ohne Layout-Möglichkeiten (z.B. backspace zwischen jeder neuen Zeile) entsteht |
|
|
|
# # bessere Variante: for-Schleife in das Template (allMessages.html) auslagern |
|
|
|
|
|
|
|
#------------------ ab hier: schöne Variante -------------------- |
|
|
|
|
|
|
|
relevant_notices = Notice.objects.all() |
|
|
|
relevant_notices = Notice.objects.filter(pub_start__lt=timezone.datetime.now()) |
|
|
|
relevant_notices = Notice.objects.filter(pub_end__gt=timezone.datetime.now()) |
|
|
|
# all diese Befehle importieren noch NHCTS aus der Datenbank! Sie bauen nur einen SQL-Zugriffs-Befehl auf (mit entsprevchender Filterung) und erst beim Auslesen wird auf Datenbank zugegriffen |
|
|
|
|
|
|
|
context = { "notices" : relevant_notices } |
|
|
|
|
|
|
|
return render(request, 'posts/allMessages.html', context) |
|
|
|
|
|
|
|
|
|
|
|
from posts.forms import NoticeForm |
|
|
|
from django.shortcuts import redirect |
|
|
|
|
|
|
|
def new(request): |
|
|
|
#return HttpResponse("Eingabe einer neuen Nachricht!") |
|
|
|
if request.method == "POST": |
|
|
|
eingabeFenster = NoticeForm(request.POST) |
|
|
|
if eingabeFenster.is_valid(): |
|
|
|
newNotice = Notice(notice_title = eingabeFenster.cleaned_data['title'], |
|
|
|
notice_text = eingabeFenster.cleaned_data['text'], |
|
|
|
pub_start = eingabeFenster.cleaned_data['start'], |
|
|
|
pub_end = eingabeFenster.cleaned_data['end']) |
|
|
|
newNotice.save() |
|
|
|
return redirect('index') |
|
|
|
context = {'form' : NoticeForm() } |
|
|
|
return render(request, 'posts/edit.html', context) |
|
|
|
|
|
|
|
def delete(request, id): |
|
|
|
msg_to_delete = Notice.objects.get(id = id) |
|
|
|
msg_to_delete.clean() |
|
|
|
return redirect('index') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from posts.models import Notice |
|
|
|
from posts.serializers import NoticeSerializer |
|
|
|
from django.http import JsonResponse |
|
|
|
from rest_framework.parsers import JSONParser |
|
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
|
|
|
|
|
|
@csrf_exempt |
|
|
|
def notice_list(request): |
|
|
|
if request.method == 'GET': |
|
|
|
notices = Notice.objects.all() |
|
|
|
serializer = NoticeSerializer(notices, many=True) |
|
|
|
return JsonResponse(serializer.data, safe=False) |
|
|
|
|
|
|
|
elif request.method == 'POST': |
|
|
|
data = JSONParser().parse(request) |
|
|
|
serializer = NoticeSerializer(data = data) |
|
|
|
if serializer.is_valid(): |
|
|
|
serializer.save() |
|
|
|
return JsonResponse(serializer.data, status=201) |
|
|
|
else: |
|
|
|
return JsonResponse(serializer.errors, status=400) |
|
|
|
|
|
|
|
else: |
|
|
|
return HttpResponse(status=405) |
|
|
|
|
|
|
|
@csrf_exempt |
|
|
|
def notice_detail(request, id): |
|
|
|
try: |
|
|
|
notice = Notice.objects.get(id=id) |
|
|
|
except: |
|
|
|
return HttpResponse(status=404) |
|
|
|
|
|
|
|
if request.method == 'GET': |
|
|
|
serializer = NoticeSerializer(notice) |
|
|
|
return JsonResponse(serializer.data) |
|
|
|
|
|
|
|
elif request.method== 'PUT': |
|
|
|
data = JSONParser().parse(request) |
|
|
|
serializer = NoticeSerializer(notice, data=data) |
|
|
|
if serializer.is_valid(): |
|
|
|
serializer.save() |
|
|
|
return JsonResponse(serializer.data) |
|
|
|
else: |
|
|
|
return JsonResponse(serializer.errors, status=400) |
|
|
|
|
|
|
|
elif request.method == 'DELETE': |
|
|
|
notice.delete() |
|
|
|
return HttpResponse(status=204) |
|
|
|
|
|
|
|
else: |
|
|
|
return HttpResponse(status=405) |
|
|
|
|
|
|
|
|