Add current attempts and max attempts to challenge view (#1507)

* Display current attempts in challenge view when max attempts is enabled
* Closes #1477
This commit is contained in:
Kevin Chung
2020-06-23 12:21:18 -04:00
committed by GitHub
parent 84c02b11af
commit 412692d49a
4 changed files with 39 additions and 1 deletions

View File

@@ -6,7 +6,17 @@ from sqlalchemy.sql import and_
from CTFd.cache import clear_standings
from CTFd.models import ChallengeFiles as ChallengeFilesModel
from CTFd.models import Challenges, Fails, Flags, Hints, HintUnlocks, Solves, Tags, db
from CTFd.models import (
Challenges,
Fails,
Flags,
Hints,
HintUnlocks,
Solves,
Submissions,
Tags,
db,
)
from CTFd.plugins.challenges import CHALLENGE_CLASSES, get_chal_class
from CTFd.schemas.flags import FlagSchema
from CTFd.schemas.hints import HintSchema
@@ -273,6 +283,15 @@ class Challenge(Resource):
else:
response["solves"] = None
if authed():
# Get current attempts for the user
attempts = Submissions.query.filter_by(
account_id=user.account_id, challenge_id=challenge_id
).count()
else:
attempts = 0
response["attempts"] = attempts
response["files"] = files
response["tags"] = tags
response["hints"] = hints
@@ -283,6 +302,8 @@ class Challenge(Resource):
files=files,
tags=tags,
hints=[Hints(**h) for h in hints],
max_attempts=chal.max_attempts,
attempts=attempts,
challenge=chal,
)

View File

@@ -83,6 +83,16 @@
{% endfor %}
</div>
{% if max_attempts > 1 %}
<div class="row text-center">
<div class="col-md-12">
<p>
{{ attempts }}/{{ max_attempts }} attempt{{ attempts|pluralize(attempts) }}
</p>
</div>
</div>
{% endif %}
<div class="row submit-row">
<div class="col-md-9 form-group">
{% block input %}

View File

@@ -0,0 +1,5 @@
def pluralize(number, singular="", plural="s"):
if number == 1:
return singular
else:
return plural

View File

@@ -23,6 +23,7 @@ from CTFd.utils.config.pages import get_pages
from CTFd.utils.countries import get_countries, lookup_country_code
from CTFd.utils.dates import isoformat, unix_time, unix_time_millis
from CTFd.utils.events import EventManager, RedisEventManager
from CTFd.utils.humanize.words import pluralize
from CTFd.utils.modes import generate_account_url, get_mode_as_word
from CTFd.utils.plugins import (
get_configurable_plugins,
@@ -48,6 +49,7 @@ def init_template_filters(app):
app.jinja_env.filters["unix_time"] = unix_time
app.jinja_env.filters["unix_time_millis"] = unix_time_millis
app.jinja_env.filters["isoformat"] = isoformat
app.jinja_env.filters["pluralize"] = pluralize
def init_template_globals(app):