This commit is contained in:
Anna Sudnitsina 2017-11-21 15:02:43 +03:00
parent 67306c6032
commit 925e62be0c
5 changed files with 39 additions and 20 deletions

View File

@ -4,6 +4,8 @@ from django.db import models
from django.utils import timezone
from tinymce.models import HTMLField
from taggit.managers import TaggableManager
from taggit.models import Tag
from django.db.models import Count, Max, Min
class Post(models.Model):
author = models.ForeignKey('auth.User')
@ -22,3 +24,16 @@ class Post(models.Model):
def __unicode__(self):
return self.title
def font_size(self):
min_font = 12
max_font = 28
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)
tag_count = Post.objects.filter(tags__name=self.name).count()
#print Tag.objects.filter(name=self.name).annotate(c = Count('post'))
font = int(min_font + (tag_count-min_tag)*step)
return font
Tag.f = font_size

View File

@ -136,16 +136,17 @@
display: inline;
margin-right: 10px;
}
.paginator {
font-size: 20px;
#pagination {
font-size: 16px;
list-style: none;
text-align: center;
//text-align: center;
}
.paginator a {
#pagination a {
display: inline-block;
padding: 2 1; text-decoration:none;
text-align: center;
//text-align: center;
}
div.post { font-size: 16px;}
.post {
box-shadow: 0 0.005em 0.01em 0 rgba(0, 0, 0, 0.12), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
box-sizing: border-box;
@ -153,8 +154,6 @@
color: #1a1a1a;
display: block;
font-family: 'Open Sans'
font-size: 14px;
font-weight: 300;
line-height: 26px;
margin: 0 0 10px 0;

View File

@ -5,7 +5,7 @@
<title>BLOG</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<!--link rel="stylesheet" href="{% static 'css/blog.css' %}"-->
<link rel="stylesheet" href="{% static 'css/new.css' %}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css">
</head>
@ -48,7 +48,7 @@
<h2> Теги </h2>
{% for tag in cloud %}
<a href="{% url 'post_list' tag=tag %}" style="font-size: {{tag.f}}; margin: 2px ">{{ tag.name }}({{ tag.c }})</a>
<a href="{% url 'post_list' tag=tag %}" style="font-size: {{tag.f}}px; margin: 2px ">{{ tag.name }}</a>
{% endfor %}
</div>
@ -57,18 +57,18 @@
</div>
<div id="pagination">
<p>
{% if posts.has_previous %} <a href="?page={{ posts.previous_page_number }}">предыдущая</a>{% endif %}
{% if posts.has_next %}<a href="?page={{ posts.next_page_number }}">следующая</a> {% endif %}
{% if posts.has_previous %} <a href="?page={{ posts.previous_page_number }}{% if search_query != '' %}&search={{ search_query }} {% endif %}"><-предыдущая</a>{% endif %}
{% if posts.has_next %}<a href="?page={{ posts.next_page_number }}{% if search_query != '' %}&search={{ search_query }} {% endif %}">следующая-></a> {% endif %}
</p>
{% if posts.has_other_pages %}
Страницы:
<a href="?page=1">|<<</a>
<a href="?page=1{% if search_query != '' %}&search={{ search_query }} {% endif %}">|<<</a>
{% for page in posts.paginator.num_pages|truncate:posts.number %}
{% if page == posts.number %} {{page}}
{% else %} <a href="?page={{page}}">{{page}}</a> {% endif %} {% endfor %}
<a href="?page={{ posts.paginator.num_pages }}"> >>| </a>
{% else %} <a href="?page={{page}}{% if search_query != '' %}&search={{ search_query }} {% endif %}">{{page}}</a> {% endif %} {% endfor %}
<a href="?page={{ posts.paginator.num_pages }}{% if search_query != '' %}&search={{ search_query }} {% endif %}"> >>| </a>
{% endif %}
{% comment %}
<ul class="menu_top paginator">
{% if posts.has_previous %}
@ -80,7 +80,8 @@
<li> <a href="?page={{ posts.next_page_number }}"> {{ posts.next_page_number }}</a> </li>
<!--li> <a href="?page={{ posts.next_page_number|add:1 }}">{{ posts.next_page_number|add:1 }}</a></li-->
<li> <a href="?page={{ posts.paginator.num_pages }}"> >> </a></li> {% endif %}
{% comment %}
{% endcomment %}
{% comment %}
<!--li> {% for i in posts.paginator.page_range %} </li-->
{% endcomment %}
</ul>

View File

@ -4,13 +4,12 @@ register = template.Library()
def truncate(paginator, number):
if number < 5:
truncated_paginator = xrange(1, number+3)
truncated_paginator = xrange(1, min(paginator+1, number+3))
elif number > 4 and number < (paginator-3):
truncated_paginator = xrange(number-3, number+3)
else:
truncated_paginator = xrange(number-3, paginator)
truncated_paginator = xrange(number-3, paginator+1)
return truncated_paginator
register.filter('truncate', truncate)

View File

@ -20,9 +20,12 @@ def cloud():
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
@ -31,7 +34,9 @@ def post_list(request, page='1', tag=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')
@ -45,7 +50,7 @@ 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, 'cloud': tag_cloud})
return render_to_response('blog/post_list.html', {'posts': posts, 'latest': latest, 'cloud': tag_cloud, 'search_query': search_query})
def post_detail(request, pk):