import logging import mysite.settings from django.shortcuts import render, get_object_or_404 from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from django.utils import timezone from django.contrib.admin.views.decorators import staff_member_required from django.contrib.auth.models import User from .models import Post, CustomUser from taggit.models import Tag from .forms import PostForm, NewTagForm from django.contrib import messages from post_office import mail from hitcount.models import HitCount from hitcount.views import HitCountMixin 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) #external code customised #import from https://tutorial.djangogirls.org/en/django_forms/ #start --- @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]) 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() form.save_m2m() 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) messages.info(request, 'Der Artikel "' + post.title + '" wurde gelöscht') post.delete() return redirect('post_list') #end --- @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()) @login_required def search_add(request): 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) posts = posts.filter(tags__name__in=[search_query]) u = User.objects.get(username=request.user) if u: tagsuser = Tag.objects.filter(customuser__user = u) user_instance = get_object_or_404(CustomUser, user=request.user) if request.method == "POST": form = NewTagForm(request.POST, instance=user_instance) if form.is_valid(): obj = form.save(commit=False) obj.user = request.user tag_names = [tag.name for tag in Tag.objects.all()] m_tags = form.cleaned_data['tags'] m_tags = ' '.join(str(m_tags) for m_tags in m_tags) if m_tags in tag_names: obj.tags.add(m_tags) obj.save() messages.info(request, 'Der Tag "' + m_tags + '" wurde gespeichert') return redirect('/search/') else: messages.info(request, 'Sorry !! Den Tag den du suchst gibt es leider nicht!') else: form = NewTagForm() return render(request, 'search_add.html', locals()) #external code customised #import from https://github.com/ui/django-post_office mail.send( # List of email addresses also accepted 'kleinhenz.e@gmail.com', 'esther.kleinhenz@web.de', subject='My email', message='Hi there!', html_message='Hi there!', )