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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. from django.db.models import Q
  10. import logging
  11. import mysite.settings
  12. import operator
  13. # Create your views here.
  14. def navlogin(request):
  15. log = logging.getLogger('medinf')
  16. logout(request)
  17. error = ""
  18. if request.POST:
  19. username = request.POST.get("username", "?")
  20. password = request.POST.get("password", "?")
  21. user = authenticate(username=username, password=password)
  22. if user is not None:
  23. if user.is_active:
  24. login(request, user)
  25. return redirect(mysite.settings.LOGIN_REDIRECT_URL)
  26. else:
  27. log.info("Inactive user {} tried to login".format(username))
  28. error = "Ihre Benutzerkennung wurde deaktiviert."
  29. else:
  30. log.info("Login failed for {}".format(username))
  31. error = "Benutzername oder Kennwort falsch."
  32. context = {'error': error}
  33. return render(request, 'index.html', context)
  34. @login_required
  35. def post_list(request):
  36. posts = Post.objects.filter(
  37. published_date__lte=timezone.now()).order_by('published_date')
  38. return render(request, 'post_list.html', {'posts': posts})
  39. @login_required
  40. def post_detail(request, pk):
  41. post = get_object_or_404(Post, pk=pk)
  42. return render(request, 'post_detail.html', {'post': post})
  43. @login_required
  44. @staff_member_required
  45. def post_new(request):
  46. if request.method == "POST":
  47. form = PostForm(request.POST)
  48. if form.is_valid():
  49. post = form.save(commit=False)
  50. post.author = request.user
  51. post.save()
  52. return redirect('post_detail', pk=post.pk)
  53. else:
  54. form = PostForm()
  55. return render(request, 'post_edit.html', {'form': form})
  56. @login_required
  57. @staff_member_required
  58. def post_edit(request, pk):
  59. post = get_object_or_404(Post, pk=pk)
  60. if request.method == "POST":
  61. form = PostForm(request.POST, instance=post)
  62. if form.is_valid():
  63. post = form.save(commit=False)
  64. post.author = request.user
  65. post.save()
  66. return redirect('post_detail', pk=post.pk)
  67. else:
  68. form = PostForm(instance=post)
  69. return render(request, 'post_edit.html', {'form': form})
  70. @login_required
  71. @staff_member_required
  72. def post_draft_list(request):
  73. posts = Post.objects.filter(
  74. published_date__isnull=True).order_by('created_date')
  75. return render(request, 'post_draft_list.html', {'posts': posts})
  76. @login_required
  77. @staff_member_required
  78. def post_publish(request, pk):
  79. post = get_object_or_404(Post, pk=pk)
  80. post.publish()
  81. return redirect('post_detail', pk=pk)
  82. @login_required
  83. @staff_member_required
  84. def post_remove(request, pk):
  85. post = get_object_or_404(Post, pk=pk)
  86. post.delete()
  87. return redirect('post_list')
  88. @login_required
  89. def student_page(request):
  90. return render(request, 'student_page.html', {})
  91. """ Search for querys
  92. def blog_search_list_view(post_list, self):
  93. result = super(post_list, self).get_queryset()
  94. query = self.request.GET.get('q')
  95. if query:
  96. query_list = query.split()
  97. result = result.filter(
  98. reduce(operator.and_,
  99. (Q(title__icontains=q) for q in query_list)) |
  100. reduce(operator.and_,
  101. (Q(content__icontains=q) for q in query_list))
  102. )
  103. return result """