from django.conf import settings as config from django.urls import reverse from .community import MENU from .fans import unread_count from .backgrounds import SAFE_ROUTES from .models import FloatingArtwork, PageBackground, TitleFollow, ManagedPage from .newsletter import SignupForm, signup_open from .views import settings_row from .theme import HEX_COLOR, css_variables def site_context(request): if request.resolver_match and request.resolver_match.namespace == 'admin': return {} match = request.resolver_match url_name = match.url_name if match else None active_nav = { 'chapters': 'read', 'chapter': 'read', 'chapter-discussion': 'read', 'slx-series-reviews': 'reviews', 'slx-series-review': 'reviews', 'slx-series-review-write': 'reviews', 'slx-reviews': 'reviews', 'slx-review': 'reviews', 'slx-review-write': 'reviews', 'blog': 'blog', 'blog-post': 'blog', 'upcoming': 'upcoming', 'stream': 'live', 'slx-community': 'community', 'books': 'books', 'search': 'search', 'library': 'library', 'fan-notices': 'notices', 'fan-login': 'account', 'fan-signup': 'account', }.get(url_name) if url_name in ('entry', 'entries'): active_nav = match.kwargs.get('kind') settings = settings_row() context = {'settings': settings, 'site_theme_vars': css_variables(settings.site_palette), 'active_nav': active_nav, 'fan_unread': 0, 'following_title': False} if match and not match.namespace and url_name in SAFE_ROUTES and len(request.path_info) <= 200: backdrop = PageBackground.objects.filter(path=request.path_info, enabled=True).first() if backdrop and HEX_COLOR.fullmatch(backdrop.color): context['page_backdrop'] = backdrop context['page_tint'] = min(100, backdrop.tint_strength) art = FloatingArtwork.objects.filter(path=request.path_info, enabled=True) bands = {} for item in art: if item.art and item.placement in FloatingArtwork.Placement.values and item.alignment in FloatingArtwork.Alignment.values: bands.setdefault(item.placement, {}).setdefault(item.alignment, []).append(item) context['floating_top_art'] = bands.get('top') context['floating_bottom_art'] = bands.get('bottom') context['newsletter_signup_open'] = signup_open() if (context['newsletter_signup_open'] and config.SLX_NEWSLETTER_POPUP_ENABLED and url_name in SAFE_ROUTES and url_name not in {'newsletter-signup', 'coming-soon'}): context['newsletter_popup_form'] = SignupForm(auto_id='popup_%s') hidden = settings.hidden_menu if isinstance(settings.hidden_menu, list) else [] links = [] for key, label, route in MENU: if key in hidden: continue if key in ('library', 'notices') and not request.user.is_authenticated: continue if key == 'account' and request.user.is_authenticated: continue url = (settings.shop_url or reverse('shop-index')) if key == 'shop' else reverse(route, args=[key] if key in ('cast', 'world') else []) links.append({'key': key, 'label': label, 'url': url, 'active': key == active_nav}) custom = list(ManagedPage.objects.filter(published=True, show_in_menu=True).order_by('pk')) replacements = {page.menu_key: page for page in custom if page.menu_key} for link in links: page = replacements.pop(link['key'], None) if page: link.update(label=page.title, url=page.get_absolute_url(), active=request.path == page.get_absolute_url()) for page in custom: if not page.menu_key: links.append({'key': page.slug, 'label': page.title, 'url': page.get_absolute_url(), 'active': request.path == page.get_absolute_url()}) context['public_menu'] = links if request.user.is_authenticated: follow = TitleFollow.objects.filter(user=request.user).first() context.update(fan_unread=unread_count(request.user, follow), following_title=bool(follow)) return context