Files
lrc-backend/backend/models/virtual_command_model.py

134 lines
6.4 KiB
Python

import json
from datetime import datetime
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import backref
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))
# probably useless!!
# This is the association table for the many-to-many relationship between
# virtual commands and recorder commands.
virtual_command_recorder_model_table = db.Table('virtual_command_recorder_model',
db.Column('virtual_command_id', db.Integer,
db.ForeignKey('virtual_command.id',
onupdate="CASCADE",
ondelete="CASCADE"),
primary_key=True),
db.Column('recorder_model_id', db.Integer,
db.ForeignKey('recorder_model.id',
onupdate="CASCADE",
ondelete="CASCADE"),
primary_key=True))
# probably useless!!
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')
_command_default_params_json_string = db.Column(db.UnicodeText, default='')
# probably useless!!
recorder_model_commands = db.relationship('RecorderModel', secondary=virtual_command_recorder_model_table,
back_populates='virtual_commands')
# probably useless!!
# parent_virtual_command = db.relationship('VirtualCommand', back_populates='child_virtual_commands')
parent_virtual_command_id = db.Column(db.Integer, db.ForeignKey('virtual_command.id'))
child_virtual_commands = db.relationship('VirtualCommand',
backref=backref('parent_virtual_command', remote_side=[id]))
_command_order_json_string = db.Column(db.String, default='')
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_json_string is None:
return []
return json.loads(self._command_order_json_string)
@command_order.setter
def command_order(self, ordered_list_of_commands: list):
self._command_order_json_string = json.dumps(ordered_list_of_commands)
@hybrid_property
def command_default_params(self) -> list:
return json.loads(self._command_default_params_json_string)
@command_default_params.setter
def command_default_params(self, value: list):
self._command_default_params_json_string = json.dumps(value)
def add_command_default_param(self, param_name: str, value: str):
command_default_params = json.loads(self._command_default_params_json_string)
command_default_params.append({'param_name': param_name, 'value': value})
self._command_default_params_json_string = json.dumps(command_default_params)
def __str__(self):
return self.name
def to_dict(self):
return dict(id=self.id, name=self.name, description=self.description)
def toJSON(self):
return json.dumps(self.to_dict(), default=lambda o: o.__dict__,
sort_keys=True, indent=4)