django_blog/blog/models.py
2017-12-08 01:29:07 +03:00

42 lines
1.2 KiB
Python

# -*- coding: utf-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')
title = models.CharField(max_length=200)
text = HTMLField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
tags = TaggableManager(blank=True)
def publish(self):
self.published_date = timezone.now()
self.save()
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"]
try: step = (max_font - min_font)/float(max_tag-min_tag)
except: step = 1
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