1 from collections
import defaultdict
, namedtuple
, Counter
2 from operator
import itemgetter
3 from datetime
import datetime
4 from random
import choice
5 from itertools
import chain
7 from django
.utils
.text
import slugify
9 from mygpo
.decorators
import query_if_required
, repeat_on_conflict
10 from mygpo
.categories
.models
import Category
, CategoryEntry
15 def __init__(self
, total
=100, num_cat
=10, podcasts_per_cat
=10):
17 self
.num_cat
= num_cat
18 self
.podcasts_per_cat
= podcasts_per_cat
19 self
._categories
= None
22 def _needs_query(self
):
23 return self
._categories
is None
26 categories
= list(Category
.objects
.filter(num_entries__gt
=0)
27 .order_by('-modified')[:self
.total
])
28 self
._categories
= categories
[:self
.num_cat
]
29 self
._tagcloud
= sorted(categories
[self
.num_cat
:],
30 key
=lambda x
: x
.title
.lower())
38 def max_entries(self
):
39 return max([e
.num_entries
for e
in self
.tagcloud
] + [0])
42 def min_entries(self
):
43 return min([e
.num_entries
for e
in self
.tagcloud
] + [0])
48 return self
._categories
52 def update_category(podcast
):
53 all_tags
= list(t
.tag
for t
in podcast
.tags
.all())
58 random_tag
= choice(all_tags
)
60 category
, created
= Category
.objects
.get_or_create(
61 tags__tag
=slugify(random_tag
.strip()),
68 # update modified timestamp
71 # add podcast to the category as newest entry
72 entry
, created
= CategoryEntry
.objects
.get_or_create(
78 # update modified timestamp