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.8KB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django.shortcuts import render, redirect
  2. from .models import Notice
  3. from django.utils import timezone
  4. from .forms import NoticeForm
  5. from django.contrib.auth.decorators import login_required
  6. from django.contrib import messages
  7. @login_required
  8. def new(request):
  9. if request.method == 'POST':
  10. form = NoticeForm(request.POST)
  11. if form.is_valid():
  12. notice = Notice()
  13. notice.notice_title = form.cleaned_data['notice_title']
  14. notice.notice_text = form.cleaned_data['notice_text']
  15. notice.pub_start = form.cleaned_data['pub_start']
  16. notice.pub_end = form.cleaned_data['pub_end']
  17. notice.user_id = request.user.id
  18. notice.save()
  19. messages.success(request, f"Neue Nachricht erstellt")
  20. return redirect('index')
  21. else:
  22. for msg in form.error_messages:
  23. messages.error(request, f"{msg}: {form.error_messages[msg]}")
  24. context = {'form': NoticeForm()}
  25. return render(request, 'polls/edit.html', context)
  26. @login_required
  27. def delete(request, deleteId=None):
  28. if deleteId is not None:
  29. try:
  30. notice = Notice.objects.get(pk=deleteId)
  31. if request.user.id == notice.user_id or request.user.is_staff:
  32. notice.delete()
  33. messages.success(request, f"Nachricht gelöscht")
  34. else:
  35. messages.warning(request, f"Keine Berechtigung")
  36. except:
  37. messages.warning(request, f"Nachricht nicht gefunden")
  38. return redirect('index')
  39. def index(request):
  40. notices = Notice.objects.all()
  41. notices = notices.filter(pub_start__lte=timezone.now())
  42. notices = notices.filter(pub_end__gte=timezone.now())
  43. context = {'notices': notices}
  44. return render(request, 'polls/index.html', context)