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.
 
 
 
 
 
 

49 lines
1.6 KiB

from django.utils import translation
from django.core.cache import cache
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
class ArticleManager(TranslationManager):
cache_time = 45
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
def news(self):
"""
return queryset of news
"""
model = self.model
return self.language().filter(type=model.news)
def blogs(self):
"""
return queryset of blogs
"""
model = self.model
return self.language().filter(type=model.blog)
def main_page_news(self, cached=True):
lang = translation.get_language()
key = 'main_page_news_%s' % lang
if cached and key in cache:
cached_news = cache.get(key)
return cached_news
else:
news = list(self.news().filter(publish_date__isnull=False).order_by('-main_page', '-publish_date', '-modified')[:3])
cache.set(key, news, self.cache_time)
return news
def main_page_blogs(self, cached=True):
lang = translation.get_language()
key = 'main_page_blogs_%s' % lang
if cached and key in cache:
cached_blogs = cache.get(key)
return cached_blogs
else:
blogs = list(self.blogs().filter(publish_date__isnull=False).order_by('-main_page', '-publish_date')[:3])
cache.set(key, blogs, self.cache_time)
return blogs