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

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from django.shortcuts import render, get_object_or_404
  2. from django.utils import timezone
  3. from .models import Post, CustomUser
  4. from .forms import PostForm, NewTagForm
  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. from taggit_templatetags2.views import TagCanvasListView
  11. import logging
  12. import mysite.settings
  13. import operator
  14. # Create your views here.
  15. def navlogin(request):
  16. log = logging.getLogger('medinf')
  17. logout(request)
  18. error = ""
  19. if request.POST:
  20. username = request.POST.get("username", "?")
  21. password = request.POST.get("password", "?")
  22. user = authenticate(username=username, password=password)
  23. if user is not None:
  24. if user.is_active:
  25. login(request, user)
  26. return redirect(mysite.settings.LOGIN_REDIRECT_URL)
  27. else:
  28. log.info("Inactive user {} tried to login".format(username))
  29. error = "Ihre Benutzerkennung wurde deaktiviert."
  30. else:
  31. log.info("Login failed for {}".format(username))
  32. error = "Benutzername oder Kennwort falsch."
  33. context = {'error': error}
  34. return render(request, 'index.html', context)
  35. @login_required
  36. def post_list(request):
  37. posts = Post.objects.filter(
  38. published_date__lte=timezone.now()).order_by('published_date')
  39. return render(request, 'post_list.html', {'posts': posts})
  40. @login_required
  41. def post_detail(request, pk):
  42. post = get_object_or_404(Post, pk=pk)
  43. return render(request, 'post_detail.html', {'post': post})
  44. @login_required
  45. @staff_member_required
  46. def post_new(request):
  47. if request.method == "POST":
  48. form = PostForm(request.POST)
  49. if form.is_valid():
  50. post = form.save(commit=False)
  51. post.author = request.user
  52. post.save()
  53. return redirect('post_detail', pk=post.pk)
  54. else:
  55. form = PostForm()
  56. return render(request, 'post_edit.html', {'form': form})
  57. @login_required
  58. @staff_member_required
  59. def post_edit(request, pk):
  60. post = get_object_or_404(Post, pk=pk)
  61. if request.method == "POST":
  62. form = PostForm(request.POST, instance=post)
  63. if form.is_valid():
  64. post = form.save(commit=False)
  65. post.author = request.user
  66. post.save()
  67. form.save_m2m()
  68. return redirect('post_detail', pk=post.pk)
  69. else:
  70. form = PostForm(instance=post)
  71. return render(request, 'post_edit.html', {'form': form})
  72. @login_required
  73. @staff_member_required
  74. def post_draft_list(request):
  75. posts = Post.objects.filter(
  76. published_date__isnull=True).order_by('created_date')
  77. return render(request, 'post_draft_list.html', {'posts': posts})
  78. @login_required
  79. @staff_member_required
  80. def post_publish(request, pk):
  81. post = get_object_or_404(Post, pk=pk)
  82. post.publish()
  83. return redirect('post_detail', pk=pk)
  84. @login_required
  85. @staff_member_required
  86. def post_remove(request, pk):
  87. post = get_object_or_404(Post, pk=pk)
  88. post.delete()
  89. return redirect('post_list')
  90. @login_required
  91. def student_page(request):
  92. user_instance = CustomUser.objects.get(user=request.user)
  93. if request.method == "POST":
  94. form = NewTagForm(request.POST, instance=user_instance)
  95. if form.is_valid():
  96. obj = form.save(commit=False)
  97. obj.user = request.user
  98. obj.save()
  99. m_tags = form.cleaned_data['m_tags']
  100. obj.tags.add(*m_tags)
  101. form.save_m2m()
  102. return redirect('/student/')
  103. else:
  104. form = NewTagForm()
  105. return render(request, 'student_page.html', {'form':form})
  106. class TagSearch(TagCanvasListView):
  107. template_name = 'blog_search_list_view.html'
  108. model = Post
  109. context_object_name = 'list'
  110. paginate_by = 8
  111. def get_context_data(self, **kwargs):
  112. context = super(CustomerSearch, self).get_context_data(**kwargs)
  113. context = self.get_queryset()
  114. return context
  115. def get_queryset(self):
  116. cObj = Customer.objects.all()
  117. var_get_search = self.request.GET.get('search_box')
  118. if var_get_search is not None:
  119. cObj = cObj.filter(firstname__icontains=var_get_search)
  120. if var_get_order_by is not None:
  121. cObj = cObj.order_by(var_get_order_by)
  122. return cObj
  123. def blog_search_list_view(request):
  124. return render(request, 'blog_search_list_view.html', {})
  125. def tag_cloud(request):
  126. return render(request, 'tag_cloud.html', {})