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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. import sys
  12. import collections
  13. from taggit_templatetags2.views import TagCanvasListView
  14. from django.contrib.auth.models import User
  15. from django.contrib import messages
  16. from post_office.models import EmailTemplate
  17. from post_office import mail
  18. from hitcount.models import HitCount
  19. from hitcount.views import HitCountMixin
  20. import logging
  21. import mysite.settings
  22. import operator
  23. def navlogin(request):
  24. log = logging.getLogger('mysite')
  25. logout(request)
  26. error = ""
  27. if request.POST:
  28. username = request.POST.get("username", "?")
  29. password = request.POST.get("password", "?")
  30. user = authenticate(username=username, password=password)
  31. if user is not None:
  32. if user.is_active:
  33. login(request, user)
  34. return redirect(mysite.settings.LOGIN_REDIRECT_URL)
  35. else:
  36. log.info("Inactive user {} tried to login".format(username))
  37. error = "Ihre Benutzerkennung wurde deaktiviert."
  38. else:
  39. log.info("Login failed for {}".format(username))
  40. error = "Benutzername oder Kennwort falsch."
  41. context = {'error': error}
  42. return render(request, 'index.html', context)
  43. @login_required
  44. def post_list(request, slug=None):
  45. log = logging.getLogger('mysite')
  46. posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
  47. if slug:
  48. tag = get_object_or_404(Tag, slug=slug)
  49. posts = posts.filter(tags__in=[tag])
  50. return render(request, 'post_list.html', locals())
  51. @login_required
  52. def post_detail(request, pk):
  53. post = get_object_or_404(Post, pk=pk)
  54. hit_count = HitCount.objects.get_for_object(post)
  55. hit_count_response = HitCountMixin.hit_count(request, hit_count)
  56. return render(request, 'post_detail.html', locals())
  57. @login_required
  58. @staff_member_required
  59. def post_new(request):
  60. if request.method == "POST":
  61. form = PostForm(request.POST)
  62. if form.is_valid():
  63. post = form.save(commit=False)
  64. post.author = request.user
  65. post.save()
  66. form.save_m2m()
  67. return redirect('post_detail', pk=post.pk)
  68. else:
  69. form = PostForm()
  70. return render(request, 'post_edit.html', {'form': form})
  71. @login_required
  72. @staff_member_required
  73. def post_edit(request, pk):
  74. post = get_object_or_404(Post, pk=pk)
  75. if request.method == "POST":
  76. form = PostForm(request.POST, instance=post)
  77. if form.is_valid():
  78. post = form.save(commit=False)
  79. post.author = request.user
  80. post.save()
  81. form.save_m2m()
  82. return redirect('post_detail', pk=post.pk)
  83. else:
  84. form = PostForm(instance=post)
  85. return render(request, 'post_edit.html', {'form': form})
  86. @login_required
  87. @staff_member_required
  88. def post_draft_list(request):
  89. posts = Post.objects.filter(
  90. published_date__isnull=True).order_by('created_date')
  91. return render(request, 'post_draft_list.html', {'posts': posts})
  92. @login_required
  93. @staff_member_required
  94. def post_publish(request, pk):
  95. post = get_object_or_404(Post, pk=pk)
  96. post.publish()
  97. return redirect('post_detail', pk=pk)
  98. @login_required
  99. @staff_member_required
  100. def post_remove(request, pk):
  101. post = get_object_or_404(Post, pk=pk)
  102. messages.info(request, 'Der Artikel "' + post.title + '" wurde gelöscht')
  103. post.delete()
  104. return redirect('post_list')
  105. """ @login_required
  106. def tag_remove(request, slug=None):
  107. log = logging.getLogger('mysite')
  108. user_instance = get_object_or_404(CustomUser, user=request.user)
  109. tag = Tag.get_object_or_404(Tag, slug = slug)
  110. log.info(tag)
  111. if slug:
  112. user_instance.tags.remove(tag)
  113. save_m2m()
  114. return redirect('student_page') """
  115. @login_required
  116. def tag_list(request):
  117. log = logging.getLogger('mysite')
  118. u = User.objects.get(username=request.user)
  119. if u:
  120. tags_user = Tag.objects.filter(customuser__user = u)
  121. log.info(tags_user)
  122. for tag in tags_user:
  123. posts = Post.objects.filter(tags__in=[tag]).order_by('-published_date')
  124. log.info(posts)
  125. return render(request, 'tag_list.html', locals())
  126. @login_required
  127. def search_add(request):
  128. posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
  129. if request.method == 'GET':
  130. search_query = request.GET.get('search_box', None)
  131. posts = posts.filter(tags__name__in=[search_query])
  132. u = User.objects.get(username=request.user)
  133. if u:
  134. tagsuser = Tag.objects.filter(customuser__user = u)
  135. user_instance = get_object_or_404(CustomUser, user=request.user)
  136. if request.method == "POST":
  137. form = NewTagForm(request.POST, instance=user_instance)
  138. if form.is_valid():
  139. obj = form.save(commit=False)
  140. obj.user = request.user
  141. tag_names = [tag.name for tag in Tag.objects.all()]
  142. m_tags = form.cleaned_data['tags']
  143. m_tags = ' '.join(str(m_tags) for m_tags in m_tags)
  144. if m_tags in tag_names:
  145. obj.tags.add(m_tags)
  146. obj.save()
  147. messages.info(request, 'Der Tag "' + m_tags + '" wurde gespeichert')
  148. return redirect('/search/')
  149. else:
  150. messages.info(request, 'Sorry !! Den Tag den du suchst gibt es leider nicht!')
  151. else:
  152. form = NewTagForm()
  153. return render(request, 'search_add.html', locals())
  154. mail.send(
  155. 'kleinhenz.e@gmail.com', # List of email addresses also accepted
  156. 'esther.kleinhenz@web.de',
  157. subject='My email',
  158. message='Hi there!',
  159. html_message='Hi <strong>there</strong>!',
  160. )