NDOJ/judge/template_context.py

129 lines
3.8 KiB
Python
Raw Normal View History

2024-04-23 20:36:51 +00:00
import re
2020-01-21 06:35:58 +00:00
from functools import partial
from django.conf import settings
from django.contrib.auth.context_processors import PermWrapper
from django.contrib.sites.shortcuts import get_current_site
from django.core.cache import cache
from django.utils.functional import SimpleLazyObject, new_method_proxy
from .models import MiscConfig, NavigationBar, Profile
2024-04-23 20:36:51 +00:00
from judge.caching import cache_wrapper
2020-01-21 06:35:58 +00:00
class FixedSimpleLazyObject(SimpleLazyObject):
2022-05-14 17:57:27 +00:00
if not hasattr(SimpleLazyObject, "__iter__"):
2020-01-21 06:35:58 +00:00
__iter__ = new_method_proxy(iter)
def get_resource(request):
use_https = settings.DMOJ_SSL
if use_https == 1:
2022-05-14 17:57:27 +00:00
scheme = "https" if request.is_secure() else "http"
2020-01-21 06:35:58 +00:00
elif use_https > 1:
2022-05-14 17:57:27 +00:00
scheme = "https"
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
scheme = "http"
2022-12-18 09:31:31 +00:00
2020-01-21 06:35:58 +00:00
return {
2022-05-14 17:57:27 +00:00
"INLINE_JQUERY": settings.INLINE_JQUERY,
"INLINE_FONTAWESOME": settings.INLINE_FONTAWESOME,
"JQUERY_JS": settings.JQUERY_JS,
"FONTAWESOME_CSS": settings.FONTAWESOME_CSS,
"DMOJ_SCHEME": scheme,
"DMOJ_CANONICAL": settings.DMOJ_CANONICAL,
2022-12-18 09:31:31 +00:00
"use_darkmode": request.session.get("darkmode", False) == True,
2020-01-21 06:35:58 +00:00
}
def get_profile(request):
if request.user.is_authenticated:
return Profile.objects.get_or_create(user=request.user)[0]
return None
def comet_location(request):
if request.is_secure():
2022-05-14 17:57:27 +00:00
websocket = getattr(settings, "EVENT_DAEMON_GET_SSL", settings.EVENT_DAEMON_GET)
poll = getattr(settings, "EVENT_DAEMON_POLL_SSL", settings.EVENT_DAEMON_POLL)
2020-01-21 06:35:58 +00:00
else:
websocket = settings.EVENT_DAEMON_GET
poll = settings.EVENT_DAEMON_POLL
2022-05-14 17:57:27 +00:00
return {"EVENT_DAEMON_LOCATION": websocket, "EVENT_DAEMON_POLL_LOCATION": poll}
2020-01-21 06:35:58 +00:00
2024-04-23 20:36:51 +00:00
@cache_wrapper(prefix="nb")
def _nav_bar():
return NavigationBar.objects.all()
2020-01-21 06:35:58 +00:00
def __nav_tab(path):
2024-04-23 20:36:51 +00:00
nav_bar_list = list(_nav_bar())
nav_bar_dict = {nb.id: nb for nb in nav_bar_list}
result = next((nb for nb in nav_bar_list if re.match(nb.regex, path)), None)
if result:
while result.parent_id:
result = nav_bar_dict.get(result.parent_id)
return result.key
else:
return []
2020-01-21 06:35:58 +00:00
def general_info(request):
path = request.get_full_path()
return {
2022-05-14 17:57:27 +00:00
"nav_tab": FixedSimpleLazyObject(partial(__nav_tab, request.path)),
2024-04-23 20:36:51 +00:00
"nav_bar": _nav_bar(),
2022-05-14 17:57:27 +00:00
"LOGIN_RETURN_PATH": "" if path.startswith("/accounts/") else path,
"perms": PermWrapper(request.user),
2020-01-21 06:35:58 +00:00
}
def site(request):
2022-05-14 17:57:27 +00:00
return {"site": get_current_site(request)}
2020-01-21 06:35:58 +00:00
class MiscConfigDict(dict):
2022-05-14 17:57:27 +00:00
__slots__ = ("language", "site")
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
def __init__(self, language="", domain=None):
2020-01-21 06:35:58 +00:00
self.language = language
self.site = domain
super(MiscConfigDict, self).__init__()
def __missing__(self, key):
2022-05-14 17:57:27 +00:00
cache_key = "misc_config:%s:%s:%s" % (self.site, self.language, key)
2020-01-21 06:35:58 +00:00
value = cache.get(cache_key)
if value is None:
2022-05-14 17:57:27 +00:00
keys = ["%s.%s" % (key, self.language), key] if self.language else [key]
2020-01-21 06:35:58 +00:00
if self.site is not None:
2022-05-14 17:57:27 +00:00
keys = ["%s:%s" % (self.site, key) for key in keys] + keys
map = dict(
MiscConfig.objects.values_list("key", "value").filter(key__in=keys)
)
2020-01-21 06:35:58 +00:00
for item in keys:
if item in map:
value = map[item]
break
else:
2022-05-14 17:57:27 +00:00
value = ""
2020-01-21 06:35:58 +00:00
cache.set(cache_key, value, 86400)
self[key] = value
return value
def misc_config(request):
domain = get_current_site(request).domain
2022-05-14 17:57:27 +00:00
return {
"misc_config": MiscConfigDict(domain=domain),
"i18n_config": MiscConfigDict(language=request.LANGUAGE_CODE, domain=domain),
}
2020-01-21 06:35:58 +00:00
def site_name(request):
2022-05-14 17:57:27 +00:00
return {
"SITE_NAME": settings.SITE_NAME,
"SITE_LONG_NAME": settings.SITE_LONG_NAME,
"SITE_ADMIN_EMAIL": settings.SITE_ADMIN_EMAIL,
}