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 2.8KB

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