wip
This commit is contained in:
parent
6dd38036e7
commit
097b3dbf09
34 changed files with 1719 additions and 520 deletions
|
@ -1,7 +1,7 @@
|
|||
from flask import Blueprint, render_template, redirect, url_for, request, flash, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
import markdown
|
||||
from app.core.models import Server, App, Subnet
|
||||
from app.core.models import Server, App, Subnet, Port
|
||||
from app.core.extensions import db, limiter
|
||||
from datetime import datetime
|
||||
|
||||
|
@ -196,22 +196,50 @@ def app_new():
|
|||
server_id = request.form.get('server_id')
|
||||
documentation = request.form.get('documentation', '')
|
||||
|
||||
# Get port data from form
|
||||
port_numbers = request.form.getlist('port_numbers[]')
|
||||
protocols = request.form.getlist('protocols[]')
|
||||
port_descriptions = request.form.getlist('port_descriptions[]')
|
||||
|
||||
# Basic validation
|
||||
if not name or not server_id:
|
||||
flash('Please fill in all required fields', 'danger')
|
||||
return render_template(
|
||||
'dashboard/app_form.html',
|
||||
title='New Application',
|
||||
servers=servers,
|
||||
now=datetime.now()
|
||||
servers=servers
|
||||
)
|
||||
|
||||
# Create new app
|
||||
app = App(
|
||||
name=name,
|
||||
server_id=server_id,
|
||||
documentation=documentation
|
||||
)
|
||||
|
||||
db.session.add(app)
|
||||
db.session.flush() # Get the app ID without committing
|
||||
|
||||
# Add ports if provided
|
||||
for i in range(len(port_numbers)):
|
||||
if port_numbers[i] and port_numbers[i].strip():
|
||||
try:
|
||||
port_num = int(port_numbers[i])
|
||||
|
||||
# Get protocol and description, handling index errors
|
||||
protocol = protocols[i] if i < len(protocols) else 'TCP'
|
||||
description = port_descriptions[i] if i < len(port_descriptions) else ''
|
||||
|
||||
new_port = Port(
|
||||
app_id=app.id,
|
||||
port_number=port_num,
|
||||
protocol=protocol,
|
||||
description=description
|
||||
)
|
||||
db.session.add(new_port)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
db.session.commit()
|
||||
|
||||
flash('Application created successfully', 'success')
|
||||
|
@ -220,8 +248,7 @@ def app_new():
|
|||
return render_template(
|
||||
'dashboard/app_form.html',
|
||||
title='New Application',
|
||||
servers=servers,
|
||||
now=datetime.now()
|
||||
servers=servers
|
||||
)
|
||||
|
||||
@bp.route('/app/<int:app_id>', methods=['GET'])
|
||||
|
@ -245,49 +272,69 @@ def app_view(app_id):
|
|||
def app_edit(app_id):
|
||||
"""Edit an existing application"""
|
||||
app = App.query.get_or_404(app_id)
|
||||
servers = Server.query.all()
|
||||
|
||||
if request.method == 'POST':
|
||||
name = request.form.get('name')
|
||||
server_id = request.form.get('server_id')
|
||||
documentation = request.form.get('documentation', '')
|
||||
|
||||
# Process ports
|
||||
ports = []
|
||||
port_numbers = request.form.getlist('port[]')
|
||||
port_types = request.form.getlist('port_type[]')
|
||||
port_descs = request.form.getlist('port_desc[]')
|
||||
# Get port data from form
|
||||
port_numbers = request.form.getlist('port_numbers[]')
|
||||
protocols = request.form.getlist('protocols[]')
|
||||
port_descriptions = request.form.getlist('port_descriptions[]')
|
||||
|
||||
for i in range(len(port_numbers)):
|
||||
if port_numbers[i]:
|
||||
port = {
|
||||
'port': int(port_numbers[i]),
|
||||
'type': port_types[i] if i < len(port_types) else 'tcp',
|
||||
'desc': port_descs[i] if i < len(port_descs) else '',
|
||||
'status': 'open'
|
||||
}
|
||||
ports.append(port)
|
||||
# Validate inputs
|
||||
if not all([name, server_id]):
|
||||
flash('All fields are required', 'danger')
|
||||
return render_template('dashboard/app_form.html',
|
||||
title='Edit Application',
|
||||
app=app,
|
||||
servers=servers,
|
||||
edit_mode=True)
|
||||
|
||||
# Update app
|
||||
app.name = name
|
||||
app.server_id = server_id
|
||||
app.documentation = documentation
|
||||
app.ports = ports
|
||||
|
||||
db.session.commit()
|
||||
# Delete existing ports and recreate them
|
||||
# This simplifies handling additions, deletions, and updates
|
||||
Port.query.filter_by(app_id=app.id).delete()
|
||||
|
||||
flash('Application updated successfully', 'success')
|
||||
return redirect(url_for('dashboard.app_view', app_id=app.id))
|
||||
# Add new ports
|
||||
for i in range(len(port_numbers)):
|
||||
if port_numbers[i] and port_numbers[i].strip():
|
||||
try:
|
||||
port_num = int(port_numbers[i])
|
||||
|
||||
# Get protocol and description, handling index errors
|
||||
protocol = protocols[i] if i < len(protocols) else 'TCP'
|
||||
description = port_descriptions[i] if i < len(port_descriptions) else ''
|
||||
|
||||
new_port = Port(
|
||||
app_id=app.id,
|
||||
port_number=port_num,
|
||||
protocol=protocol,
|
||||
description=description
|
||||
)
|
||||
db.session.add(new_port)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
flash(f'Application {name} has been updated', 'success')
|
||||
return redirect(url_for('dashboard.server_view', server_id=app.server_id))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash(f'Error updating application: {str(e)}', 'danger')
|
||||
|
||||
# GET request - show form with current values
|
||||
servers = Server.query.all()
|
||||
|
||||
return render_template(
|
||||
'dashboard/app_edit.html',
|
||||
title=f'Edit Application - {app.name}',
|
||||
app=app,
|
||||
servers=servers,
|
||||
use_editor=True
|
||||
)
|
||||
return render_template('dashboard/app_form.html',
|
||||
title='Edit Application',
|
||||
app=app,
|
||||
servers=servers,
|
||||
edit_mode=True)
|
||||
|
||||
@bp.route('/app/<int:app_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue