Add User and Team Jinja globals to access User/Team attrs (#1511)

* Adds User and Team globals to Jinja theme files. 
* Closes #1234
This commit is contained in:
Kevin Chung
2020-06-25 12:00:33 -04:00
committed by GitHub
parent 74084d7d9a
commit dea6b122b7
3 changed files with 62 additions and 0 deletions

View File

@@ -18,3 +18,32 @@ TeamAttrs = namedtuple(
"created",
],
)
class _TeamAttrsWrapper:
def __getattr__(self, attr):
from CTFd.utils.user import get_current_team_attrs
attrs = get_current_team_attrs()
return getattr(attrs, attr, None)
@property
def place(self):
from CTFd.utils.user import get_current_team
team = get_current_team()
if team:
return team.place
return None
@property
def score(self):
from CTFd.utils.user import get_current_team
team = get_current_team()
if team:
return team.score
return None
Team = _TeamAttrsWrapper()

View File

@@ -20,3 +20,32 @@ UserAttrs = namedtuple(
"created",
],
)
class _UserAttrsWrapper:
def __getattr__(self, attr):
from CTFd.utils.user import get_current_user_attrs
attrs = get_current_user_attrs()
return getattr(attrs, attr, None)
@property
def place(self):
from CTFd.utils.user import get_current_user
user = get_current_user()
if user:
return user.place
return None
@property
def score(self):
from CTFd.utils.user import get_current_user
user = get_current_user()
if user:
return user.score
return None
User = _UserAttrsWrapper()

View File

@@ -56,6 +56,8 @@ def init_template_globals(app):
from CTFd.constants.config import Configs
from CTFd.constants.plugins import Plugins
from CTFd.constants.sessions import Session
from CTFd.constants.users import User
from CTFd.constants.teams import Team
from CTFd.forms import Forms
from CTFd.utils.config.visibility import (
accounts_visible,
@@ -98,6 +100,8 @@ def init_template_globals(app):
app.jinja_env.globals.update(Plugins=Plugins)
app.jinja_env.globals.update(Session=Session)
app.jinja_env.globals.update(Forms=Forms)
app.jinja_env.globals.update(User=User)
app.jinja_env.globals.update(Team=Team)
def init_logs(app):