From 57d4cb4318abe3752f8363579064033a21138567 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 29 Apr 2020 12:45:06 -0400 Subject: [PATCH] Remove duplicated get_config from models and import before usage --- CTFd/models/__init__.py | 43 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/CTFd/models/__init__.py b/CTFd/models/__init__.py index 828ae67..777059b 100644 --- a/CTFd/models/__init__.py +++ b/CTFd/models/__init__.py @@ -1,12 +1,10 @@ import datetime -import six from flask_marshmallow import Marshmallow from flask_sqlalchemy import SQLAlchemy from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import column_property, validates -from CTFd.cache import cache from CTFd.utils.crypto import hash_password from CTFd.utils.humanize.numbers import ordinalize @@ -142,6 +140,8 @@ class Awards(db.Model): @hybrid_property def account_id(self): + from CTFd.utils import get_config + user_mode = get_config("user_mode") if user_mode == "teams": return self.team_id @@ -259,6 +259,8 @@ class Users(db.Model): @hybrid_property def account_id(self): + from CTFd.utils import get_config + user_mode = get_config("user_mode") if user_mode == "teams": return self.team_id @@ -291,6 +293,8 @@ class Users(db.Model): return None def get_solves(self, admin=False): + from CTFd.utils import get_config + solves = Solves.query.filter_by(user_id=self.id) freeze = get_config("freeze") if freeze and admin is False: @@ -299,6 +303,8 @@ class Users(db.Model): return solves.all() def get_fails(self, admin=False): + from CTFd.utils import get_config + fails = Fails.query.filter_by(user_id=self.id) freeze = get_config("freeze") if freeze and admin is False: @@ -307,6 +313,8 @@ class Users(db.Model): return fails.all() def get_awards(self, admin=False): + from CTFd.utils import get_config + awards = Awards.query.filter_by(user_id=self.id) freeze = get_config("freeze") if freeze and admin is False: @@ -432,6 +440,8 @@ class Teams(db.Model): return None def get_solves(self, admin=False): + from CTFd.utils import get_config + member_ids = [member.id for member in self.members] solves = Solves.query.filter(Solves.user_id.in_(member_ids)).order_by( @@ -446,6 +456,8 @@ class Teams(db.Model): return solves.all() def get_fails(self, admin=False): + from CTFd.utils import get_config + member_ids = [member.id for member in self.members] fails = Fails.query.filter(Fails.user_id.in_(member_ids)).order_by( @@ -460,6 +472,8 @@ class Teams(db.Model): return fails.all() def get_awards(self, admin=False): + from CTFd.utils import get_config + member_ids = [member.id for member in self.members] awards = Awards.query.filter(Awards.user_id.in_(member_ids)).order_by( @@ -523,6 +537,8 @@ class Submissions(db.Model): @hybrid_property def account_id(self): + from CTFd.utils import get_config + user_mode = get_config("user_mode") if user_mode == "teams": return self.team_id @@ -531,6 +547,8 @@ class Submissions(db.Model): @hybrid_property def account(self): + from CTFd.utils import get_config + user_mode = get_config("user_mode") if user_mode == "teams": return self.team @@ -600,6 +618,8 @@ class Unlocks(db.Model): @hybrid_property def account_id(self): + from CTFd.utils import get_config + user_mode = get_config("user_mode") if user_mode == "teams": return self.team_id @@ -668,22 +688,3 @@ class Tokens(db.Model): class UserTokens(Tokens): __mapper_args__ = {"polymorphic_identity": "user"} - - -@cache.memoize() -def get_config(key): - """ - This should be a direct clone of its implementation in utils. It is used to avoid a circular import. - """ - config = Configs.query.filter_by(key=key).first() - if config and config.value: - value = config.value - if value and value.isdigit(): - return int(value) - elif value and isinstance(value, six.string_types): - if value.lower() == "true": - return True - elif value.lower() == "false": - return False - else: - return value