From d4ba7fbb4df5bf276b009820e8f59cbbe3779b86 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 9 Feb 2025 20:18:43 -0500 Subject: [PATCH] Add some refactors --- app/scout/routes.py | 41 ++++++++++++++++++------------------- app/scout/scouting_utils.py | 18 ++++++++-------- app/static/js/scout.add.js | 12 ++++++++--- app/static/js/scout.edit.js | 16 +++++++++++---- app/static/js/scout.list.js | 19 +++++++++++------ 5 files changed, 62 insertions(+), 44 deletions(-) diff --git a/app/scout/routes.py b/app/scout/routes.py index aec731e..b566f4a 100644 --- a/app/scout/routes.py +++ b/app/scout/routes.py @@ -31,27 +31,26 @@ def on_blueprint_init(state): @limiter.limit("10 per minute") @handle_route_errors def add(): - if request.method == "POST": - data = request.get_json() if request.is_json else request.form.to_dict() - - # Convert the drawing coordinates from string to JSON if present - if "auto_path_coords" in data and isinstance(data["auto_path_coords"], str): - try: - json.loads(data["auto_path_coords"]) # Validate JSON - except json.JSONDecodeError: - flash("Invalid path coordinates format", "error") - return redirect(url_for("scouting.home")) - - success, message = scouting_manager.add_scouting_data(data, current_user.get_id()) - - if success: - flash("Team data added successfully", "success") - else: - flash(f"Error adding data: {message}", "error") - - return redirect(url_for("scouting.home")) - - return render_template("scouting/add.html") + if request.method != "POST": + return render_template("scouting/add.html") + data = request.get_json() if request.is_json else request.form.to_dict() + + # Convert the drawing coordinates from string to JSON if present + if "auto_path_coords" in data and isinstance(data["auto_path_coords"], str): + try: + json.loads(data["auto_path_coords"]) # Validate JSON + except json.JSONDecodeError: + flash("Invalid path coordinates format", "error") + return redirect(url_for("scouting.home")) + + success, message = scouting_manager.add_scouting_data(data, current_user.get_id()) + + if success: + flash("Team data added successfully", "success") + else: + flash(f"Error adding data: {message}", "error") + + return redirect(url_for("scouting.home")) @scouting_bp.route("/scouting/list") diff --git a/app/scout/scouting_utils.py b/app/scout/scouting_utils.py index 5fe6bc8..db8ab18 100644 --- a/app/scout/scouting_utils.py +++ b/app/scout/scouting_utils.py @@ -20,11 +20,7 @@ def __init__(self, mongo_uri): def _ensure_collections(self): """Ensure required collections exist""" if "team_data" not in self.db.list_collection_names(): - self.db.create_collection("team_data") - # Create indexes - self.db.team_data.create_index([("team_number", 1)]) - self.db.team_data.create_index([("scouter_id", 1)]) - logger.info("Created team_data collection and indexes") + self._create_team_data_collection() def connect(self): """Establish connection to MongoDB with basic error handling""" @@ -38,15 +34,17 @@ def connect(self): # Ensure team_data collection exists if "team_data" not in self.db.list_collection_names(): - self.db.create_collection("team_data") - # Create indexes - self.db.team_data.create_index([("team_number", 1)]) - self.db.team_data.create_index([("scouter_id", 1)]) - logger.info("Created team_data collection and indexes") + self._create_team_data_collection() except Exception as e: logger.error(f"Failed to connect to MongoDB: {str(e)}") raise + def _create_team_data_collection(self): + self.db.create_collection("team_data") + self.db.team_data.create_index([("team_number", 1)]) + self.db.team_data.create_index([("scouter_id", 1)]) + logger.info("Created team_data collection and indexes") + def ensure_connected(self): """Ensure we have a valid connection, reconnect if necessary""" try: diff --git a/app/static/js/scout.add.js b/app/static/js/scout.add.js index 73619c9..9c30c7a 100644 --- a/app/static/js/scout.add.js +++ b/app/static/js/scout.add.js @@ -151,7 +151,9 @@ function loadImages() { } function drawBackground() { - if (!bgImage.complete) return; + if (!bgImage.complete) { + return; + } const canvasWidth = canvas.width / window.devicePixelRatio; const canvasHeight = canvas.height / window.devicePixelRatio; @@ -164,7 +166,9 @@ function drawBackground() { if (isMobile) { // Use the pre-split field images for mobile const mobileImage = isRedAlliance ? mobileRedImage : mobileBlueImage; - if (!mobileImage.complete) return; + if (!mobileImage.complete) { + return; + } imageScale = Math.min( canvasWidth / mobileImage.width, @@ -224,7 +228,9 @@ function startDrawing(e) { function draw(e) { e.preventDefault(); - if (!isDrawing) return; + if (!isDrawing) { + return; + } const pos = getPointerPosition(e); diff --git a/app/static/js/scout.edit.js b/app/static/js/scout.edit.js index a8f4ffb..216dc60 100644 --- a/app/static/js/scout.edit.js +++ b/app/static/js/scout.edit.js @@ -108,7 +108,9 @@ function resizeCanvas() { } function drawBackground() { - if (!bgImage.complete) return; + if (!bgImage.complete) { + return; + } const canvasWidth = canvas.width / window.devicePixelRatio; const canvasHeight = canvas.height / window.devicePixelRatio; @@ -121,7 +123,9 @@ function drawBackground() { if (isMobile) { // Use the pre-split field images for mobile const mobileImage = isRedAlliance ? mobileRedImage : mobileBlueImage; - if (!mobileImage.complete) return; + if (!mobileImage.complete) { + return; + } imageScale = Math.min( canvasWidth / mobileImage.width, @@ -198,7 +202,9 @@ function startDrawing(e) { function draw(e) { e.preventDefault(); - if (!isDrawing) return; + if (!isDrawing) { + return; + } const pos = getPointerPosition(e); @@ -291,7 +297,9 @@ function loadExistingPath(pathData) { // Set alliance if it exists in the data if (data.alliance) { const allianceInput = document.querySelector(`input[name="alliance"][value="${data.alliance}"]`); - if (allianceInput) allianceInput.checked = true; + if (allianceInput) { + allianceInput.checked = true; + } } redrawPaths(); diff --git a/app/static/js/scout.list.js b/app/static/js/scout.list.js index 16999fa..b4b7bfa 100644 --- a/app/static/js/scout.list.js +++ b/app/static/js/scout.list.js @@ -62,14 +62,18 @@ document.addEventListener('DOMContentLoaded', function() { function initializeCanvas() { canvas = document.getElementById('modalAutoPath'); - if (!canvas) return; + if (!canvas) { + return; + } ctx = canvas.getContext('2d'); resizeCanvas(); } function resizeCanvas() { - if (!canvas || !ctx) return; + if (!canvas || !ctx) { + return; + } const container = canvas.parentElement; const containerWidth = container.clientWidth; @@ -85,7 +89,9 @@ function resizeCanvas() { } function drawPath(pathData, deviceType) { - if (!canvas || !ctx) return; + if (!canvas || !ctx) { + return; + } const currentIsMobile = window.innerWidth < 768; const alliance = pathData.alliance || 'red'; @@ -149,9 +155,10 @@ function drawPath(pathData, deviceType) { function showAutoPath(pathData, autoNotes, deviceType) { // Ensure pathData is an object + let pathObj = pathData; if (typeof pathData === 'string') { try { - pathData = JSON.parse(pathData); + pathObj = JSON.parse(pathData); } catch (error) { console.error('Failed to parse path data:', error); return; @@ -175,13 +182,13 @@ function showAutoPath(pathData, autoNotes, deviceType) { resizeCanvas(); // Ensure pathData has the expected structure - if (!pathData || !pathData.points) { + if (!pathObj || !pathObj.points) { console.error('Invalid path data structure'); return; } // Draw the path - drawPath(pathData, deviceType); + drawPath(pathObj, deviceType); } function closeAutoPathModal() {