Remove duplicated get_config from models and import before usage

remove-get-config-from-models
Kevin Chung 2020-04-29 12:45:06 -04:00
parent e15e23f038
commit 57d4cb4318
1 changed files with 22 additions and 21 deletions

View File

@ -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