Development of an internal social media platform with personalised dashboards for students
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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from django.shortcuts import render, get_object_or_404
  2. from django.utils import timezone
  3. from .models import Post
  4. from .forms import PostForm
  5. from django.shortcuts import redirect
  6. from django.contrib.auth.decorators import login_required
  7. from django.contrib.admin.views.decorators import staff_member_required
  8. from django.contrib.auth import authenticate, login, logout
  9. import logging
  10. import mysite.settings
  11. # Create your views here.
  12. def navlogin(request):
  13. log = logging.getLogger('medinf')
  14. logout(request)
  15. error = ""
  16. if request.POST:
  17. username = request.POST.get("username", "?")
  18. password = request.POST.get("password", "?")
  19. user = authenticate(username=username, password=password)
  20. if user is not None:
  21. if user.is_active:
  22. login(request, user)
  23. return redirect(mysite.settings.LOGIN_REDIRECT_URL)
  24. else:
  25. log.info("Inactive user {} tried to login".format(username))
  26. error = "Ihre Benutzerkennung wurde deaktiviert."
  27. else:
  28. log.info("Login failed for {}".format(username))
  29. error = "Benutzername oder Kennwort falsch."
  30. context = {'error': error}
  31. return render(request, 'index.html', context)
  32. @login_required
  33. def post_list(request):
  34. posts = Post.objects.filter(
  35. published_date__lte=timezone.now()).order_by('published_date')
  36. return render(request, 'post_list.html', {'posts': posts})
  37. @login_required
  38. def post_detail(request, pk):
  39. post = get_object_or_404(Post, pk=pk)
  40. return render(request, 'post_detail.html', {'post': post})
  41. @login_required
  42. @staff_member_required
  43. def post_new(request):
  44. if request.method == "POST":
  45. form = PostForm(request.POST)
  46. if form.is_valid():
  47. post = form.save(commit=False)
  48. post.author = request.user
  49. post.save()
  50. return redirect('post_detail', pk=post.pk)
  51. else:
  52. form = PostForm()
  53. return render(request, 'post_edit.html', {'form': form})
  54. @login_required
  55. @staff_member_required
  56. def post_edit(request, pk):
  57. post = get_object_or_404(Post, pk=pk)
  58. if request.method == "POST":
  59. form = PostForm(request.POST, instance=post)
  60. if form.is_valid():
  61. post = form.save(commit=False)
  62. post.author = request.user
  63. post.save()
  64. return redirect('post_detail', pk=post.pk)
  65. else:
  66. form = PostForm(instance=post)
  67. return render(request, 'post_edit.html', {'form': form})
  68. @login_required
  69. @staff_member_required
  70. def post_draft_list(request):
  71. posts = Post.objects.filter(
  72. published_date__isnull=True).order_by('created_date')
  73. return render(request, 'post_draft_list.html', {'posts': posts})
  74. @login_required
  75. @staff_member_required
  76. def post_publish(request, pk):
  77. post = get_object_or_404(Post, pk=pk)
  78. post.publish()
  79. return redirect('post_detail', pk=pk)
  80. @login_required
  81. @staff_member_required
  82. def post_remove(request, pk):
  83. post = get_object_or_404(Post, pk=pk)
  84. post.delete()
  85. return redirect('post_list')
  86. @login_required
  87. def student_page(request):
  88. return render(request, 'student_page.html', {})