From a9e0752cf47415b84d0f1e6042ba8e46f436aa41 Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 17 Nov 2017 15:32:56 +0300 Subject: [PATCH] upd --- blog/templates/blog/base.html | 2 +- blog/views.py | 48 ++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/blog/templates/blog/base.html b/blog/templates/blog/base.html index a7271ce..299f49a 100644 --- a/blog/templates/blog/base.html +++ b/blog/templates/blog/base.html @@ -47,7 +47,7 @@

Теги

- {% for tag in tags %} + {% for tag in cloud %} {{ tag.name }}({{ tag.c }}) {% endfor %} diff --git a/blog/views.py b/blog/views.py index 865961c..a8042ab 100644 --- a/blog/views.py +++ b/blog/views.py @@ -9,6 +9,19 @@ 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)/(max_tag-min_tag) + tags = Tag.objects.all().annotate( + c = Count('post'), f=min_font + (Count('post')-min_tag)*step).filter( + c__gt = 0).order_by('name') + return tags def post_list(request, page='1', tag=None): if tag is None: @@ -20,7 +33,8 @@ def post_list(request, page='1', tag=None): 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] - tags = Tag.objects.all().annotate(c = Count('post'), f=Count('post')**(1/1.05)+12).filter(c__gt = 0).order_by('name') + #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. @@ -28,24 +42,26 @@ def post_list(request, page='1', tag=None): 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, 'tags': tags}) + return render_to_response('blog/post_list.html', {'posts': posts, 'latest': latest, 'cloud': tag_cloud}) def post_detail(request, pk): - post = get_object_or_404(Post, pk=pk) - tags = post.tags.names() - 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=/blog/') - return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags, 'latest': latest}) + 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=/blog/') + return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags, 'latest': latest, 'cloud': tag_cloud}) @login_required(login_url="/admin/login/?next=/blog/") 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(): @@ -57,11 +73,13 @@ def post_new(request): return redirect('blog.views.post_detail', pk=post.pk) else: form = PostForm() - return render(request, 'blog/post_edit.html', {'form': form}) + return render(request, 'blog/post_edit.html', {'form': form, 'latest': latest, 'cloud': tag_cloud}) @login_required(login_url="/admin/login/?next=/blog/") 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) @@ -74,4 +92,4 @@ def post_edit(request, pk): return redirect('blog.views.post_detail', pk=post.pk) else: form = PostForm(instance=post) - return render(request, 'blog/post_edit.html', {'form': form}) + return render(request, 'blog/post_edit.html', {'form': form, 'latest': latest, 'cloud': tag_cloud})