Skip to content

Commit 6ad782b

Browse files
committed
Add form input validation
1 parent 94a8585 commit 6ad782b

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

src/main/main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def get_runs_implemented(handler: PackageHandler) -> int:
436436
return 1
437437

438438

439-
def get_runs_default(handler, url, title, site, difficulty, problem, submitted_solution, site_solution, notes, nb, today):
439+
def get_runs_default(handler: PackageHandler, today: datetime, data: dict) -> None:
440440
"""
441441
Process form inputs and coordinate execution flow by passing values to specialized functions.
442442
Acts as a coordinator between form submission and data processing pipeline.
@@ -454,17 +454,27 @@ def get_runs_default(handler, url, title, site, difficulty, problem, submitted_s
454454
4. Updates configuration columns
455455
"""
456456

457-
if nb is None:
457+
if data["nb"] is None:
458458
nb = "TBD"
459+
else:
460+
nb = data["nb"]
459461

460462
# Clean input strings
461-
package_list = clean_strings(url, title, site, difficulty, problem, submitted_solution, site_solution, notes, nb)
463+
new_package = clean_strings(data["url"],
464+
data["title"],
465+
data["site"],
466+
data["difficulty"],
467+
data["problem"],
468+
data["submitted_solution"],
469+
data["site_solution"],
470+
data["notes"],
471+
nb)
462472

463473

464474
# ######################################
465475
# GET RUNS STARTED (FIRST OR REGULAR)
466476
# ######################################
467-
get_runs_started(handler, package_list, today)
477+
get_runs_started(handler, new_package, today)
468478

469479

470480
# ######################################

src/main/notebook.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,36 +169,57 @@ def get_form_section_button(handler, today):
169169
"""
170170

171171
container_layout = widgets.Layout(
172-
display='flex',
173-
flex_flow='column',
174-
align_items='center',
175-
width='100%'
172+
display="flex",
173+
flex_flow="column",
174+
align_items="center",
175+
width="100%"
176176
)
177177

178+
179+
def validate_form_data():
180+
181+
form_inputs = {
182+
"url": url_widget.value,
183+
"title": title_widget.value,
184+
"site": site_widget.value,
185+
"difficulty": difficulty_widget.value,
186+
"problem": problem_widget.value,
187+
"submitted_solution": submitted_solution_widget.value,
188+
"site_solution": site_solution_widget.value,
189+
"notes": notes_widget.value,
190+
"nb": nb_widget.value
191+
}
192+
193+
for key, value in form_inputs.items():
194+
195+
key_label = key.replace("_", " ").title()
196+
197+
if not isinstance(value, str):
198+
199+
return False, f"Field {key_label} must be a string"
200+
201+
if not value.strip():
202+
203+
return False, f"Field {key_label} cannot be empty"
204+
205+
206+
return True, form_inputs
207+
208+
178209
def create_solution_file(b):
179210

211+
is_valid, form_inputs_validated = validate_form_data()
212+
213+
if not is_valid:
214+
print(f"Validation error: {form_inputs_validated}")
215+
return
216+
180217
print(b.tooltip)
181218

182-
from src import get_runs_default
183-
184-
# try:
185-
# nb = nb
186-
# except:
187-
# nb = "TBD"
188-
189-
get_runs_default(
190-
handler,
191-
title = title_widget.value,
192-
url = url_widget.value,
193-
site = site_widget.value,
194-
difficulty = difficulty_widget.value,
195-
problem = problem_widget.value,
196-
submitted_solution = submitted_solution_widget.value,
197-
site_solution = site_solution_widget.value,
198-
notes = notes_widget.value,
199-
nb = nb_widget.value,
200-
today = today
201-
)
219+
220+
from src import get_runs_default # pylint: disable=import-outside-toplevel
221+
222+
get_runs_default(handler, today, form_inputs_validated)
202223

203224

204225
create_button = widgets.Button(description="Process Entry", tooltip="Processing...")

0 commit comments

Comments
 (0)