From 072553953e191682b767a013c71cddad5e32f4fb Mon Sep 17 00:00:00 2001 From: Marius Mutu Date: Sun, 7 Sep 2025 00:10:21 +0300 Subject: [PATCH] Transform rename button to full edit functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace rename modal with comprehensive edit modal supporting name, MAC, and IP changes - Add edit_computer() method with full validation (MAC format, duplicates) - Create new /api/edit endpoint accepting all computer attributes - Update frontend JavaScript for multi-field editing with client-side validation - Rename functions from openRenameModal/performRename to openEditModal/performEdit - Pre-populate edit form with current values and validate MAC address format 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- app/app.py | 81 ++++++++++++++++++++++++++++++++++++++++ app/static/js/app.js | 71 +++++++++++++++++++++-------------- app/templates/index.html | 24 +++++++----- 3 files changed, 139 insertions(+), 37 deletions(-) diff --git a/app/app.py b/app/app.py index 5f0e4a1..4d75ef3 100644 --- a/app/app.py +++ b/app/app.py @@ -198,6 +198,76 @@ class WOLManager: return {'success': True, 'message': f'Computerul {deleted_name} a fost șters!'} + def edit_computer(self, old_name, new_name, new_mac, new_ip=None): + """Editează un calculator existent - permite modificarea numelui, MAC-ului și IP-ului""" + if not new_name.strip(): + return {'success': False, 'message': 'Numele nou nu poate fi gol!'} + + if not new_mac.strip(): + return {'success': False, 'message': 'Adresa MAC nu poate fi goală!'} + + # Validează formatul MAC-ului (XX:XX:XX:XX:XX:XX) + import re + mac_pattern = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$' + if not re.match(mac_pattern, new_mac): + return {'success': False, 'message': 'Formatul MAC-ului este invalid! Folosește formatul XX:XX:XX:XX:XX:XX'} + + # Normalizează MAC-ul (lowercase și cu :) + new_mac = new_mac.lower().replace('-', ':') + + computers = [] + found = False + old_mac = None + + # Citește toate computerele + if os.path.exists(CONFIG_FILE): + with open(CONFIG_FILE, 'r') as f: + for line in f: + line = line.strip() + if line and not line.startswith('#'): + parts = line.split('|') + if len(parts) >= 2: + if parts[0] == old_name: + old_mac = parts[1] + found = True + # Verifică dacă noul MAC este diferit și nu există deja + if new_mac != old_mac.lower(): + # Verifică dacă noul MAC există deja la alt computer + for check_line in computers: + if not check_line.startswith('#') and check_line.strip(): + check_parts = check_line.split('|') + if len(check_parts) >= 2 and check_parts[1].lower() == new_mac: + return {'success': False, 'message': f'Adresa MAC {new_mac} este deja folosită de alt computer!'} + + # Actualizează computerul cu noile valori + new_ip_value = new_ip.strip() if new_ip else '' + computers.append(f"{new_name}|{new_mac}|{new_ip_value}") + else: + # Verifică dacă noul MAC este folosit de alt computer + if parts[1].lower() == new_mac and parts[0] != old_name: + return {'success': False, 'message': f'Adresa MAC {new_mac} este deja folosită de computerul "{parts[0]}"!'} + computers.append(line) + else: + computers.append(line) + + if not found: + return {'success': False, 'message': f'Computerul "{old_name}" nu a fost găsit!'} + + # Verifică dacă noul nume există deja (doar dacă s-a schimbat numele) + if new_name != old_name: + for computer_line in computers: + if not computer_line.startswith('#') and computer_line.strip(): + parts = computer_line.split('|') + if len(parts) >= 2 and parts[0] == new_name and not computer_line.startswith(f"{new_name}|{new_mac}|"): + return {'success': False, 'message': f'Numele "{new_name}" este deja folosit de alt computer!'} + + # Rescrie fișierul + with open(CONFIG_FILE, 'w') as f: + for computer_line in computers: + f.write(computer_line + '\n') + + return {'success': True, 'message': f'Computerul a fost actualizat cu succes!'} + def scan_network(self, custom_network=None): try: # Încearcă să citească rezultatele scanului Windows mai întâi @@ -398,6 +468,17 @@ def rename_computer(): ) return jsonify(result) +@app.route('/api/edit', methods=['POST']) +def edit_computer(): + data = request.get_json() + result = wol_manager.edit_computer( + data.get('old_name'), + data.get('new_name'), + data.get('new_mac'), + data.get('new_ip') + ) + return jsonify(result) + @app.route('/api/delete', methods=['POST']) def delete_computer(): data = request.get_json() diff --git a/app/static/js/app.js b/app/static/js/app.js index 55c2e1d..aa54278 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -1,11 +1,11 @@ // WOL Manager JavaScript -let scanModal, addModal, renameModal; +let scanModal, addModal, editModal; // Initialize on page load window.onload = function() { scanModal = document.getElementById('scanModal'); addModal = document.getElementById('addModal'); - renameModal = document.getElementById('renameModal'); + editModal = document.getElementById('editModal'); refreshComputers(); }; @@ -46,7 +46,7 @@ function displayComputers(computers) { - - + +