def common_pieces(request): now = datetime.now() if request.is_ajax(): base_template = 'base_ajax.html' else: base_template = 'base.html' rubrics = cached(lambda: list(Rubric.objects.all()), 'rubrics', duration=60*60*24) featured = cached(lambda: list(FeaturedItems.objects.filter(is_active=True)), 'featured', duration=60*60*24) try: quote = cached(lambda: DailyQuote.objects.filter(day=now.date()), 'quote', duration=60*60)[0] quote.recache() except IndexError: quote = None news = cached(lambda: map(news_stripper, ContentItem.objects.filter(enabled=True, pub_date__lte=now, rubric__title=u'Новости')[0:10]), 'news2', duration=60*60*24) current_items = cached( lambda: ContentItem.batched.batch_select('authors').select_related().exclude(rubric = 1).filter(enabled=True, published=False)[0:12], 'red_string', duration=60*60*24 ) return { 'news': news, 'base_template': base_template, 'rubrics': rubrics, 'featured': featured, 'quote': quote, 'current_items': current_items }
def get(key, default=None): def dictify(qs): res = {} for o in qs: res[o.key] = o.get_value() return res config = cached(lambda: dictify(Entry.objects.all()), 'config-settings') return config.get(key, default)
def display_last_poll(context): poll = cached( lambda: Poll.calculate(Poll.objects.filter(status=1).annotate(total_votes=Count('vote')).latest('creation_date')), 'latest_poll_object', duration=60 ) try: session = context['request'].session user_has_vote = poll.pk in session.get('poll', []) except KeyError: user_has_vote = False return {'poll': poll, 'user_has_vote': user_has_vote}
def get_source(self): return cached(lambda: ContentItem.batched.batch_select('authors').get(pk=self.source_id), 'quote-source', duration=60*60)
def home(request): no_cache = False now = datetime.now().date() wstart = now - oneday*(now.weekday()+5) if now.weekday() >= 2: wstart += 7*oneday wend = wstart + 7*oneday if request.user.is_authenticated() and request.user.is_staff and request.GET.get('next_number'): wstart += 7*oneday wend += 7*oneday no_cache = True num = 1 + (wstart - datetime(year=wstart.year,day=1,month=1).date()).days / 7 def get_illustration(): try: zh = ZhivotovIllustration.objects.filter(pub_date__lte = wend).latest('pub_date') except ZhivotovIllustration.DoesNotExist: zh = None return zh def get_content(): qs = ContentItem.batched.batch_select('authors').select_related('rubric').filter( enabled=True, rubric__on_main=True, pub_date__gte = wstart, pub_date__lt = wend, published=True ) newsletter = {} for item in list(qs): newsletter.setdefault(item.rubric_id, {'rubric': None, 'items': []}) if newsletter[item.rubric_id]['rubric'] is None: newsletter[item.rubric_id]['rubric'] = item.rubric newsletter[item.rubric_id]['items'].append(item) return sorted(newsletter.values(), key=lambda p: p['rubric'].position) if not no_cache: newsletter = cached( get_content, 'newsletter', duration=60*60*4 ) else: newsletter = get_content() def get_latest_rubric(rubric, kind='video'): try: return ContentItem.objects.filter(kind=kind, enabled=True, rubric=rubric).latest('pub_date') except ContentItem.DoesNotExist: return None if not no_cache: illustration = cached( get_illustration, 'illustration', duration=6000 ) else: illustration = get_illustration() zavtra_tv = cached( lambda: ContentItem.objects.filter(kind='video', enabled=True, rubric=19 if settings.DEBUG else 44)[0:1], 'zavtra-tv2', duration=60*60*4 ) special_project = cached( lambda: get_latest_rubric(47), 'special-project', duration=60*60*4 ) return { 'issue_info': { 'date': wstart, 'num': num }, 'newsletter': newsletter, 'illustration': illustration, 'zavtra_tv': zavtra_tv, 'special_project': special_project }
def get_tree(): def _get_tree(): root = Category.objects.root_nodes()[0] return root.get_descendants(include_self=True) return cached(_get_tree, 'categories')