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

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