gergelypolonkai-web-django/gergelypolonkaiweb/helper.py

53 lines
1.4 KiB
Python
Raw Permalink Normal View History

2013-10-01 20:56:20 +00:00
from django.db.models import Count
from taggit.models import Tag
from math import floor
from operator import itemgetter
2013-10-01 21:39:27 +00:00
from django.conf import settings
import os
from random import choice
def randomheader(request):
2014-04-21 08:39:43 +00:00
header_file = choice(filter(
lambda x: os.path.isfile(
settings.HEADER_DIR + os.path.sep + x
),
os.listdir(settings.HEADER_DIR)
))
2013-10-01 21:39:27 +00:00
return {'header': header_file}
2013-10-01 20:56:20 +00:00
def tagcloud(request):
tagcloud = []
2014-04-21 08:39:43 +00:00
tagcloudlist = Tag.objects.annotate(
ct = Count('taggit_taggeditem_items')
).order_by('-ct')
2013-10-01 20:56:20 +00:00
if (len(tagcloudlist) > 0):
tmax = tagcloudlist[0].ct
tmin = 1
if (tmax == tmin):
tmax += 1
2013-10-01 20:56:20 +00:00
for cloudelement in tagcloudlist:
tagcount = cloudelement.ct
2013-10-01 20:56:20 +00:00
if (tagcount >= tmin):
size = int(floor((5.0 * (tagcount - tmin)) / (tmax - tmin)))
2014-04-21 08:39:43 +00:00
tagcloud.append({
'name': cloudelement.name,
'slug': cloudelement.slug,
'size': size
})
2013-10-01 20:56:20 +00:00
2014-04-21 08:39:43 +00:00
tagcloud.sort(key = itemgetter('name'))
2013-10-01 20:56:20 +00:00
return {'tagcloud': tagcloud}
2013-10-15 07:29:03 +00:00
def viewname(request):
if hasattr(request, 'view_name'):
return {
'view_name': request.view_name,
}
else:
return {}