64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
# -*- 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()
|
|
|
|
|
|
class Room(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(127), unique=True, nullable=False)
|
|
number = db.Column(db.Unicode(63), unique=True, nullable=True)
|
|
description = db.Column(db.Unicode(255), unique=False, nullable=True, default="")
|
|
recorder = db.relationship('Recorder', uselist=False, back_populates='room') # one-to-one relation (uselist=False)
|
|
|
|
def __init__(self, **kwargs):
|
|
super(Room, self).__init__(**kwargs)
|
|
|
|
@staticmethod
|
|
def get_by_name(name):
|
|
"""
|
|
Find group by name
|
|
:param name:
|
|
:return:
|
|
"""
|
|
return Room.query.filter(Room.name == name).first()
|
|
|
|
@staticmethod
|
|
def get_all():
|
|
"""
|
|
Return all groups
|
|
:return:
|
|
"""
|
|
return Room.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 Permission(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))
|
|
groups = db.relationship(Room, secondary=room_permission_table,
|
|
back_populates='permissions')
|