landing.py
63 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
neko259
|
r1740 | from datetime import datetime | ||
from datetime import timedelta | ||||
neko259
|
r1905 | from django.db.models import Count, Q | ||
neko259
|
r1732 | from django.shortcuts import render | ||
from django.utils.decorators import method_decorator | ||||
from django.views.decorators.csrf import csrf_protect | ||||
neko259
|
r1740 | from boards import settings | ||
neko259
|
r2004 | from boards.models import Post, Tag, STATUS_ACTIVE, TagAlias | ||
from boards.settings import SECTION_VIEW | ||||
neko259
|
r1732 | from boards.views.base import BaseBoardView | ||
neko259
|
r2125 | from boards.abstracts.settingsmanager import get_settings_manager,\ | ||
SETTING_ONLY_FAVORITES | ||||
neko259
|
r1732 | |||
PARAM_SECTION_STR = 'section_str' | ||||
PARAM_LATEST_THREADS = 'latest_threads' | ||||
neko259
|
r2092 | PARAM_LATEST_POSTS = 'latest_posts' | ||
neko259
|
r1732 | |||
TEMPLATE = 'boards/landing.html' | ||||
class LandingView(BaseBoardView): | ||||
@method_decorator(csrf_protect) | ||||
def get(self, request): | ||||
params = dict() | ||||
params[PARAM_SECTION_STR] = Tag.objects.get_tag_url_list( | ||||
neko259
|
r1905 | Tag.objects.filter(Q(aliases__in=TagAlias.objects.filter_localized()) & Q(required=True)) | ||
.order_by('aliases__name')) | ||||
neko259
|
r1740 | |||
today = datetime.now() - timedelta(1) | ||||
neko259
|
r2125 | ops = Post.objects.filter(thread__replies__pub_time__gt=today, opening=True, thread__status=STATUS_ACTIVE) | ||
fav_tags = self._get_fav_tags(request) | ||||
if len(fav_tags) > 0: | ||||
ops = ops.filter(thread__tags__in=fav_tags) | ||||
ops = ops.annotate(today_post_count=Count('thread__replies'))\ | ||||
.order_by('-pub_time') | ||||
neko259
|
r1843 | |||
neko259
|
r2004 | max_landing_threads = settings.get_int(SECTION_VIEW, 'MaxLandingThreads') | ||
neko259
|
r1843 | if max_landing_threads > 0: | ||
ops = ops[:max_landing_threads] | ||||
neko259
|
r1740 | params[PARAM_LATEST_THREADS] = ops | ||
neko259
|
r2092 | max_landing_posts = settings.get_int(SECTION_VIEW, 'MaxLandingPosts') | ||
neko259
|
r2125 | latest_posts = Post.objects.filter(pub_time__gt=today) | ||
if len(fav_tags) > 0: | ||||
latest_posts = latest_posts.filter(thread__tags__in=fav_tags) | ||||
params[PARAM_LATEST_POSTS] = latest_posts\ | ||||
neko259
|
r2092 | .order_by('-pub_time')[:max_landing_posts] | ||
neko259
|
r1732 | return render(request, TEMPLATE, params) | ||
neko259
|
r2125 | def _get_fav_tags(self, request): | ||
fav_tags = [] | ||||
settings_manager = get_settings_manager(request) | ||||
if settings_manager.get_setting(SETTING_ONLY_FAVORITES): | ||||
fav_tags = settings_manager.get_fav_tags() | ||||
return fav_tags | ||||