django_blog/blog/views.py

78 lines
3.4 KiB
Python
Raw Normal View History

2017-10-29 13:40:42 +03:00
# -*- coding: utf-8 -*-
from django.shortcuts import render
from .models import Post
2017-11-13 21:12:12 +03:00
from django.db.models import Count, Max, Min, Q
2017-10-29 13:40:42 +03:00
from django.utils import timezone
from django.shortcuts import render, get_object_or_404, render_to_response
from django.shortcuts import redirect
from .forms import PostForm
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import login_required
2017-11-09 17:34:42 +03:00
from taggit.models import Tag
2017-10-29 13:40:42 +03:00
2017-11-09 17:34:42 +03:00
def post_list(request, page='1', tag=None):
if tag is None:
post_list = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')#[int(page)*5-5:int(page)*5]
else:
post_list = Post.objects.filter(tags__name=tag).order_by('-published_date')
if request.GET.get('search'):
post_list = Post.objects.filter(Q(title__contains = request.GET.get('search')) | Q(text__contains = request.GET.get('search'))).order_by('-published_date')
2017-10-29 13:40:42 +03:00
paginator = Paginator(post_list, 5)
page = request.GET.get('page')
2017-11-09 17:34:42 +03:00
latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9]
2017-11-13 21:12:12 +03:00
tags = Tag.objects.all().annotate(c = Count('post'), f=Count('post')**(1/1.05)+12).filter(c__gt = 0).order_by('name')
2017-10-29 13:40:42 +03:00
try:
posts = paginator.page(page)
2017-11-09 17:34:42 +03:00
except PageNotAnInteger: # If page is not an integer, deliver first page.
2017-10-29 13:40:42 +03:00
posts = paginator.page(1)
2017-11-09 17:34:42 +03:00
except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results.
2017-10-29 13:40:42 +03:00
posts = paginator.page(paginator.num_pages)
2017-11-09 17:34:42 +03:00
print dir(posts.paginator)
return render_to_response('blog/post_list.html', {'posts': posts, 'latest': latest, 'tags': tags})
2017-10-29 13:40:42 +03:00
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
tags = post.tags.names()
2017-11-09 17:34:42 +03:00
latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9]
2017-10-29 13:40:42 +03:00
if request.POST.get('action') == 'delete':
2017-11-09 17:34:42 +03:00
if request.user.is_authenticated():
post.delete()
return redirect('/')
else:
return redirect('/admin/login/?next=/blog/')
return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags, 'latest': latest})
2017-10-29 13:40:42 +03:00
@login_required(login_url="/admin/login/?next=/blog/")
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.published_date = timezone.now()
post.save()
form.save_m2m()
return redirect('blog.views.post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
2017-11-09 17:34:42 +03:00
2017-10-29 13:40:42 +03:00
@login_required(login_url="/admin/login/?next=/blog/")
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.published_date = timezone.now()
post.save()
form.save_m2m()
return redirect('blog.views.post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})