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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. 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. post.delete()
  103. return redirect('post_list')
  104. @login_required
  105. def tag_remove(request, slug=None):
  106. if slug:
  107. tag = get_object_or_404(Tag, slug=slug)
  108. tag.delete()
  109. save_m2m()
  110. return redirect('student_page')
  111. @login_required
  112. def student_page(request):
  113. log = logging.getLogger('mysite')
  114. user_instance = get_object_or_404(CustomUser, user=request.user)
  115. log.info(user_instance)
  116. if request.method == "POST":
  117. log.info('post method')
  118. form = NewTagForm(request.POST, instance=user_instance)
  119. if form.is_valid():
  120. log.info('form is valid')
  121. obj = form.save(commit=False)
  122. obj.user = request.user
  123. obj.save()
  124. tag_names = [tag.name for tag in Tag.objects.all()]
  125. log.info(tag_names)
  126. m_tags = form.cleaned_data['m_tags']
  127. m_tags = ' '.join(str(m_tags) for m_tags in m_tags)
  128. log.info(m_tags)
  129. if m_tags in tag_names:
  130. log.info('worked out')
  131. obj.tags.add(m_tags)
  132. form.save_m2m()
  133. messages.info(request, 'Der Tag "' + m_tags + '" wurde gespeichert')
  134. return redirect('/student/')
  135. else:
  136. messages.info(request, 'Sorry !! Den Tag den du suchst gibt es leider nicht!')
  137. else:
  138. form = NewTagForm()
  139. return render(request, 'student_page.html', {'form':form})
  140. @login_required
  141. def tag_list(request):
  142. log = logging.getLogger('mysite')
  143. u = User.objects.get(username=request.user)
  144. if u:
  145. tags_user = Tag.objects.filter(customuser__user = u)
  146. log.info(tags_user)
  147. for tag in tags_user:
  148. posts = Post.objects.filter(tags__in=[tag]).order_by('-published_date')
  149. log.info(posts)
  150. return render(request, 'tag_list.html', locals())
  151. def blog_search_list_view(request):
  152. log = logging.getLogger('mysite')
  153. posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
  154. if request.method == 'GET':
  155. search_query = request.GET.get('search_box', None)
  156. log.info(search_query)
  157. posts = posts.filter(tags__name__in=[search_query])
  158. return render(request, 'blog_search_list_view.html', locals())
  159. def tag_cloud(request):
  160. return render(request, 'tag_cloud.html', {})
  161. mail.send(
  162. 'kleinhenz.e@gmail.com', # List of email addresses also accepted
  163. 'esther.kleinhenz@web.de',
  164. subject='My email',
  165. message='Hi there!',
  166. html_message='Hi <strong>there</strong>!',
  167. )