You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
884 B
32 lines
884 B
# -*- coding: utf-8 -*
|
|
from random import choice, shuffle
|
|
from django.db import models
|
|
from django.core.cache import cache
|
|
|
|
|
|
class BiasedManager(models.Manager):
|
|
def by_time(self, **kwargs):
|
|
all = super(BiasedManager, self).get_query_set().filter(**kwargs)
|
|
result = []
|
|
for i in all:
|
|
for j in range(i.often):
|
|
result.append(i)
|
|
return result
|
|
|
|
def one(self, **kwargs):
|
|
return choice(self.by_time(**kwargs))
|
|
|
|
def by_often(self, **kwargs):
|
|
result = self.by_time(**kwargs)
|
|
shuffle(result)
|
|
return result
|
|
|
|
class BannerGroupCached(models.Manager):
|
|
def all(self):
|
|
key = 'banner_group_all'
|
|
result = cache.get(key)
|
|
if not result:
|
|
result = list(self.filter())
|
|
cache.set(key, result, 90)
|
|
return result
|
|
|
|
|