mirror of
https://github.com/csd4ni3l/grass_touching_captcha.git
synced 2026-01-01 04:23:45 +01:00
FIx registering not working by now redirecting to register and challenges having a completed attribute
This commit is contained in:
21
main.py
21
main.py
@@ -66,7 +66,7 @@ def generate_challenge_route():
|
||||
username = request.json["username"]
|
||||
|
||||
if not username in challenges:
|
||||
challenges[username] = generate_challenge(username)
|
||||
challenges[username] = {"text": generate_challenge(username), "completed": False}
|
||||
|
||||
return challenges[username]
|
||||
|
||||
@@ -117,8 +117,8 @@ def submit_challenge():
|
||||
if not challenges.get(username):
|
||||
return Response("You havent started a challenge yet.", 400)
|
||||
|
||||
detected_text, text_similarity = check_text_similarity(f"{UPLOAD_DIR}/{image_uuid}.{image_type}", challenges[username])
|
||||
challenges.pop(username)
|
||||
detected_text, text_similarity = check_text_similarity(f"{UPLOAD_DIR}/{image_uuid}.{image_type}", challenges[username]["text"])
|
||||
challenges[username]['completed'] = True
|
||||
|
||||
if not text_similarity >= MINIMUM_OCR_SIMILARITY:
|
||||
return Response(f"The text is incorrect on the image. Similarity: {round(text_similarity * 100, 2)}% Detected Text: {detected_text}")
|
||||
@@ -191,6 +191,15 @@ def register():
|
||||
return render_template("register.jinja2")
|
||||
elif request.method == "POST":
|
||||
username, password = request.form.get("username"), request.form.get("password")
|
||||
|
||||
if not challenges.get(username):
|
||||
return Response("Start and finish a challenge before registering.", 401)
|
||||
|
||||
if not challenges[username]["completed"]:
|
||||
return Response("Finish a challenge before registering.", 401)
|
||||
|
||||
challenges.pop(username)
|
||||
|
||||
cur = get_db().cursor()
|
||||
|
||||
cur.execute("SELECT username FROM Users WHERE username = ?", (username,))
|
||||
@@ -205,11 +214,7 @@ def register():
|
||||
get_db().commit()
|
||||
cur.close()
|
||||
|
||||
user = User()
|
||||
user.id = username
|
||||
flask_login.login_user(user, remember=True)
|
||||
|
||||
return redirect(url_for("application"))
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/uploads/<filename>")
|
||||
def uploads(filename):
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
<div class="d-flex justify-content-center align-items-center" style="height: 75vh;">
|
||||
<div class="bg-white rounded rounded-5 border border-5 border-white p-4">
|
||||
<form action="/register" method="post">
|
||||
<h2>Register</h2>
|
||||
<div class="form-group" style="margin-top: 4%;">
|
||||
<label for="usernameinput">Username</label>
|
||||
@@ -24,7 +25,8 @@
|
||||
|
||||
<small id="proofhelp" class="form-text text-muted">Dont worry! We wont tell your friends.</small>
|
||||
</div>
|
||||
<button id="submit" class="btn btn-primary mx-auto d-block" style="width: 100%; margin-top: 4%;">Submit</button>
|
||||
<button id="submit" type="button" class="btn btn-primary mx-auto d-block" style="width: 100%; margin-top: 4%;">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -34,20 +36,19 @@ function upload_success(image_url) {
|
||||
document.getElementById("submit").innerHTML = "Submit"
|
||||
document.getElementById("submit").type = "submit"
|
||||
|
||||
document.getElementById("grass-touching-form").innerHTML += `<img class="preview-img" src="${image_url}"></img>`;
|
||||
document.getElementById("grass-touching-form").innerHTML += `<img class="preview-img" src="${image_url}">`;
|
||||
}
|
||||
|
||||
function upload_error(error_message) {
|
||||
console.error(error_message)
|
||||
|
||||
file_input = document.getElementById("file_input");
|
||||
file_input.value = ''
|
||||
|
||||
document.getElementById("submit").innerHTML = "Submit"
|
||||
document.getElementById("submit").disabled = false;
|
||||
document.getElementById("grass-touching-form").innerHTML += `<div class="text-danger">${error_message}</div>`;
|
||||
|
||||
file_input = document.getElementById("file_input");
|
||||
file_input.removeEventListener("change", read_file);
|
||||
file_input.addEventListener("change", read_file);
|
||||
}
|
||||
|
||||
function upload_file(file_type, file_content) {
|
||||
@@ -100,6 +101,10 @@ function read_file () {
|
||||
else if (file.type.match("image/png")) {
|
||||
file_type = "png";
|
||||
}
|
||||
else {
|
||||
document.getElementById("grass-touching-form").textContent += `<div class="text-danger">Only JPEG and PNG is supported.</div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
upload_file(file_type, filereader.result);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user