added virtual commands model
This commit is contained in:
@@ -11,6 +11,7 @@ import re
|
||||
from sqlalchemy import or_
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from backend.models.virtual_command_model import virtual_command_recorder_command_table
|
||||
|
||||
metadata = MetaData()
|
||||
|
||||
@@ -32,6 +33,7 @@ recorder_model_recorder_command_table = db.Table('recorder_model_recorder_comman
|
||||
class RecorderModel(db.Model):
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
model_name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||
model_checksum = db.Column(db.String(63), unique=True, nullable=True)
|
||||
notes = db.Column(db.Unicode(255), unique=False, nullable=True, default=None)
|
||||
recorder_commands = db.relationship('RecorderCommand', secondary=recorder_model_recorder_command_table,
|
||||
back_populates='recorder_models')
|
||||
@@ -49,9 +51,9 @@ class Recorder(db.Model):
|
||||
network_name = db.Column(db.String(127), unique=True, nullable=True, default=None)
|
||||
telnet_port = db.Column(db.Integer, unique=False, nullable=False, default=23)
|
||||
ssh_port = db.Column(db.Integer, unique=False, nullable=False, default=22)
|
||||
use_telnet_instead_ssh = db.Column(db.Boolean, nullable=False, default=False)
|
||||
recorder_model_id = db.Column(db.Integer, db.ForeignKey('recorder_model.id'))
|
||||
recorder_model = db.relationship('RecorderModel', back_populates='recorders')
|
||||
virtual_commands = db.relationship('VirtualCommand', back_populates='recorders')
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(Recorder, self).__init__(**kwargs)
|
||||
@@ -90,6 +92,9 @@ class RecorderCommand(db.Model):
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||
description = db.Column(db.Unicode(511), nullable=True, default=None)
|
||||
command = db.Column(db.String(2047), nullable=False)
|
||||
parameters = db.Column(db.String(2047), nullable=True)
|
||||
recorder_models = db.relationship('RecorderModel', secondary=recorder_model_recorder_command_table,
|
||||
back_populates='recorder_commands')
|
||||
|
||||
virtual_commands = db.relationship('VirtualCommand', secondary=virtual_command_recorder_command_table,
|
||||
back_populates='recorder_commands')
|
||||
|
||||
@@ -13,7 +13,7 @@ from backend.models.example_model import ExampleDataItem
|
||||
import re
|
||||
import jwt
|
||||
from flask_login import UserMixin
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy import or_, event
|
||||
from datetime import datetime, timedelta
|
||||
from passlib.hash import sha256_crypt
|
||||
from hashlib import md5
|
||||
@@ -455,3 +455,9 @@ class Permission(db.Model):
|
||||
description = db.Column(db.Unicode(511))
|
||||
groups = db.relationship(Group, secondary=group_permission_table,
|
||||
back_populates='permissions')
|
||||
|
||||
|
||||
@event.listens_for(Permission.__table__, 'after_create')
|
||||
def insert_initial_permissions(*args, **kwargs):
|
||||
for p in app.config.get("PERMISSIONS", []):
|
||||
db.session.add(p)
|
||||
|
||||
94
models/virtual_command_model.py
Normal file
94
models/virtual_command_model.py
Normal file
@@ -0,0 +1,94 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
|
||||
from backend import db
|
||||
|
||||
# This is the association table for the many-to-many relationship between
|
||||
# virtual commands and recorder commands.
|
||||
virtual_command_recorder_command_table = db.Table('virtual_command_recorder_command',
|
||||
db.Column('virtual_command_id', db.Integer,
|
||||
db.ForeignKey('virtual_command.id',
|
||||
onupdate="CASCADE",
|
||||
ondelete="CASCADE"),
|
||||
primary_key=True),
|
||||
db.Column('recorder_command_id', db.Integer,
|
||||
db.ForeignKey('recorder_command.id',
|
||||
onupdate="CASCADE",
|
||||
ondelete="CASCADE"),
|
||||
primary_key=True))
|
||||
|
||||
|
||||
# This is the association table for the many-to-many relationship between
|
||||
# virtual commands and recorder commands.
|
||||
virtual_command_recorder_table = db.Table('virtual_command_recorder',
|
||||
db.Column('virtual_command_id', db.Integer,
|
||||
db.ForeignKey('virtual_command.id',
|
||||
onupdate="CASCADE",
|
||||
ondelete="CASCADE"),
|
||||
primary_key=True),
|
||||
db.Column('recorder_id', db.Integer,
|
||||
db.ForeignKey('recorder.id',
|
||||
onupdate="CASCADE",
|
||||
ondelete="CASCADE"),
|
||||
primary_key=True))
|
||||
|
||||
|
||||
class VirtualCommand(db.Model):
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||
description = db.Column(db.Unicode(255), unique=False, nullable=True, default="")
|
||||
|
||||
recorders = db.relationship('Recorder', secondary=virtual_command_recorder_table,
|
||||
back_populates='virtual_commands')
|
||||
|
||||
recorder_commands = db.relationship('RecorderCommand', secondary=virtual_command_recorder_command_table,
|
||||
back_populates='virtual_commands')
|
||||
|
||||
parent_virtual_command = db.relationship('VirtualCommand', back_populates='child_virtual_commands',
|
||||
nullable=True, default=None)
|
||||
child_virtual_commands = db.relationship('VirtualCommand', back_populates='parent_virtual_command')
|
||||
|
||||
command_order_string = db.Column(db.String)
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(VirtualCommand, self).__init__(**kwargs)
|
||||
|
||||
@staticmethod
|
||||
def get_by_name(name):
|
||||
"""
|
||||
Find group by name
|
||||
:param name:
|
||||
:return:
|
||||
"""
|
||||
return VirtualCommand.query.filter(VirtualCommand.name == name).first()
|
||||
|
||||
@staticmethod
|
||||
def get_all():
|
||||
"""
|
||||
Return all groups
|
||||
:return:
|
||||
"""
|
||||
return VirtualCommand.query.all()
|
||||
|
||||
@hybrid_property
|
||||
def command_order(self):
|
||||
if self.command_order_string is None:
|
||||
return []
|
||||
return self.command_order_string.split()
|
||||
|
||||
@command_order.setter
|
||||
def command_order(self, value: list):
|
||||
pass
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def to_dict(self):
|
||||
return dict(id=self.id, name=self.name)
|
||||
|
||||
def toJSON(self):
|
||||
return json.dumps(self.to_dict(), default=lambda o: o.__dict__,
|
||||
sort_keys=True, indent=4)
|
||||
Reference in New Issue
Block a user