# Path: handlers/base.py
# class BaseHandler(tornado.web.RequestHandler):
#     def __init__(self, application, request, **kwargs):
#         super(BaseHandler, self).__init__(application, request, **kwargs)
#         self.session = DB_Session()
# 
#     def data_received(self, chunk):
#         pass
# 
#     def get_current_user(self):
#         return self.get_secure_cookie("username")
# 
#     def on_finish(self):
#         self.session.close()
# 
# Path: models/models.py
# class ResText(BaseModel):
#     __tablename__ = 'resText'
# 
#     id = Column(Integer, Sequence('resText_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     content = Column(String(255))  # 资源内容
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resText_resGroup, back_populates='resText')
# 
# class ResImage(BaseModel):
#     __tablename__ = 'resImage'
# 
#     id = Column(Integer, Sequence('resImage_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     path = Column(String(128))  # 资源路径
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resImage_resGroup, back_populates='resImage')
# 
# class ResVideo(BaseModel):
#     __tablename__ = 'resVideo'
# 
#     id = Column(Integer, Sequence('resVideo_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     path = Column(String(128))  # 资源路径
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resVideo_resGroup, back_populates='resVideo')
# 
# class ResGroup(BaseModel):
#     __tablename__ = 'resGroup'
# 
#     id = Column(Integer, Sequence('resGroup_id_seq'), primary_key=True)  # id
#     name = Column(String(32))  # 资源组名称
#     data = Column(DATE)  # 新建时间
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resText = relationship('ResText', secondary=resText_resGroup, back_populates='resGroup')
#     resImage = relationship('ResImage', secondary=resImage_resGroup, back_populates='resGroup')
#     resVideo = relationship('ResVideo', secondary=resVideo_resGroup, back_populates='resGroup')
#     resWeb = relationship('ResWeb', secondary=resWeb_resGroup, back_populates='resGroup')
#     userGroup = relationship('UserGroup', secondary=userGroup_resGroup, back_populates='resGroup')
#     deviceGroup = relationship("DeviceGroup", backref="resGroup")  # 对应的设备组
# 
# class ResWeb(BaseModel):
#     __tablename__ = 'resWeb'
# 
#     id = Column(Integer, Sequence('resWeb_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     content = Column(String(128))  # 资源内容
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resWeb_resGroup, back_populates='resWeb')


import os
import time
import tornado
import tornado.auth
import tornado.web
from handlers.base import BaseHandler
from models.models import ResText, ResImage, ResVideo, ResGroup, ResWeb
                memo=memo
        )
        try:
            self.session.merge(video_resource)
            self.session.commit()
            self.redirect('/res/video')
        except:
            err = "视频修改失败"
            video_resource = self.session.query(ResVideo).filter(ResVideo.id == video_id).first()
            self.render("res_video_detail.html", auth_user=self.current_user, video_resource=video_resource, err=err)


# 视频资源-删除
class ResVideoDeleteHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        video_id = self.get_argument("id")
        video_resource = self.session.query(ResVideo).filter(ResVideo.id == video_id).first()
        if os.path.exists(video_resource.path):
            os.remove(video_resource.path)
        self.session.delete(video_resource)
        self.session.commit()
        self.redirect('/res/video')


# 资源组管理
class ResGroupHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        res_group_id = self.get_argument('res_group_id')

# Path: handlers/base.py
# class BaseHandler(tornado.web.RequestHandler):
#     def __init__(self, application, request, **kwargs):
#         super(BaseHandler, self).__init__(application, request, **kwargs)
#         self.session = DB_Session()
# 
#     def data_received(self, chunk):
#         pass
# 
#     def get_current_user(self):
#         return self.get_secure_cookie("username")
# 
#     def on_finish(self):
#         self.session.close()
# 
# Path: models/models.py
# class ResText(BaseModel):
#     __tablename__ = 'resText'
# 
#     id = Column(Integer, Sequence('resText_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     content = Column(String(255))  # 资源内容
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resText_resGroup, back_populates='resText')
# 
# class ResImage(BaseModel):
#     __tablename__ = 'resImage'
# 
#     id = Column(Integer, Sequence('resImage_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     path = Column(String(128))  # 资源路径
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resImage_resGroup, back_populates='resImage')
# 
# class ResVideo(BaseModel):
#     __tablename__ = 'resVideo'
# 
#     id = Column(Integer, Sequence('resVideo_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     path = Column(String(128))  # 资源路径
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resVideo_resGroup, back_populates='resVideo')
# 
# class ResGroup(BaseModel):
#     __tablename__ = 'resGroup'
# 
#     id = Column(Integer, Sequence('resGroup_id_seq'), primary_key=True)  # id
#     name = Column(String(32))  # 资源组名称
#     data = Column(DATE)  # 新建时间
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resText = relationship('ResText', secondary=resText_resGroup, back_populates='resGroup')
#     resImage = relationship('ResImage', secondary=resImage_resGroup, back_populates='resGroup')
#     resVideo = relationship('ResVideo', secondary=resVideo_resGroup, back_populates='resGroup')
#     resWeb = relationship('ResWeb', secondary=resWeb_resGroup, back_populates='resGroup')
#     userGroup = relationship('UserGroup', secondary=userGroup_resGroup, back_populates='resGroup')
#     deviceGroup = relationship("DeviceGroup", backref="resGroup")  # 对应的设备组
# 
# class ResWeb(BaseModel):
#     __tablename__ = 'resWeb'
# 
#     id = Column(Integer, Sequence('resWeb_id_seq'), primary_key=True)  # 资源id
#     name = Column(String(32))  # 资源名称
#     content = Column(String(128))  # 资源内容
#     memo = Column(String(255))  # 资源备注
#     data = Column(DATE)  # 资源新建时间
#     is_able = Column(String(16))  # 是否审批通过
#     company_id = Column(Integer, ForeignKey('company.id'))  # 外键  公司
#     resGroup = relationship('ResGroup', secondary=resWeb_resGroup, back_populates='resWeb')


# Path: handlers/res.py
import os
import time
import tornado
import tornado.auth
import tornado.web
from handlers.base import BaseHandler
from models.models import ResText, ResImage, ResVideo, ResGroup, ResWeb

                memo=memo
        )
        try:
            self.session.merge(video_resource)
            self.session.commit()
            self.redirect('/res/video')
        except:
            err = "视频修改失败"
            video_resource = self.session.query(ResVideo).filter(ResVideo.id == video_id).first()
            self.render("res_video_detail.html", auth_user=self.current_user, video_resource=video_resource, err=err)


# 视频资源-删除
class ResVideoDeleteHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        video_id = self.get_argument("id")
        video_resource = self.session.query(ResVideo).filter(ResVideo.id == video_id).first()
        if os.path.exists(video_resource.path):
            os.remove(video_resource.path)
        self.session.delete(video_resource)
        self.session.commit()
        self.redirect('/res/video')


# 资源组管理
class ResGroupHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        res_group_id = self.get_argument('res_group_id')

        res_groups = self.session.query(ResGroup).filter(ResGroup.company_id == self.get_secure_cookie('company_id')).all()