UI updates and caching fixes on submission
This commit is contained in:
35
__init__.py
35
__init__.py
@@ -7,6 +7,7 @@ from CTFd.utils.decorators.visibility import check_challenge_visibility
|
||||
from CTFd.plugins.flags import get_flag_class
|
||||
from CTFd.plugins.challenges import get_chal_class
|
||||
from CTFd.utils import get_config, set_config
|
||||
from CTFd.cache import clear_challenges, clear_standings
|
||||
|
||||
def load(app):
|
||||
plugin_bp = Blueprint('category_submission', __name__,
|
||||
@@ -51,12 +52,12 @@ def load(app):
|
||||
if category not in enabled_cats:
|
||||
return jsonify({'success': False, 'message': 'Invalid category'})
|
||||
|
||||
# Rate Limiting
|
||||
# 1. Rate Limiting
|
||||
last_sub = Submissions.query.filter_by(user_id=user.id).order_by(Submissions.date.desc()).first()
|
||||
if last_sub and (time.time() - last_sub.date.timestamp() < cooldown):
|
||||
return jsonify({'success': False, 'message': f'Wait {cooldown}s'})
|
||||
|
||||
# Search unsolved challenges in this category
|
||||
# 2. Search unsolved challenges in this category
|
||||
solve_filter = (Solves.team_id == team.id) if team else (Solves.user_id == user.id)
|
||||
challenges = Challenges.query.filter(
|
||||
Challenges.category == category,
|
||||
@@ -65,25 +66,35 @@ def load(app):
|
||||
).all()
|
||||
|
||||
for chall in challenges:
|
||||
# 1. Manual check (no DB write)
|
||||
flags = Flags.query.filter_by(challenge_id=chall.id).all()
|
||||
for flag in flags:
|
||||
try:
|
||||
# Perform comparison in-memory to avoid cluttering logs with incorrects
|
||||
if get_flag_class(flag.type).compare(flag, provided):
|
||||
# 2. Match found! Now use built-in CTFd methods.
|
||||
# 3. Match Found: Log the correct submission
|
||||
sub = Submissions(
|
||||
user_id=user.id, team_id=team.id if team else None,
|
||||
challenge_id=chall.id, ip=request.remote_addr, provided=provided, type='correct'
|
||||
)
|
||||
db.session.add(sub)
|
||||
|
||||
# 4. Native Solve Logic (Handles points and Solves table)
|
||||
chal_class = get_chal_class(chall.type)
|
||||
|
||||
# attempt() creates the 'correct' Submission record
|
||||
chal_class.attempt(chall, request)
|
||||
|
||||
# solve() creates the Solve record and awards points
|
||||
chal_class.solve(user=user, team=team, challenge=chall, request=request)
|
||||
|
||||
# 5. Clear Caches so UI reflects changes immediately
|
||||
clear_challenges()
|
||||
clear_standings()
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'message': f'Correct! Solved: {chall.name}'})
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'Correct: {chall.name}',
|
||||
'challenge_id': chall.id
|
||||
})
|
||||
except Exception: continue
|
||||
|
||||
# 3. No match: Log ONE incorrect attempt globally
|
||||
# 6. No Match: Log exactly one incorrect attempt globally
|
||||
db.session.add(Submissions(
|
||||
user_id=user.id, team_id=team.id if team else None,
|
||||
challenge_id=None, ip=request.remote_addr, provided=provided, type='incorrect'
|
||||
@@ -93,4 +104,4 @@ def load(app):
|
||||
|
||||
app.register_blueprint(plugin_bp)
|
||||
from CTFd.utils.plugins import register_script
|
||||
register_script('/assets/category_submit.js')
|
||||
register_script('/assets/category_submit.js')
|
||||
Reference in New Issue
Block a user