corected the delete function on settings page
This commit is contained in:
@@ -38,6 +38,14 @@ class MirServerSettings(db.Model):
|
||||
password = db.Column(db.String(100), nullable=False)
|
||||
auth_header = db.Column(db.String(500), nullable=False)
|
||||
|
||||
class BoardSettings(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
hostname = db.Column(db.String(100), nullable=False)
|
||||
mission1 = db.Column(db.String(100), nullable=False)
|
||||
mission2 = db.Column(db.String(100), nullable=False)
|
||||
mission3 = db.Column(db.String(100), nullable=False)
|
||||
mission4 = db.Column(db.String(100), nullable=False)
|
||||
|
||||
# Create the database if it does not exist
|
||||
if not os.path.exists('instance/logs.db'):
|
||||
with app.app_context():
|
||||
@@ -70,10 +78,6 @@ def log_message():
|
||||
input2_status = latest_log.input2_status if latest_log else 'off'
|
||||
input3_status = latest_log.input3_status if latest_log else 'off'
|
||||
input4_status = latest_log.input4_status if latest_log else 'off'
|
||||
mission1 = latest_log.mission1 if latest_log else ''
|
||||
mission2 = latest_log.mission2 if latest_log else ''
|
||||
mission3 = latest_log.mission3 if latest_log else ''
|
||||
mission4 = latest_log.mission4 if latest_log else ''
|
||||
|
||||
if hostname and ip_address and message:
|
||||
# Parse the message to update relay status
|
||||
@@ -97,30 +101,35 @@ def log_message():
|
||||
action = 'on' if "pressed" in message else 'off'
|
||||
if input_index == 1:
|
||||
input1_status = action
|
||||
if action == 'on':
|
||||
execute_mission(mission1)
|
||||
elif input_index == 2:
|
||||
input2_status = action
|
||||
if action == 'on':
|
||||
execute_mission(mission2)
|
||||
elif input_index == 3:
|
||||
input3_status = action
|
||||
if action == 'on':
|
||||
execute_mission(mission3)
|
||||
elif input_index == 4:
|
||||
input4_status = action
|
||||
if action == 'on':
|
||||
execute_mission(mission4)
|
||||
|
||||
new_log = Log(
|
||||
hostname=hostname, ip_address=ip_address, message=message,
|
||||
relay1_status=relay1_status, relay2_status=relay2_status,
|
||||
relay3_status=relay3_status, relay4_status=relay4_status,
|
||||
input1_status=input1_status, input2_status=input2_status,
|
||||
input3_status=input3_status, input4_status=input4_status,
|
||||
mission1=mission1, mission2=mission2, mission3=mission3, mission4=mission4
|
||||
input3_status=input3_status, input4_status=input4_status
|
||||
)
|
||||
db.session.add(new_log)
|
||||
|
||||
# Check if the board settings already exist
|
||||
board_settings = BoardSettings.query.filter_by(hostname=hostname).first()
|
||||
if not board_settings:
|
||||
# Initialize board settings if they do not exist
|
||||
board_settings = BoardSettings(
|
||||
hostname=hostname,
|
||||
mission1='',
|
||||
mission2='',
|
||||
mission3='',
|
||||
mission4=''
|
||||
)
|
||||
db.session.add(board_settings)
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Log saved'}), 201
|
||||
return jsonify({'status': 'error', 'message': 'Invalid data'}), 400
|
||||
@@ -185,7 +194,14 @@ def view_board(hostname):
|
||||
|
||||
@app.route('/delete_board/<hostname>', methods=['POST'])
|
||||
def delete_board(hostname):
|
||||
# Delete all logs related to the board
|
||||
Log.query.filter_by(hostname=hostname).delete()
|
||||
|
||||
# Delete the board settings if they exist
|
||||
board_settings = BoardSettings.query.filter_by(hostname=hostname).first()
|
||||
if board_settings:
|
||||
db.session.delete(board_settings)
|
||||
|
||||
db.session.commit()
|
||||
flash(f'Board {hostname} and its settings have been deleted.', 'success')
|
||||
return redirect(url_for('settings'))
|
||||
@@ -223,20 +239,15 @@ def settings():
|
||||
mir_user = app.config.get('MIR_USER', '')
|
||||
mir_password = app.config.get('MIR_PASSWORD', '')
|
||||
mir_auth_header = app.config.get('MIR_AUTH_HEADER', '')
|
||||
boards = Log.query.with_entities(Log.hostname).distinct().all()
|
||||
boards = BoardSettings.query.all()
|
||||
board_settings = []
|
||||
for board in boards:
|
||||
latest_log = Log.query.filter_by(hostname=board.hostname).order_by(Log.timestamp.desc()).first()
|
||||
board_settings.append({
|
||||
'hostname': board.hostname,
|
||||
'input1': latest_log.input1_status if latest_log else 'off',
|
||||
'input2': latest_log.input2_status if latest_log else 'off',
|
||||
'input3': latest_log.input3_status if latest_log else 'off',
|
||||
'input4': latest_log.input4_status if latest_log else 'off',
|
||||
'mission1': latest_log.mission1 if latest_log else '',
|
||||
'mission2': latest_log.mission2 if latest_log else '',
|
||||
'mission3': latest_log.mission3 if latest_log else '',
|
||||
'mission4': latest_log.mission4 if latest_log else ''
|
||||
'mission1': board.mission1,
|
||||
'mission2': board.mission2,
|
||||
'mission3': board.mission3,
|
||||
'mission4': board.mission4
|
||||
})
|
||||
return render_template('settings.html', cleanup_time=cleanup_time, mir_ip=mir_ip, mir_user=mir_user, mir_password=mir_password, mir_auth_header=mir_auth_header, boards=board_settings)
|
||||
|
||||
@@ -268,29 +279,28 @@ def set_mir_server():
|
||||
|
||||
@app.route('/set_board_settings/<hostname>', methods=['POST'])
|
||||
def set_board_settings(hostname):
|
||||
input1 = request.form.get('input1')
|
||||
input2 = request.form.get('input2')
|
||||
input3 = request.form.get('input3')
|
||||
input4 = request.form.get('input4')
|
||||
mission1 = request.form.get('mission1')
|
||||
mission2 = request.form.get('mission2')
|
||||
mission3 = request.form.get('mission3')
|
||||
mission4 = request.form.get('mission4')
|
||||
if input1 and input2 and input3 and input4 and mission1 and mission2 and mission3 and mission4:
|
||||
latest_log = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).first()
|
||||
if latest_log:
|
||||
latest_log.input1_status = input1
|
||||
latest_log.input2_status = input2
|
||||
latest_log.input3_status = input3
|
||||
latest_log.input4_status = input4
|
||||
latest_log.mission1 = mission1
|
||||
latest_log.mission2 = mission2
|
||||
latest_log.mission3 = mission3
|
||||
latest_log.mission4 = mission4
|
||||
db.session.commit()
|
||||
flash('Board settings updated successfully!', 'success')
|
||||
if mission1 and mission2 and mission3 and mission4:
|
||||
board_settings = BoardSettings.query.filter_by(hostname=hostname).first()
|
||||
if board_settings:
|
||||
board_settings.mission1 = mission1
|
||||
board_settings.mission2 = mission2
|
||||
board_settings.mission3 = mission3
|
||||
board_settings.mission4 = mission4
|
||||
else:
|
||||
flash('No logs found for the specified board.', 'danger')
|
||||
board_settings = BoardSettings(
|
||||
hostname=hostname,
|
||||
mission1=mission1,
|
||||
mission2=mission2,
|
||||
mission3=mission3,
|
||||
mission4=mission4
|
||||
)
|
||||
db.session.add(board_settings)
|
||||
db.session.commit()
|
||||
flash('Board settings updated successfully!', 'success')
|
||||
else:
|
||||
flash('Invalid board settings!', 'danger')
|
||||
return redirect(url_for('settings'))
|
||||
|
||||
BIN
server_api/instance/logs.db
Normal file
BIN
server_api/instance/logs.db
Normal file
Binary file not shown.
@@ -45,6 +45,9 @@
|
||||
</head>
|
||||
<body>
|
||||
<div class="container mt-5">
|
||||
<div class="text-right mb-4">
|
||||
<a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Main Page</a>
|
||||
</div>
|
||||
<h1 class="text-center mb-4">Settings</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@@ -96,33 +99,32 @@
|
||||
<form action="{{ url_for('set_board_settings', hostname=board.hostname) }}" method="post">
|
||||
<div class="form-group">
|
||||
<label for="mission1">Mission ID for Input 1:</label>
|
||||
<input type="text" class="form-control" id="mission1" name="mission1" value="{{ board.mission1 }}" required>
|
||||
<input type="text" class="form-control" id="mission1" name="mission1" value="{{ board.mission1 or '' }}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="mission2">Mission ID for Input 2:</label>
|
||||
<input type="text" class="form-control" id="mission2" name="mission2" value="{{ board.mission2 }}" required>
|
||||
<input type="text" class="form-control" id="mission2" name="mission2" value="{{ board.mission2 or '' }}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="mission3">Mission ID for Input 3:</label>
|
||||
<input type="text" class="form-control" id="mission3" name="mission3" value="{{ board.mission3 }}" required>
|
||||
<input type="text" class="form-control" id="mission3" name="mission3" value="{{ board.mission3 or '' }}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="mission4">Mission ID for Input 4:</label>
|
||||
<input type="text" class="form-control" id="mission4" name="mission4" value="{{ board.mission4 }}" required>
|
||||
<input type="text" class="form-control" id="mission4" name="mission4" value="{{ board.mission4 or '' }}" required>
|
||||
</div>
|
||||
<div class="d-flex justify-content-between">
|
||||
<form action="{{ url_for('delete_board', hostname=board.hostname) }}" method="post" onsubmit="return confirm('Are you sure you want to delete this board?');">
|
||||
<button type="submit" class="btn btn-danger">Delete Board</button>
|
||||
</form>
|
||||
<button type="submit" class="btn btn-primary">Save Settings</button>
|
||||
</div>
|
||||
</form>
|
||||
<form action="{{ url_for('delete_board', hostname=board.hostname) }}" method="post" onsubmit="return confirm('Are you sure you want to delete this board?');" class="mt-2">
|
||||
<button type="submit" class="btn btn-danger">Delete Board</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
<a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Main Page</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user