added code to initialize db with models and recodres
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Models for lecture recorder
|
||||
"""
|
||||
import importlib
|
||||
import json
|
||||
import pkgutil
|
||||
import os
|
||||
|
||||
from sqlalchemy import MetaData
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from sqlalchemy.orm import validates
|
||||
|
||||
@@ -14,11 +18,18 @@ from sqlalchemy import or_
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from backend.models.virtual_command_model import virtual_command_recorder_command_table, virtual_command_recorder_table
|
||||
from backend.recorder_adapters import get_defined_recorder_adapters
|
||||
from backend.tools.helpers import file_md5
|
||||
|
||||
metadata = MetaData()
|
||||
|
||||
KNOWN_RECORDERS = {re.compile(r'(SMP)[\s]*([\d]+)[\s]*.?[\s]*([\S]*)'): 'SMP',
|
||||
re.compile(
|
||||
r'(LectureRecorder X2|LectureRecorder|VGADVI Recorder|DVI Broadcaster DL|DVIRecorderDL)'): 'Epiphan'}
|
||||
|
||||
|
||||
class RecorderModel(db.Model):
|
||||
__table_args__ = {'extend_existing': True}
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||
last_time_modified = db.Column(db.DateTime, nullable=True, default=None)
|
||||
@@ -27,7 +38,8 @@ class RecorderModel(db.Model):
|
||||
notes = db.Column(db.Unicode(255), unique=False, nullable=True, default=None)
|
||||
recorder_commands = db.relationship('RecorderCommand', back_populates='recorder_model')
|
||||
recorders = db.relationship('Recorder', back_populates='recorder_model')
|
||||
checksum = db.Column(db.String(63), unique=True, nullable=False)
|
||||
checksum = db.Column(db.String(63), unique=True,
|
||||
nullable=False) # checksum of the recorder commands! (see: model_updater.py)
|
||||
_requires_user = db.Column(db.Integer, default=False, name='requires_user')
|
||||
_requires_password = db.Column(db.Integer, default=True, name='requires_password')
|
||||
|
||||
@@ -65,6 +77,7 @@ class RecorderModel(db.Model):
|
||||
|
||||
|
||||
class Recorder(db.Model):
|
||||
__table_args__ = {'extend_existing': True}
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||
last_time_modified = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||
@@ -76,6 +89,8 @@ class Recorder(db.Model):
|
||||
room_id = db.Column(db.Integer, db.ForeignKey('room.id'))
|
||||
room = db.relationship('Room', uselist=False, back_populates='recorder') # one-to-one relation (uselist=False)
|
||||
ip = db.Column(db.String(15), unique=True, nullable=True, default=None)
|
||||
configured_options_json_string = db.Column(db.UnicodeText, default='')
|
||||
firmware_version = db.Column(db.String, nullable=True, default=None)
|
||||
ip6 = db.Column(db.String(46), unique=True, nullable=True, default=None)
|
||||
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)
|
||||
@@ -84,7 +99,8 @@ class Recorder(db.Model):
|
||||
password = db.Column(db.String, nullable=True, default=None)
|
||||
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', secondary=virtual_command_recorder_table, back_populates='recorders')
|
||||
virtual_commands = db.relationship('VirtualCommand', secondary=virtual_command_recorder_table,
|
||||
back_populates='recorders')
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(Recorder, self).__init__(**kwargs)
|
||||
@@ -106,6 +122,17 @@ class Recorder(db.Model):
|
||||
assert len(value) > 2
|
||||
return value
|
||||
|
||||
@hybrid_property
|
||||
def configured_options(self) -> list:
|
||||
return json.loads(self.configured_options_json_string)
|
||||
|
||||
@configured_options.setter
|
||||
def configured_options(self, value: list):
|
||||
self.configured_options_json_string = json.dumps(value)
|
||||
|
||||
def add_configured_option(self, value: str):
|
||||
self.configured_options_json_string = json.dumps(self.configured_options.append(value))
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -118,10 +145,11 @@ class Recorder(db.Model):
|
||||
|
||||
|
||||
class RecorderCommand(db.Model):
|
||||
__table_args__ = {'extend_existing': True}
|
||||
"""Table containing permissions associated with groups."""
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||
last_time_modified = db.Column(db.DateTime, nullable=True, default=None)
|
||||
last_time_modified = db.Column(db.DateTime, nullable=True, default=datetime.utcnow())
|
||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||
alternative_name = db.Column(db.Unicode(63), unique=True, nullable=True, default=None)
|
||||
disabled = db.Column(db.Boolean, default=False)
|
||||
@@ -130,7 +158,7 @@ class RecorderCommand(db.Model):
|
||||
recorder_model = db.relationship('RecorderModel', back_populates='recorder_commands')
|
||||
recorder_model_id = db.Column(db.Integer, db.ForeignKey('recorder_model.id'))
|
||||
virtual_commands = db.relationship('VirtualCommand', secondary=virtual_command_recorder_command_table,
|
||||
back_populates='recorder_commands')
|
||||
back_populates='recorder_commands')
|
||||
|
||||
@staticmethod
|
||||
def get_all():
|
||||
@@ -145,3 +173,35 @@ class RecorderCommand(db.Model):
|
||||
@parameters.setter
|
||||
def parameters(self, parameters_dict: dict):
|
||||
self.parameters_string = json.dumps(parameters_dict)
|
||||
|
||||
|
||||
def pre_create_recorders():
|
||||
models = set()
|
||||
f = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'models', 'initial_recorders.json'))
|
||||
with open(f, 'r') as json_file:
|
||||
recorders = json.load(json_file)['recorders']
|
||||
for r in recorders:
|
||||
type = r.get('type')
|
||||
firmware_version = r.get('firmware_version', None)
|
||||
for k_r in KNOWN_RECORDERS:
|
||||
if match := k_r.search(type):
|
||||
print(KNOWN_RECORDERS[k_r])
|
||||
print(match)
|
||||
print(match.groups())
|
||||
|
||||
models.add(r.get('type', ''))
|
||||
models.discard('')
|
||||
print(models)
|
||||
|
||||
|
||||
def pre_fill_table():
|
||||
try:
|
||||
pre_create_recorders()
|
||||
print(db.session.commit())
|
||||
except IntegrityError as e:
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
# pre_create_recorder_models()
|
||||
|
||||
Reference in New Issue
Block a user