virtual_command_model changes -> model parser needs modification -> also the model relations between recorden model and virtual command may be removed \(not done yet\)

This commit is contained in:
2020-08-14 15:59:24 +02:00
parent b18f9ba616
commit 3ae6db1184
2 changed files with 37 additions and 12 deletions

View File

@@ -13,7 +13,7 @@ from flask_jwt_extended import jwt_required
from flask_restx import fields, Resource
from backend import db, app
from backend.api import api_virtual_command
from backend.api import api_virtual_command, recorder_command_model
from backend.models import VirtualCommand
from backend.models.recorder_model import Recorder, RecorderModel, RecorderCommand
from backend.models.room_model import Room
@@ -37,6 +37,10 @@ virtual_command_model = api_virtual_command.model('VirtualCommand', {
allow_null=True,
skip_none=False,
description='Parent virtual command.')),
'recorder_commands': fields.List(fields.Nested(recorder_command_model)),
'command_default_params': fields.List(fields.Raw()),
'room': fields.Nested(api_virtual_command.model('recorder_room',
{'id': fields.Integer(), 'name': fields.String(),
'number': fields.String(), 'alternate_name': fields.String()}),
@@ -58,9 +62,9 @@ class VirtualCommandResource(Resource):
@api_virtual_command.marshal_with(virtual_command_model, skip_none=False)
def get(self, id):
"""Fetch a recorder given its identifier"""
recorder = Recorder.query.get(id)
if recorder is not None:
return recorder
command = VirtualCommand.query.get(id)
if command is not None:
return command
api_virtual_command.abort(404)
@jwt_required
@@ -91,7 +95,7 @@ class VirtualCommandResource(Resource):
@api_virtual_command.route('')
class RecorderList(Resource):
class VirtualCommandList(Resource):
@jwt_required
@api_virtual_command.doc('recorders')
@api_virtual_command.marshal_list_with(virtual_command_model, skip_none=False)
@@ -100,7 +104,7 @@ class RecorderList(Resource):
List all recorders
:return: recorders
"""
return Recorder.get_all()
return VirtualCommand.get_all()
virtual_command_model_parser = api_virtual_command.parser()
virtual_command_model_parser.add_argument('notes', type=str, required=True)
@@ -119,7 +123,7 @@ class RecorderList(Resource):
if room is not None:
api_virtual_command.payload["room"] = room
else:
return "specified room (id: {}) does not exist!".format(api_virtual_command.payload["room_id"]), 404
return "specified v-command (id: {}) does not exist!".format(api_virtual_command.payload["room_id"]), 404
recorder_model_id = api_virtual_command.payload.pop('recorder_model_id', None)
if recorder_model_id is None:
api_virtual_command.payload["recorder_model"] = None
@@ -138,7 +142,7 @@ class RecorderList(Resource):
if recorder is not None:
api_virtual_command.payload["recorder"] = recorder
else:
return "specified recorder (id: {}) does not exist!".format(
return "specified v-command (id: {}) does not exist!".format(
api_virtual_command.payload["recorder_id"]), 404
virtual_command = VirtualCommand(**api_virtual_command.payload)
db.session.add(virtual_command)

View File

@@ -34,6 +34,7 @@ virtual_command_recorder_table = db.Table('virtual_command_recorder',
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',
@@ -49,6 +50,9 @@ virtual_command_recorder_model_table = db.Table('virtual_command_recorder_model'
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())
@@ -61,15 +65,19 @@ class VirtualCommand(db.Model):
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_string = db.Column(db.String)
_command_order_json_string = db.Column(db.String, default='')
def __init__(self, **kwargs):
super(VirtualCommand, self).__init__(**kwargs)
@@ -93,13 +101,26 @@ class VirtualCommand(db.Model):
@hybrid_property
def command_order(self):
if self.command_order_string is None:
if self._command_order_json_string is None:
return []
return json.loads(self.command_order_string)
return json.loads(self._command_order_json_string)
@command_order.setter
def command_order(self, ordered_list_of_commands: list):
self.command_order_string = json.dumps(ordered_list_of_commands)
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