# -*- coding: utf-8 -*- """ Models for lecture recorder """ import json from sqlalchemy import MetaData from backend import db, app, login_manager import re from sqlalchemy import or_ from datetime import datetime, timedelta metadata = MetaData() # This is the association table for the many-to-many relationship between # groups and permissions. recorder_recorder_command_table = db.Table('recorder_recorder_command', db.Column('recorder_id', db.Integer, db.ForeignKey('recorder.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)) class Recorder(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="") room = db.relationship('Room', uselist=False, back_populates='recorder') # one-to-one relation (uselist=False) recorder_commands = db.relationship('RecorderCommand', secondary=recorder_recorder_command_table, back_populates='recorders') def __init__(self, **kwargs): super(Recorder, self).__init__(**kwargs) @staticmethod def get_by_name(name): """ Find group by name :param name: :return: """ return Recorder.query.filter(Recorder.name == name).first() @staticmethod def get_all(): """ Return all groups :return: """ return Group.query.all() 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) class RecorderCommand(db.Model): """Table containing permissions associated with groups.""" 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)) recorders = db.relationship(Recorder, secondary=recorder_recorder_command_table, back_populates='recorder_commands')