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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. log.info(posts)
  51. return render(request, 'post_list.html', locals())
  52. @login_required
  53. def post_detail(request, pk):
  54. post = get_object_or_404(Post, pk=pk)
  55. hit_count = HitCount.objects.get_for_object(post)
  56. hit_count_response = HitCountMixin.hit_count(request, hit_count)
  57. return render(request, 'post_detail.html', locals())
  58. @login_required
  59. @staff_member_required
  60. def post_new(request):
  61. if request.method == "POST":
  62. form = PostForm(request.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()
  71. return render(request, 'post_edit.html', {'form': form})
  72. @login_required
  73. @staff_member_required
  74. def post_edit(request, pk):
  75. post = get_object_or_404(Post, pk=pk)
  76. if request.method == "POST":
  77. form = PostForm(request.POST, instance=post)
  78. if form.is_valid():
  79. post = form.save(commit=False)
  80. post.author = request.user
  81. post.save()
  82. form.save_m2m()
  83. return redirect('post_detail', pk=post.pk)
  84. else:
  85. form = PostForm(instance=post)
  86. return render(request, 'post_edit.html', {'form': form})
  87. @login_required
  88. @staff_member_required
  89. def post_draft_list(request):
  90. posts = Post.objects.filter(
  91. published_date__isnull=True).order_by('created_date')
  92. return render(request, 'post_draft_list.html', {'posts': posts})
  93. @login_required
  94. @staff_member_required
  95. def post_publish(request, pk):
  96. post = get_object_or_404(Post, pk=pk)
  97. post.publish()
  98. return redirect('post_detail', pk=pk)
  99. @login_required
  100. @staff_member_required
  101. def post_remove(request, pk):
  102. post = get_object_or_404(Post, pk=pk)
  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. log.info(u)
  110. tag = Tag.get_object_or_404(Tag, slug = slug)
  111. log.info(tag)
  112. if slug:
  113. user_instance.tags.remove(tag)
  114. save_m2m()
  115. return redirect('student_page')
  116. @login_required
  117. def student_page(request):
  118. log = logging.getLogger('mysite')
  119. user_instance = get_object_or_404(CustomUser, user=request.user)
  120. if request.method == "POST":
  121. log.info('post method')
  122. form = NewTagForm(request.POST, instance=user_instance)
  123. if form.is_valid():
  124. log.info('form is valid')
  125. obj = form.save(commit=False)
  126. obj.user = request.user
  127. obj.save()
  128. tag_names = [tag.name for tag in Tag.objects.all()]
  129. log.info(tag_names)
  130. m_tags = form.cleaned_data['tags']
  131. m_tags = ' '.join(str(m_tags) for m_tags in m_tags)
  132. log.info(m_tags)
  133. if m_tags in tag_names:
  134. log.info('worked out')
  135. obj.tags.add(m_tags)
  136. form.save_m2m()
  137. messages.info(request, 'Der Tag "' + m_tags + '" wurde gespeichert')
  138. return redirect('/student/')
  139. else:
  140. messages.info(request, 'Sorry !! Den Tag den du suchst gibt es leider nicht!')
  141. else:
  142. form = NewTagForm()
  143. return render(request, 'student_page.html', {'form':form})
  144. @login_required
  145. def tag_list(request):
  146. log = logging.getLogger('mysite')
  147. u = User.objects.get(username=request.user)
  148. if u:
  149. tags_user = Tag.objects.filter(customuser__user = u)
  150. log.info(tags_user)
  151. for tag in tags_user:
  152. posts = Post.objects.filter(tags__in=[tag]).order_by('-published_date')
  153. log.info(posts)
  154. return render(request, 'tag_list.html', locals())
  155. def blog_search_list_view(request):
  156. log = logging.getLogger('mysite')
  157. posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
  158. if request.method == 'GET':
  159. search_query = request.GET.get('search_box', None)
  160. log.info(search_query)
  161. posts = posts.filter(tags__name__in=[search_query])
  162. return render(request, 'blog_search_list_view.html', locals())
  163. def tag_cloud(request):
  164. return render(request, 'tag_cloud.html', {})
  165. mail.send(
  166. 'kleinhenz.e@gmail.com', # List of email addresses also accepted
  167. 'esther.kleinhenz@web.de',
  168. subject='My email',
  169. message='Hi there!',
  170. html_message='Hi <strong>there</strong>!',
  171. )