Solutions for MEIM
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.shortcuts import render, redirect
  2. from datetime import timedelta
  3. from django.utils import timezone
  4. from posts.models import Notice
  5. from posts.forms import NoticeForm
  6. from django.contrib.auth.decorators import login_required
  7. from django.contrib.admin.views.decorators import staff_member_required
  8. # Create your views here.
  9. #Titel und die Texte aller Meldungen, deren Veröffentlichungsdatum vor und der Endedatum nach dem aktuellen Datum liegt.
  10. def home(request, value=""):
  11. now = timezone.now()
  12. notices = Notice.objects.all()
  13. display_notices = list()
  14. for notice in notices:
  15. if notice.pub_start < now and notice.pub_end > now:
  16. display_notices.append(notice)
  17. context = {
  18. "title": "Beboop",
  19. "notices": display_notices
  20. }
  21. return render(request, 'index.html', context)
  22. @login_required
  23. def new(request):
  24. if request.method == "POST":
  25. form = NoticeForm(request.POST)
  26. if form.is_valid():
  27. newNotice = Notice(notice_title=form.cleaned_data['title'],
  28. notice_text=form.cleaned_data['text'],
  29. pub_start=form.cleaned_data['start'],
  30. pub_end=form.cleaned_data['end'])
  31. newNotice.save()
  32. return redirect('index')
  33. context = {'form': NoticeForm()}
  34. return render(request, 'edit.html', context)
  35. @staff_member_required
  36. def delete(request, value):
  37. notice = Notice.objects.get(id = value)
  38. notice.delete()
  39. return redirect('index')