added room and recorder api and added length check (for sqlite)
This commit is contained in:
@@ -27,10 +27,13 @@ api_v1 = Api(api_bp, prefix="/v1", version='0.1', title='Vue Test API',
|
|||||||
|
|
||||||
api_user = Namespace('user', description="User management namespace", authorizations=api_authorizations)
|
api_user = Namespace('user', description="User management namespace", authorizations=api_authorizations)
|
||||||
api_group = Namespace('group', description="Group management namespace", authorizations=api_authorizations)
|
api_group = Namespace('group', description="Group management namespace", authorizations=api_authorizations)
|
||||||
|
api_room = Namespace('room', description="Room management namespace", authorizations=api_authorizations)
|
||||||
|
api_recorder = Namespace('recorder', description="Recorder management namespace", authorizations=api_authorizations)
|
||||||
|
|
||||||
api_v1.add_namespace(api_user)
|
api_v1.add_namespace(api_user)
|
||||||
api_v1.add_namespace(api_group
|
api_v1.add_namespace(api_group)
|
||||||
)
|
api_v1.add_namespace(api_room)
|
||||||
|
api_v1.add_namespace(api_recorder)
|
||||||
|
|
||||||
auth_api_bp = Blueprint('auth_api', __name__, url_prefix='/api/auth')
|
auth_api_bp = Blueprint('auth_api', __name__, url_prefix='/api/auth')
|
||||||
# user_api_bp = Blueprint('user_api', __name__, url_prefix='/api/user')
|
# user_api_bp = Blueprint('user_api', __name__, url_prefix='/api/user')
|
||||||
@@ -40,6 +43,9 @@ from .example_api import *
|
|||||||
from .auth_api import *
|
from .auth_api import *
|
||||||
from .user_api import *
|
from .user_api import *
|
||||||
from .group_api import *
|
from .group_api import *
|
||||||
|
from .room_api import *
|
||||||
|
from .recorder_api import *
|
||||||
|
#from .group_api import *
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route('/<path:path>')
|
@api_bp.route('/<path:path>')
|
||||||
|
|||||||
@@ -12,13 +12,15 @@ from backend import db
|
|||||||
from backend.api import api_group
|
from backend.api import api_group
|
||||||
from backend.models.user_model import Group
|
from backend.models.user_model import Group
|
||||||
|
|
||||||
|
|
||||||
group_model = api_group.model('Group', {
|
group_model = api_group.model('Group', {
|
||||||
'id': fields.String(required=False, description='The group\'s identifier'),
|
'id': fields.String(required=False, description='The group\'s identifier'),
|
||||||
'name': fields.String(required=True, description='The group\'s name'),
|
'name': fields.String(required=True, description='The group\'s name'),
|
||||||
'description': fields.String(required=False, description='The group\'s description'),
|
'description': fields.String(required=False, description='The group\'s description'),
|
||||||
'users': fields.List(fields.Nested(api_group.model('group_member',
|
'users': fields.List(fields.Nested(api_group.model('group_member',
|
||||||
{'id': fields.Integer(), 'first_name': fields.String(), 'last_name': fields.String()})),
|
{'id': fields.Integer(), 'nickname': fields.String(),
|
||||||
|
'first_name': fields.String(), 'last_name': fields.String(),
|
||||||
|
'email': fields.String(), 'registered_on': fields.DateTime(),
|
||||||
|
'last_seen': fields.DateTime()})),
|
||||||
required=False, description='Group members.')
|
required=False, description='Group members.')
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -61,6 +63,7 @@ class GroupResource(Resource):
|
|||||||
return group
|
return group
|
||||||
api_group.abort(404)
|
api_group.abort(404)
|
||||||
|
|
||||||
|
|
||||||
@api_group.route('')
|
@api_group.route('')
|
||||||
class GroupList(Resource):
|
class GroupList(Resource):
|
||||||
@jwt_required
|
@jwt_required
|
||||||
|
|||||||
255
api/recorder_api.py
Normal file
255
api/recorder_api.py
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
# Copyright (c) 2019. Tobias Kurze
|
||||||
|
"""
|
||||||
|
This module provides functions related to authentication through the API.
|
||||||
|
For example: listing of available auth providers or registration of users.
|
||||||
|
|
||||||
|
Login through API does not start a new session, but instead returns JWT.
|
||||||
|
"""
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
from flask_restplus import fields, Resource
|
||||||
|
|
||||||
|
from backend import db
|
||||||
|
from backend.api import api_recorder
|
||||||
|
from backend.models.recorder_model import Recorder, RecorderModel, RecorderCommand
|
||||||
|
|
||||||
|
recorder_model = api_recorder.model('Recorder', {
|
||||||
|
'id': fields.String(required=False, description='The recorder\'s identifier'),
|
||||||
|
'created_at': fields.DateTime(required=False, description='Creation date of the recorder'),
|
||||||
|
'name': fields.String(required=True, description='The recorder\'s name'),
|
||||||
|
'description': fields.String(required=False, description='The recorder\'s description'),
|
||||||
|
'ip': fields.String(required=False, description='The recorder\'s IP address'),
|
||||||
|
'network_name': fields.String(required=False, description='The recorder\'s network name'),
|
||||||
|
'recorder_model': fields.Nested(api_recorder.model('recorder_model',
|
||||||
|
{'id': fields.Integer(), 'name': fields.String()}),
|
||||||
|
required=False,
|
||||||
|
description='Model of the recorder.'),
|
||||||
|
'room': fields.Nested(api_recorder.model('recorder_room',
|
||||||
|
{'id': fields.Integer(), 'name': fields.String(),
|
||||||
|
'number': fields.String(), 'alternate_name': fields.String()}),
|
||||||
|
required=False,
|
||||||
|
description='Room in which the recorder is located.')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
# ==
|
||||||
|
|
||||||
|
@api_recorder.route('/<int:id>')
|
||||||
|
@api_recorder.response(404, 'Recorder not found')
|
||||||
|
@api_recorder.param('id', 'The recorder identifier')
|
||||||
|
class RecorderResource(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('get_recorder')
|
||||||
|
@api_recorder.marshal_with(recorder_model)
|
||||||
|
def get(self, id):
|
||||||
|
"""Fetch a recorder given its identifier"""
|
||||||
|
recorder = Recorder.query.get(id)
|
||||||
|
if recorder is not None:
|
||||||
|
return recorder
|
||||||
|
api_recorder.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('delete_todo')
|
||||||
|
@api_recorder.response(204, 'Todo deleted')
|
||||||
|
def delete(self, id):
|
||||||
|
"""Delete a recorder given its identifier"""
|
||||||
|
recorder = Recorder.query.get(id)
|
||||||
|
if recorder is not None:
|
||||||
|
db.session.delete(recorder)
|
||||||
|
db.session.commit()
|
||||||
|
return '', 204
|
||||||
|
api_recorder.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('update_recorder')
|
||||||
|
@api_recorder.expect(recorder_model)
|
||||||
|
@api_recorder.marshal_with(recorder_model)
|
||||||
|
def put(self, id):
|
||||||
|
"""Update a recorder given its identifier"""
|
||||||
|
num_rows_matched = Recorder.query.filter_by(id=id).update(api_recorder.payload)
|
||||||
|
if num_rows_matched < 1:
|
||||||
|
api_recorder.abort(404)
|
||||||
|
db.session.commit()
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@api_recorder.route('')
|
||||||
|
class RecorderList(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('recorders')
|
||||||
|
@api_recorder.marshal_list_with(recorder_model)
|
||||||
|
def get(self):
|
||||||
|
"""
|
||||||
|
List all recorders
|
||||||
|
:return: recorders
|
||||||
|
"""
|
||||||
|
return Recorder.get_all()
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('create_recorder')
|
||||||
|
@api_recorder.expect(recorder_model)
|
||||||
|
@api_recorder.marshal_with(recorder_model, code=201)
|
||||||
|
def post(self):
|
||||||
|
recorder = Recorder(**api_recorder.payload)
|
||||||
|
db.session.add(recorder)
|
||||||
|
db.session.commit()
|
||||||
|
return recorder
|
||||||
|
|
||||||
|
|
||||||
|
# ==
|
||||||
|
|
||||||
|
|
||||||
|
recorder_model_model = api_recorder.model('Recorder Model', {
|
||||||
|
'id': fields.String(required=False, description='The recorder model\'s identifier'),
|
||||||
|
'name': fields.String(required=True, description='The recorder model\'s name'),
|
||||||
|
'notes': fields.String(required=False, description='The recorder model\'s notes'),
|
||||||
|
|
||||||
|
'recorders': fields.List(fields.Nested(api_recorder.model('recorder_model',
|
||||||
|
{'id': fields.Integer(), 'name': fields.String(),
|
||||||
|
'network_name': fields.String(),
|
||||||
|
'ip': fields.String()})), required=False,
|
||||||
|
description='Model of the recorder.'),
|
||||||
|
'commands': fields.List(fields.Nested(api_recorder.model('recorder_model_commands',
|
||||||
|
{'id': fields.Integer(), 'name': fields.String()})),
|
||||||
|
required=False,
|
||||||
|
description='Room in which the recorder is located.')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@api_recorder.route('/model/<int:id>')
|
||||||
|
@api_recorder.response(404, 'Recorder Model not found')
|
||||||
|
@api_recorder.param('id', 'The recorder model identifier')
|
||||||
|
class RecorderModelResource(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('get_recorder_model')
|
||||||
|
@api_recorder.marshal_with(recorder_model_model)
|
||||||
|
def get(self, id):
|
||||||
|
"""Fetch a recorder model given its identifier"""
|
||||||
|
recorder_model = RecorderModel.query.get(id)
|
||||||
|
if recorder_model is not None:
|
||||||
|
return recorder_model
|
||||||
|
api_recorder.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('delete_recorder_model')
|
||||||
|
@api_recorder.response(204, 'Recorder model deleted')
|
||||||
|
def delete(self, id):
|
||||||
|
"""Delete a recorder model given its identifier"""
|
||||||
|
recorder_model = RecorderModel.query.get(id)
|
||||||
|
if recorder_model is not None:
|
||||||
|
db.session.delete(recorder_model)
|
||||||
|
db.session.commit()
|
||||||
|
return '', 204
|
||||||
|
api_recorder.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('update_recorder_model')
|
||||||
|
@api_recorder.expect(recorder_model_model)
|
||||||
|
@api_recorder.marshal_with(recorder_model_model)
|
||||||
|
def put(self, id):
|
||||||
|
"""Update a recorder_model given its identifier"""
|
||||||
|
num_rows_matched = RecorderModel.query.filter_by(id=id).update(api_recorder.payload)
|
||||||
|
if num_rows_matched < 1:
|
||||||
|
api_recorder.abort(404)
|
||||||
|
db.session.commit()
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
|
@api_recorder.route('/model')
|
||||||
|
class RecorderModelList(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('recorders')
|
||||||
|
@api_recorder.marshal_list_with(recorder_model_model)
|
||||||
|
def get(self):
|
||||||
|
"""
|
||||||
|
List all recorder models
|
||||||
|
:return: recorder models
|
||||||
|
"""
|
||||||
|
return Recorder.get_all()
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('create_recorder')
|
||||||
|
@api_recorder.expect(recorder_model_model)
|
||||||
|
@api_recorder.marshal_with(recorder_model_model, code=201)
|
||||||
|
def post(self):
|
||||||
|
recorder = Recorder(**api_recorder.payload)
|
||||||
|
db.session.add(recorder)
|
||||||
|
db.session.commit()
|
||||||
|
return recorder
|
||||||
|
|
||||||
|
|
||||||
|
# ==
|
||||||
|
|
||||||
|
|
||||||
|
recorder_command_model = api_recorder.model('Recorder Command', {
|
||||||
|
'id': fields.String(required=False, description='The recorder command\'s identifier'),
|
||||||
|
'name': fields.String(required=True, description='The recorder command\'s name'),
|
||||||
|
'description': fields.String(required=False, description='The recorder command\'s description'),
|
||||||
|
'command': fields.String(required=True, description='The recorder command\'s name'),
|
||||||
|
'recorder_models': fields.List(fields.Nested(api_recorder.model('recorder_command_models',
|
||||||
|
{'id': fields.Integer(), 'name': fields.String()})),
|
||||||
|
required=False,
|
||||||
|
description='Recorder models associated with the command.'),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@api_recorder.route('/command/<int:id>')
|
||||||
|
@api_recorder.response(404, 'Recorder Command not found')
|
||||||
|
@api_recorder.param('id', 'The recorder command identifier')
|
||||||
|
class RecorderCommandResource(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('get_recorder_command')
|
||||||
|
@api_recorder.marshal_with(recorder_command_model)
|
||||||
|
def get(self, id):
|
||||||
|
"""Fetch a recorder command given its identifier"""
|
||||||
|
recorder_command = RecorderCommand.query.get(id)
|
||||||
|
if recorder_command is not None:
|
||||||
|
return recorder_command
|
||||||
|
api_recorder.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('delete_recorder_command')
|
||||||
|
@api_recorder.response(204, 'Recorder_command deleted')
|
||||||
|
def delete(self, id):
|
||||||
|
"""Delete a recorder command given its identifier"""
|
||||||
|
recorder_command = RecorderCommand.query.get(id)
|
||||||
|
if recorder_command is not None:
|
||||||
|
db.session.delete(recorder_command)
|
||||||
|
db.session.commit()
|
||||||
|
return '', 204
|
||||||
|
api_recorder.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('update_recorder_command')
|
||||||
|
@api_recorder.expect(recorder_command_model)
|
||||||
|
@api_recorder.marshal_with(recorder_command_model)
|
||||||
|
def put(self, id):
|
||||||
|
"""Update a recorder command given its identifier"""
|
||||||
|
num_rows_matched = RecorderCommand.query.filter_by(id=id).update(api_recorder.payload)
|
||||||
|
if num_rows_matched < 1:
|
||||||
|
api_recorder.abort(404)
|
||||||
|
db.session.commit()
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
|
@api_recorder.route('/command')
|
||||||
|
class RecorderCommandList(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('recorder_commands')
|
||||||
|
@api_recorder.marshal_list_with(recorder_command_model)
|
||||||
|
def get(self):
|
||||||
|
"""
|
||||||
|
List all recorders commands
|
||||||
|
:return: recorder commands
|
||||||
|
"""
|
||||||
|
return RecorderCommand.get_all()
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_recorder.doc('create_recorder_commands')
|
||||||
|
@api_recorder.expect(recorder_command_model)
|
||||||
|
@api_recorder.marshal_with(recorder_command_model, code=201)
|
||||||
|
def post(self):
|
||||||
|
recorder_command = RecorderCommand(**api_recorder.payload)
|
||||||
|
db.session.add(recorder_command)
|
||||||
|
db.session.commit()
|
||||||
|
return recorder_command
|
||||||
97
api/room_api.py
Normal file
97
api/room_api.py
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
# Copyright (c) 2019. Tobias Kurze
|
||||||
|
"""
|
||||||
|
This module provides functions related to authentication through the API.
|
||||||
|
For example: listing of available auth providers or registration of users.
|
||||||
|
|
||||||
|
Login through API does not start a new session, but instead returns JWT.
|
||||||
|
"""
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
from flask_restplus import fields, Resource
|
||||||
|
from sqlalchemy import exc
|
||||||
|
|
||||||
|
from backend import db, app
|
||||||
|
from backend.api import api_room
|
||||||
|
from backend.models.room_model import Room
|
||||||
|
|
||||||
|
room_model = api_room.model('Room', {
|
||||||
|
'id': fields.String(required=False, description='The room\'s identifier'),
|
||||||
|
'created_at': fields.DateTime(required=False, description='Creation date of the room info'),
|
||||||
|
'name': fields.String(required=True, description='The room\'s name'),
|
||||||
|
'alternate_name': fields.String(required=False, description='The room\'s alternate name'),
|
||||||
|
'comment': fields.String(required=False, description='The room\'s comment'),
|
||||||
|
'number': fields.String(required=True, description='The room\'s number'),
|
||||||
|
'recorder': fields.List(fields.Nested(api_room.model('room_recorder',
|
||||||
|
{'id': fields.Integer(), 'name': fields.String(),
|
||||||
|
'ip': fields.String(), 'network_name': fields.String()})),
|
||||||
|
required=False, description='Room members.')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@api_room.route('/<int:id>')
|
||||||
|
@api_room.response(404, 'Room not found')
|
||||||
|
@api_room.param('id', 'The room identifier')
|
||||||
|
class RoomResource(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_room.doc('get_room')
|
||||||
|
@api_room.marshal_with(room_model)
|
||||||
|
def get(self, id):
|
||||||
|
"""Fetch a user given its identifier"""
|
||||||
|
room = Room.query.get(id)
|
||||||
|
if room is not None:
|
||||||
|
return room
|
||||||
|
api_room.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_room.doc('delete_todo')
|
||||||
|
@api_room.response(204, 'Todo deleted')
|
||||||
|
def delete(self, id):
|
||||||
|
'''Delete a task given its identifier'''
|
||||||
|
room = Room.query.get(id)
|
||||||
|
if room is not None:
|
||||||
|
db.session.delete(room)
|
||||||
|
db.session.commit()
|
||||||
|
return '', 204
|
||||||
|
api_room.abort(404)
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_room.doc('update_room')
|
||||||
|
@api_room.expect(room_model)
|
||||||
|
@api_room.marshal_with(room_model)
|
||||||
|
def put(self, id):
|
||||||
|
'''Update a task given its identifier'''
|
||||||
|
num_rows_matched = Room.query.filter_by(id=id).update(api_room.payload)
|
||||||
|
db.session.commit()
|
||||||
|
# if room is not None:
|
||||||
|
# app.logger.debug(room)
|
||||||
|
# room.update(api_room.payload)
|
||||||
|
# db.session.commit()
|
||||||
|
# return room
|
||||||
|
return "ok"
|
||||||
|
api_room.abort(404)
|
||||||
|
|
||||||
|
|
||||||
|
@api_room.route('')
|
||||||
|
class RoomList(Resource):
|
||||||
|
@jwt_required
|
||||||
|
@api_room.doc('rooms')
|
||||||
|
@api_room.marshal_list_with(room_model)
|
||||||
|
def get(self):
|
||||||
|
"""
|
||||||
|
List all rooms
|
||||||
|
:return: rooms
|
||||||
|
"""
|
||||||
|
return Room.get_all()
|
||||||
|
|
||||||
|
@jwt_required
|
||||||
|
@api_room.doc('create_room')
|
||||||
|
@api_room.expect(room_model)
|
||||||
|
@api_room.marshal_with(room_model, code=201)
|
||||||
|
def post(self):
|
||||||
|
room = Room(**api_room.payload)
|
||||||
|
db.session.add(room)
|
||||||
|
try:
|
||||||
|
db.session.commit()
|
||||||
|
return room
|
||||||
|
except exc.IntegrityError as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return str(e.detail), 400
|
||||||
@@ -16,9 +16,9 @@ metadata = MetaData()
|
|||||||
|
|
||||||
# This is the association table for the many-to-many relationship between
|
# This is the association table for the many-to-many relationship between
|
||||||
# groups and permissions.
|
# groups and permissions.
|
||||||
recorder_recorder_command_table = db.Table('recorder_recorder_command',
|
recorder_model_recorder_command_table = db.Table('recorder_model_recorder_command',
|
||||||
db.Column('recorder_id', db.Integer,
|
db.Column('recorder_model_id', db.Integer,
|
||||||
db.ForeignKey('recorder.id',
|
db.ForeignKey('recorder_model.id',
|
||||||
onupdate="CASCADE",
|
onupdate="CASCADE",
|
||||||
ondelete="CASCADE"),
|
ondelete="CASCADE"),
|
||||||
primary_key=True),
|
primary_key=True),
|
||||||
@@ -29,13 +29,26 @@ recorder_recorder_command_table = db.Table('recorder_recorder_command',
|
|||||||
primary_key=True))
|
primary_key=True))
|
||||||
|
|
||||||
|
|
||||||
|
class RecorderModel(db.Model):
|
||||||
|
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||||
|
model_name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||||
|
notes = db.Column(db.Unicode(255), unique=False, nullable=True, default=None)
|
||||||
|
recorder_commands = db.relationship('RecorderCommand', secondary=recorder_model_recorder_command_table,
|
||||||
|
back_populates='recorder_models')
|
||||||
|
recorders = db.relationship('Recorder', back_populates='recorder_model')
|
||||||
|
|
||||||
|
|
||||||
class Recorder(db.Model):
|
class Recorder(db.Model):
|
||||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||||
description = db.Column(db.Unicode(255), unique=False, nullable=True, default="")
|
description = db.Column(db.Unicode(255), unique=False, nullable=True, default="")
|
||||||
|
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)
|
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')
|
ip = db.Column(db.String(15), unique=True, nullable=True, default=None)
|
||||||
|
network_name = db.Column(db.String(127), unique=True, nullable=True, default=None)
|
||||||
|
recorder_model_id = db.Column(db.Integer, db.ForeignKey('recorder_model.id'))
|
||||||
|
recorder_model = db.relationship('RecorderModel', back_populates='recorders')
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super(Recorder, self).__init__(**kwargs)
|
super(Recorder, self).__init__(**kwargs)
|
||||||
@@ -56,7 +69,7 @@ class Recorder(db.Model):
|
|||||||
Return all groups
|
Return all groups
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return Group.query.all()
|
return Recorder.query.all()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@@ -73,6 +86,7 @@ class RecorderCommand(db.Model):
|
|||||||
"""Table containing permissions associated with groups."""
|
"""Table containing permissions associated with groups."""
|
||||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||||
description = db.Column(db.Unicode(511))
|
description = db.Column(db.Unicode(511), nullable=True, default=None)
|
||||||
recorders = db.relationship(Recorder, secondary=recorder_recorder_command_table,
|
command = db.Column(db.String(2047), nullable=False)
|
||||||
|
recorder_models = db.relationship('RecorderModel', secondary=recorder_model_recorder_command_table,
|
||||||
back_populates='recorder_commands')
|
back_populates='recorder_commands')
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ Models for lecture recorder
|
|||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from sqlalchemy import MetaData
|
from sqlalchemy import MetaData, CheckConstraint
|
||||||
|
|
||||||
from backend import db, app, login_manager
|
from backend import db, app, login_manager
|
||||||
import re
|
import re
|
||||||
from sqlalchemy import or_
|
from sqlalchemy import or_
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from backend.models.recorder_model import Recorder
|
||||||
|
|
||||||
|
|
||||||
metadata = MetaData()
|
metadata = MetaData()
|
||||||
@@ -19,13 +20,21 @@ class Room(db.Model):
|
|||||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||||
name = db.Column(db.Unicode(127), unique=True, nullable=False)
|
name = db.Column(db.Unicode(127), unique=True, nullable=False)
|
||||||
|
alternate_name = db.Column(db.Unicode(127), unique=True, nullable=True, default=None)
|
||||||
|
comment = db.Column(db.Unicode(2047), unique=False, nullable=True, default="")
|
||||||
number = db.Column(db.Unicode(63), unique=True, nullable=True)
|
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)
|
recorder = db.relationship('Recorder', uselist=False, back_populates='room') # one-to-one relation (uselist=False)
|
||||||
|
|
||||||
|
__table_args__ = (
|
||||||
|
CheckConstraint('length(name) > 2',
|
||||||
|
name='name_min_length'),
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super(Room, self).__init__(**kwargs)
|
super(Room, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_by_name(name):
|
def get_by_name(name):
|
||||||
"""
|
"""
|
||||||
@@ -52,12 +61,3 @@ class Room(db.Model):
|
|||||||
def toJSON(self):
|
def toJSON(self):
|
||||||
return json.dumps(self.to_dict(), default=lambda o: o.__dict__,
|
return json.dumps(self.to_dict(), default=lambda o: o.__dict__,
|
||||||
sort_keys=True, indent=4)
|
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')
|
|
||||||
|
|||||||
Reference in New Issue
Block a user