diff --git a/app/controllers/challenge.py b/app/controllers/challenge.py index c67e35d..00edf82 100644 --- a/app/controllers/challenge.py +++ b/app/controllers/challenge.py @@ -131,14 +131,15 @@ def main(challenge_id_untrusted): raise ValueError challenge_data = common_helpers.get_challenge(challenge_id) + # If challenge not found, quietly redirect to homepage if not challenge_data: -# flash("Tätä haastetta ei löydy.", "info") return {"redirect": True, "url": "/"} - # If challenge is still a draft, redirect to homepage + # If challenge is still a draft, quietly redirect to homepage if challenge_data["status"] == "draft": -# flash("Tämä haaste ei ole juuri nyt avoinna.", "info") return {"redirect": True, "url": "/"} + + # TODO: If challenge is not open anymore # Participation data # Logged in user diff --git a/app/controllers/participation.py b/app/controllers/participation.py index 98f7f73..41be3b7 100644 --- a/app/controllers/participation.py +++ b/app/controllers/participation.py @@ -248,6 +248,10 @@ def validate_participation_data(form_data): else: form_data["place"] = "" + # 0) Add trashed if not present + if "trashed" not in form_data: + form_data["trashed"] = False + # Handle taxon data # 1) Extract taxon data from form data taxa_data = taxa_to_dict(form_data) @@ -261,13 +265,23 @@ def validate_participation_data(form_data): if not common_helpers.is_yyyy_mm_dd(value): del taxa_data[key] - # 4) Add trashed if not present - if "trashed" not in form_data: - form_data["trashed"] = False - - # 5) Calculate number of species + # 4) Calculate number of species form_data["taxa_count"] = len(taxa_data) + # 5) Compare to date_fields_count, to see if all fields were received from the browser + # Do this only if user is not trashing / has not trashed the participation + if form_data["trashed"] == True or form_data["trashed"] == 1 or form_data["trashed"] == "1": + print("Trashed, skipping date_fields_count check.") + else: + if "date_fields_count" in form_data: +# print("Taxa count: ", form_data["taxa_count"]) +# print("Date field count: ", form_data["date_fields_count"]) + + if form_data["taxa_count"] != int(form_data["date_fields_count"]): + raise Exception(f"Received incomplete data from browser (expected { form_data['date_fields_count'] } taxa, received { form_data['taxa_count'] }).") + else: + raise Exception("Received incomplete data from browser (date_fields_count missing).") + # 6) Convert to JSON string (for database JSON field) where dates are YYYY-MM-DD # Note: form_data still has the original taxon data form_data["taxa_json"] = json.dumps(taxa_data) @@ -383,10 +397,6 @@ def main(challenge_id_untrusted, participation_id_untrusted, form_data = None): # Todo: Maybe instead remove only empty taxon fields here? form_data = {key: value for key, value in form_data.items(multi=True) if value} - # First check that form is fully received by checking that field form_completed has value "ready" - if "form_completed" not in form_data or form_data["form_completed"] != "ready": - raise Exception("Received incomplete data from browser.") - errors, form_data = validate_participation_data(form_data) # Case C1: Errors found. Show the form again with error messages. diff --git a/app/templates/form_challenge100.html b/app/templates/form_challenge100.html index bff9e56..89906fc 100644 --- a/app/templates/form_challenge100.html +++ b/app/templates/form_challenge100.html @@ -152,6 +152,28 @@ .then(data => speciesData = data); document.addEventListener('DOMContentLoaded', function() { + + // Count filled in date field before submission + // Todo: Change form name to participation. Side-effects? + document.getElementById('challenge').addEventListener('submit', function(event) { + + // Select all date inputs with the class 'species_date' + var dateFields = document.querySelectorAll('#challenge input[type="date"]'); + var filledCount = 0; + + // Loop through each date field to check if it's filled + dateFields.forEach(function(field) { + if (field.value) { // If the field is not empty (i.e., has a value) + filledCount++; + } + }); + + // Set the count in the hidden input field + document.getElementById('date_fields_count').value = filledCount; + console.log("Number of filled date fields: " + filledCount); + }); + + // Autocmplete logic const input = document.getElementById('autocomplete-input'); const resultsContainer = document.getElementById('autocomplete-results'); @@ -215,7 +237,7 @@ }, 300); // Wait 300 ms before processing the input }); }); - + {% endblock %} @@ -276,6 +298,9 @@

Osallistuminen: {{ html['challenge']['title'] }}

>

+ + +

@@ -316,9 +341,6 @@

{{ html['data_fields']['taxa_count'] }} lajia

{{ html['taxa']|safe }} - - - {% endblock %} diff --git a/tests-playwright/test_login.py b/tests-playwright/test_login.py index 3aeb971..fdd7690 100644 --- a/tests-playwright/test_login.py +++ b/tests-playwright/test_login.py @@ -157,6 +157,7 @@ def test_add_edit_participation(browser): assert page.input_value("#MX_71663") == "" # Trash the participation + page.fill("#MX_71822", "2024-01-01") # Add one more taxon to test trashing with changed taxon count page.click("#trash_button") page.click("#confirm_button")