django_blog/blog/views.py
2017-12-08 03:06:02 +03:00

108 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
from django.shortcuts import render
from .models import Post
from django.db.models import Count, Max, Min, Q
from django.http import HttpResponse
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
from taggit.models import Tag
import math
def cloud():
# min_font = 11
# max_font = 28
# #size = max(min_font, round((min_font+(tag_list[i]-min_w)*step)*(math.log(tag_list[i])/Math.log(max_w))))
# v = Tag.objects.all().annotate(c = Count('post')).filter(c__gt = 0).aggregate(Min('c'), Max('c'))
# max_tag, min_tag = v["c__max"], v["c__min"]
# step = (max_font - min_font)/float(max_tag-min_tag)
#print step
"""
tags = Tag.objects.all().annotate(
c = Count('post'), f=min_font + (Count('post')-min_tag)*step).filter(
c__gt = 0).order_by('name')
"""
tags = Tag.objects.all().annotate(c = Count('post')).order_by('name').filter(c__gt = 0)
return tags
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')
search_query = ''
if request.GET.get('search'):
search_query = 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')
paginator = Paginator(post_list, 5)
page = request.GET.get('page')
latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9]
#cloud = cloud() local variable 'cloud' referenced before assignment
tag_cloud = cloud()
try:
posts = paginator.page(page)
except PageNotAnInteger: # If page is not an integer, deliver first page.
posts = paginator.page(1)
except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results.
posts = paginator.page(paginator.num_pages)
#print dir(posts.paginator)
return render_to_response('blog/post_list.html', {'posts': posts, 'latest': latest, 'cloud': tag_cloud, 'search_query': search_query})
# content = {'post' : Post.objects.get(pk=1)}
# return render(request, 'blog/post_detail.html')
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
tags = post.tags.names()
tag_cloud = cloud()
latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9]
if request.POST.get('action') == 'delete':
if request.user.is_authenticated():
post.delete()
return redirect('/')
else:
return redirect('/admin/login/?next=/post/' + pk)
return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags, 'latest': latest, 'cloud': tag_cloud})
@login_required(login_url="/admin/login/?next=/")
def post_new(request):
tag_cloud = cloud()
latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9]
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('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form, 'latest': latest, 'cloud': tag_cloud})
@login_required(login_url="/admin/login/?next=/")
def post_edit(request, pk):
tag_cloud = cloud()
latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9]
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('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form, 'latest': latest, 'cloud': tag_cloud})