123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- from django.shortcuts import render, get_object_or_404
- from django.utils import timezone
- from .models import Post, CustomUser
- from taggit.models import Tag
- from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
- from .forms import PostForm, NewTagForm
- from django.shortcuts import redirect
- from django.contrib.auth.decorators import login_required
- from django.contrib.admin.views.decorators import staff_member_required
- from django.contrib.auth import authenticate, login, logout
- import sys
- import collections
- from taggit_templatetags2.views import TagCanvasListView
- from django.contrib.auth.models import User
- from django.contrib import messages
- from post_office.models import EmailTemplate
- from post_office import mail
-
- from hitcount.models import HitCount
- from hitcount.views import HitCountMixin
-
-
- import logging
- import mysite.settings
- import operator
-
-
- def navlogin(request):
-
- log = logging.getLogger('mysite')
- logout(request)
- error = ""
- if request.POST:
- username = request.POST.get("username", "?")
- password = request.POST.get("password", "?")
-
- user = authenticate(username=username, password=password)
- if user is not None:
- if user.is_active:
- login(request, user)
- return redirect(mysite.settings.LOGIN_REDIRECT_URL)
- else:
- log.info("Inactive user {} tried to login".format(username))
- error = "Ihre Benutzerkennung wurde deaktiviert."
- else:
- log.info("Login failed for {}".format(username))
- error = "Benutzername oder Kennwort falsch."
- context = {'error': error}
- return render(request, 'index.html', context)
-
-
- @login_required
- def post_list(request, slug=None):
- log = logging.getLogger('mysite')
- posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
- if slug:
- tag = get_object_or_404(Tag, slug=slug)
- posts = posts.filter(tags__in=[tag])
- log.info(posts)
- return render(request, 'post_list.html', locals())
-
-
- @login_required
- def post_detail(request, pk):
- post = get_object_or_404(Post, pk=pk)
- hit_count = HitCount.objects.get_for_object(post)
- hit_count_response = HitCountMixin.hit_count(request, hit_count)
- return render(request, 'post_detail.html', locals())
-
-
- @login_required
- @staff_member_required
- def post_new(request):
- if request.method == "POST":
- form = PostForm(request.POST)
- if form.is_valid():
- post = form.save(commit=False)
- post.author = request.user
- post.save()
- return redirect('post_detail', pk=post.pk)
- else:
- form = PostForm()
- return render(request, 'post_edit.html', {'form': form})
-
-
- @login_required
- @staff_member_required
- def post_edit(request, pk):
- post = get_object_or_404(Post, pk=pk)
- if request.method == "POST":
- form = PostForm(request.POST, instance=post)
- if form.is_valid():
- post = form.save(commit=False)
- post.author = request.user
- post.save()
- form.save_m2m()
- return redirect('post_detail', pk=post.pk)
- else:
- form = PostForm(instance=post)
- return render(request, 'post_edit.html', {'form': form})
-
-
- @login_required
- @staff_member_required
- def post_draft_list(request):
- posts = Post.objects.filter(
- published_date__isnull=True).order_by('created_date')
- return render(request, 'post_draft_list.html', {'posts': posts})
-
-
- @login_required
- @staff_member_required
- def post_publish(request, pk):
- post = get_object_or_404(Post, pk=pk)
- post.publish()
- return redirect('post_detail', pk=pk)
-
-
- @login_required
- @staff_member_required
- def post_remove(request, pk):
- post = get_object_or_404(Post, pk=pk)
- post.delete()
- return redirect('post_list')
-
-
- @login_required
- def tag_remove(request, slug=None):
- if slug:
- tag = get_object_or_404(Tag, slug=slug)
- tag.delete()
- save_m2m()
- return redirect('student_page')
-
- @login_required
- def student_page(request):
- log = logging.getLogger('mysite')
- user_instance = get_object_or_404(CustomUser, user=request.user)
- log.info(user_instance)
- if request.method == "POST":
- log.info('post method')
- form = NewTagForm(request.POST, instance=user_instance)
- if form.is_valid():
- log.info('form is valid')
- obj = form.save(commit=False)
- obj.user = request.user
- obj.save()
- tag_names = [tag.name for tag in Tag.objects.all()]
- log.info(tag_names)
- m_tags = form.cleaned_data['m_tags']
- m_tags = ' '.join(str(m_tags) for m_tags in m_tags)
- log.info(m_tags)
- if m_tags in tag_names:
- log.info('worked out')
- obj.tags.add(m_tags)
- form.save_m2m()
- messages.info(request, 'Der Tag "' + m_tags + '" wurde gespeichert')
- return redirect('/student/')
- else:
- messages.info(request, 'Sorry !! Den Tag den du suchst gibt es leider nicht!')
- else:
- form = NewTagForm()
- return render(request, 'student_page.html', {'form':form})
-
- @login_required
- def tag_list(request):
- log = logging.getLogger('mysite')
- u = User.objects.get(username=request.user)
- if u:
- tags_user = Tag.objects.filter(customuser__user = u)
- log.info(tags_user)
- for tag in tags_user:
- posts = Post.objects.filter(tags__in=[tag]).order_by('-published_date')
- log.info(posts)
- return render(request, 'tag_list.html', locals())
-
- def blog_search_list_view(request):
- log = logging.getLogger('mysite')
- posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
- if request.method == 'GET':
- search_query = request.GET.get('search_box', None)
- log.info(search_query)
- posts = posts.filter(tags__name__in=[search_query])
- return render(request, 'blog_search_list_view.html', locals())
-
- def tag_cloud(request):
- return render(request, 'tag_cloud.html', {})
-
- mail.send(
- 'kleinhenz.e@gmail.com', # List of email addresses also accepted
- 'esther.kleinhenz@web.de',
- subject='My email',
- message='Hi there!',
- html_message='Hi <strong>there</strong>!',
- )
|