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

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