some changes to auth and group api

This commit is contained in:
2019-04-15 14:14:42 +02:00
parent cbc269edf2
commit 3ecc8e0955
6 changed files with 158 additions and 6 deletions

78
models/recorder_model.py Normal file
View File

@@ -0,0 +1,78 @@
# -*- 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')

63
models/room_model.py Normal file
View File

@@ -0,0 +1,63 @@
# -*- 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')