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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, slug=None):
  99. if slug:
  100. tag = get_object_or_404(Tag, slug=slug)
  101. tag.delete()
  102. save_m2m()
  103. return redirect('student_page')
  104. @login_required
  105. def student_page(request):
  106. user_instance = get_object_or_404(CustomUser, user=request.user)
  107. if request.method == "POST":
  108. form = NewTagForm(request.POST, instance=user_instance)
  109. if form.is_valid():
  110. obj = form.save(commit=False)
  111. obj.user = request.user
  112. obj.save()
  113. m_tags = form.cleaned_data['m_tags']
  114. obj.tags.add(*m_tags)
  115. form.save_m2m()
  116. return redirect('/student/')
  117. else:
  118. form = NewTagForm()
  119. return render(request, 'student_page.html', {'form':form})
  120. @login_required
  121. def tag_list(request):
  122. u = CustomUser.objects.get(user=request.user)
  123. tags= Tag.objects.filter(customuser=u)
  124. arr=[]
  125. arr.append(tags)
  126. """
  127. for tag in tags:
  128. arr.append(str(tag))
  129. """
  130. listing = list(arr)
  131. return render(request, 'tag_list.html', {'tags':tags})
  132. class TagSearch(TagCanvasListView):
  133. template_name = 'blog_search_list_view.html'
  134. model = Post
  135. context_object_name = 'list'
  136. paginate_by = 8
  137. def get_context_data(self, **kwargs):
  138. context = super(CustomerSearch, self).get_context_data(**kwargs)
  139. context = self.get_queryset()
  140. return context
  141. def get_queryset(self):
  142. cObj = Customer.objects.all()
  143. var_get_search = self.request.GET.get('search_box')
  144. if var_get_search is not None:
  145. cObj = cObj.filter(firstname__icontains=var_get_search)
  146. if var_get_order_by is not None:
  147. cObj = cObj.order_by(var_get_order_by)
  148. return cObj
  149. def blog_search_list_view(request):
  150. return render(request, 'blog_search_list_view.html', {})
  151. def tag_cloud(request):
  152. return render(request, 'tag_cloud.html', {})
  153. """
  154. @login_required
  155. def post_of_user_list(request):
  156. u= CustomUser.objects.get(user=request.user)
  157. tag =[]
  158. posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
  159. if slug:
  160. tag = get_object_or_404(Tag, slug=slug)
  161. posts= posts.filter(tags__in=[tag])
  162. page = request.GET.get('page')
  163. return render(request, 'post_list.html', locals())
  164. """