mirror of
https://github.com/aljazceru/CTFd.git
synced 2026-02-09 08:14:22 +01:00
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:
@@ -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,
|
||||
)
|
||||
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
5
CTFd/utils/humanize/words.py
Normal file
5
CTFd/utils/humanize/words.py
Normal file
@@ -0,0 +1,5 @@
|
||||
def pluralize(number, singular="", plural="s"):
|
||||
if number == 1:
|
||||
return singular
|
||||
else:
|
||||
return plural
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user