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
.core
.proxy
import proxy_object
11 from mygpo
.categories
.models
import Category
, CategoryEntry
16 def __init__(self
, total
=100, num_cat
=10, podcasts_per_cat
=10):
18 self
.num_cat
= num_cat
19 self
.podcasts_per_cat
= podcasts_per_cat
20 self
._categories
= None
23 def _needs_query(self
):
24 return self
._categories
is None
27 categories
= list(Category
.objects
.filter(num_entries__gt
=0)
28 .order_by('-modified')[:self
.total
])
29 self
._categories
= categories
[:self
.num_cat
]
30 self
._tagcloud
= sorted(categories
[self
.num_cat
:],
31 key
=lambda x
: x
.title
.lower())
39 def max_entries(self
):
40 return max([e
.num_entries
for e
in self
.tagcloud
] + [0])
43 def min_entries(self
):
44 return min([e
.num_entries
for e
in self
.tagcloud
] + [0])
49 return self
._categories
53 def update_category(podcast
):
54 all_tags
= list(chain
.from_iterable(s
for s
in podcast
.tags
.values()))
59 random_tag
= choice(all_tags
)
61 category
, created
= Category
.objects
.get_or_create(
62 tags__tag
=slugify(random_tag
.strip()),
69 # update modified timestamp
72 # add podcast to the category as newest entry
73 entry
, created
= CategoryEntry
.objects
.get_or_create(
79 # update modified timestamp