Skip to content

Commit f2c1a5c

Browse files
committed
Modify solution_exists to take crackme_id directly
1 parent 226d954 commit f2c1a5c

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

app/controllers/solution.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,14 @@ def upload_solution_get(hexidcrackme):
8484
@limit("20 per day", key_func=lambda: session.get('name'))
8585
def upload_solution_post(hexidcrackme):
8686
"""Handle solution file upload."""
87-
_get_crackme_or_abort(hexidcrackme) # Validate crackme exists
87+
crackme = _get_crackme_or_abort(hexidcrackme)
8888
username = session.get('name')
8989
redirect_url = f'/upload/solution/{hexidcrackme}'
9090

9191
# Check if user already submitted a solution
92-
try:
93-
if solution_exists(username, hexidcrackme):
94-
flash("You've already submitted a solution to this crackme", FLASH_ERROR)
95-
return redirect(redirect_url)
96-
except ErrNoResult:
97-
abort(404)
92+
if solution_exists(username, crackme['_id']):
93+
flash("You've already submitted a solution to this crackme", FLASH_ERROR)
94+
return redirect(redirect_url)
9895

9996
if not verify_recaptcha(request):
10097
flash('reCAPTCHA invalid!', FLASH_ERROR)
@@ -198,17 +195,14 @@ def editor_solution_get(hexidcrackme):
198195
@limit("20 per day", key_func=lambda: session.get('name'))
199196
def editor_solution_post(hexidcrackme):
200197
"""Handle solution submission from the web editor."""
201-
_get_crackme_or_abort(hexidcrackme) # Validate crackme exists
198+
crackme = _get_crackme_or_abort(hexidcrackme)
202199
username = session.get('name')
203200
redirect_url = f'/upload/solution/{hexidcrackme}/editor'
204201

205202
# Check if user already submitted a solution
206-
try:
207-
if solution_exists(username, hexidcrackme):
208-
flash("You've already submitted a solution to this crackme", FLASH_ERROR)
209-
return redirect(redirect_url)
210-
except ErrNoResult:
211-
abort(404)
203+
if solution_exists(username, crackme['_id']):
204+
flash("You've already submitted a solution to this crackme", FLASH_ERROR)
205+
return redirect(redirect_url)
212206

213207
if not verify_recaptcha(request):
214208
flash('reCAPTCHA invalid!', FLASH_ERROR)

app/models/solution.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,22 @@ def solutions_by_user(username):
7272
return solutions
7373

7474

75-
def solution_exists(username, crackme_hexid):
75+
def solution_exists(username, crackme_id):
7676
"""Check if a user has already submitted a solution for a crackme.
7777
78-
Raises:
79-
ErrNoResult: If the crackme doesn't exist
80-
ErrUnavailable: If the database is unavailable
78+
Args:
79+
username: The username to check
80+
crackme_id: The crackme's ObjectId (not hexid)
81+
82+
Returns:
83+
True if a solution exists, False otherwise
8184
"""
8285
if not check_connection():
8386
raise ErrUnavailable("Database is unavailable")
8487

85-
crackme_collection = get_collection('crackme')
86-
crackme = crackme_collection.find_one({'hexid': crackme_hexid}, {'_id': 1})
87-
if not crackme:
88-
raise ErrNoResult("Crackme not found")
89-
9088
collection = get_collection('solution')
9189
return collection.find_one(
92-
{'crackmeid': crackme['_id'], 'author': username},
90+
{'crackmeid': crackme_id, 'author': username},
9391
{'_id': 1}
9492
) is not None
9593

0 commit comments

Comments
 (0)