class Professor(Usuario): professor_info = db.Column(db.String) # turmas = db.relationship('Turma', back_populates='professor') __mapper_args__ = { 'polymorphic_identity':'prof', } def dbGetAllProfIdNome(): return Usuario.query.with_entities(Usuario.id,Usuario.nome) \ .filter(Usuario.papel =='prof') \ .all() def dbGetAllProfIdNomeExcept(id): return Usuario.query.with_entities(Usuario.id,Usuario.nome) \ .filter(Usuario.papel =='prof') \ .filter(Usuario.id != id) \ .all() def dbGetUser(id): return Usuario.query.filter(Usuario.id == id) \ .filter(Usuario.papel =='prof') \ .first() def dbGetProfIdNome(id): return Usuario.query.with_entities(Usuario.id,Usuario.nome) \ .filter(Usuario.papel =='prof') \ .filter(Usuario.id == id) \ .first() def dbGetAllProf(): return Usuario.query.filter(Usuario.papel =='prof') \ .all()
class Aluno(Usuario): aluno_info = db.Column(db.String) # turmas = db.relationship('Turmas', secondary='turma_alunos', lazy='subquery', # backref=db.backref('alunos', lazy=True)) __mapper_args__ = { 'polymorphic_identity':'al', } def dbGetAllAlunoIdNome(): return Usuario.query.with_entities(Usuario.id,Usuario.nome) \ .filter(Usuario.papel =='al') \ .all()
class Modulo(db.Model): __tablename__ = 'modulos' id = db.Column(db.Integer, primary_key=True, autoincrement=True) titulo = db.Column(db.String, nullable=False) turma_id = db.Column(db.Integer, db.ForeignKey('turmas.id'), nullable=False) texto = db.Column(db.Text, nullable=False) def __init__(self, titulo, turma, texto): self.titulo = titulo self.turma_id = turma.id self.turma = turma self.texto = texto def dbAddModulo(self): db.session.add(self) db.session.commit() def dbDeleteModulo(id): modulo = Modulo.dbGetModulo(id) db.session.delete(modulo) db.session.commit() def dbUpdateModulo(self, titulo, texto): self.titulo = titulo self.texto = texto db.session.commit() def dbGetAllModulos(): return Modulo.query.all() def dbGetModulosByTurma(turma_id): return Modulo.query.filter_by(turma_id=turma_id).all() def dbGetModulo(id): return Modulo.query.filter_by(id=id).first_or_404()
class Materia(db.Model): __tablename__ = 'materias' id = db.Column(db.Integer, primary_key=True) nome = db.Column(db.String, nullable=False) turmas = db.relationship('Turma', back_populates='materia') def __init__(self, nome): self.nome = nome def dbAddMateria(self): db.session.add(self) db.session.commit() def dbUpdateMateria(self, newNome): self.nome = newNome db.session.commit() def dbDeleteMateria(id): materia = Materia.dbGetMateria(id) db.session.delete(materia) db.session.commit() def dbGetAllMateria(): return Materia.query.with_entities(Materia.id, Materia.nome).all() def dbGetAllMateriaIdNomeExcept(id): return Materia.query.with_entities(Materia.id,Materia.nome) \ .filter(Materia.id != id) \ .all() def dbGetMateria(id): return Materia.query.filter(Materia.id == id).first() def dbGetMateriaIdNome(id): return Materia.query.with_entities(Materia.id,Materia.nome) \ .filter(Materia.id == id) \ .first()
class Postagem(db.Model): __tablename__ = 'postagens' id = db.Column(db.Integer, primary_key=True) titulo = db.Column(db.String, nullable=False) turma_id = db.Column(db.Integer, db.ForeignKey('turmas.id'), nullable=False) professor_id = db.Column(db.Integer, db.ForeignKey('usuarios.id')) professor = db.relationship('Professor', backref=db.backref('postagem', lazy=True)) texto = db.Column(db.Text, nullable=False) data = db.Column(db.DateTime, nullable=False) video = db.Column(db.Text, nullable=True) def __init__(self, titulo, turma, professorId, professor, texto, data, video=None): self.titulo = titulo self.turma_id = turma.id self.turma = turma self.professor_id = professorId self.professor = professor self.texto = texto self.data = data self.video = video def dbAddPost(self): db.session.add(self) db.session.commit() def dbUpdatePost(self, titulo, texto, video=None): self.titulo = titulo self.texto = texto self.video = video db.session.commit() def dbDeletePost(id): post = Postagem.dbGetPost(id) db.session.delete(post) db.session.commit() def dbGetAllPosts(): return Postagem.query.all() def dbGetPostsByTurma(turma_id): return Postagem.query.filter_by(turma_id=turma_id).all() def dbGetPost(id): return Postagem.query.filter_by(id=id).first_or_404()
class Turma(db.Model): __tablename__ = 'turmas' id = db.Column(db.Integer, primary_key=True) titulo = db.Column(db.String, nullable=False) materia_id = db.Column(db.Integer, db.ForeignKey('materias.id'), nullable=False) materia = db.relationship('Materia', back_populates='turmas') professores = db.relationship('Professor', secondary='turma_professor', lazy='subquery', backref=db.backref('turmas', lazy=True)) postagens = db.relationship('Postagem', backref='turma') modulos = db.relationship('Modulo', backref='turma') alunos = db.relationship('Aluno', secondary='turma_alunos', lazy='subquery', backref=db.backref('turmas', lazy=True)) def __init__(self, titulo, materia): self.titulo = titulo self.materia_id = materia.id self.materia = materia def dbAddTurma(self): db.session.add(self) db.session.commit() def dbUpdateTurma(self, newTitulo, newMateria): self.titulo = newTitulo self.materia_id = newMateria.id self.materia = newMateria db.session.commit() def dbDeleteTurma(id): turma = Turma.dbGetTurma(id) db.session.delete(turma) db.session.commit() def dbGetAllTurma(): return Turma.query.all() def dbGetTurma(id): return Turma.query.filter_by(id=id).first_or_404() def dbAddAluno(self, aluno): self.alunos.append(aluno) db.session.commit() def dbDeleteAluno(self, aluno): self.alunos.remove(aluno) db.session.commit() def dbAddProfessor(self, professor): self.professores.append(professor) db.session.commit() def dbDeleteProfessor(self, professor): self.professores.remove(professor) db.session.commit() def ehProfessor(self, id): # Retorna True se o professor com id 'id' é professor da turma for prof in self.professores: if str(prof.id) == str(id): return True return False def ehAluno(self, id): # Retorna True se o aluno com id 'id' é aluno da turma for aluno in self.alunos: if str(aluno.id) == str(id): return True return False def dbGetAllTurmaIdMateriaTitulo(): turmas = Turma.query.all() lista = [] for turma in turmas: turmaId = turma.id turmaMateria = turma.materia.nome turmaTitulo = turma.titulo label = str(turmaMateria) + " - " + str(turmaTitulo) choice = (turmaId, label) lista.append(choice) return lista def getProfessor(self, id): for prof in self.professores: if int(prof.id) == int(id): return prof return None def dbGetPost(self, id): self.postagens.query.filter_by(id=id).first_or_404()
#local imports from skoolit import db # from skoolit.models import Aluno turma_alunos = db.Table( 'turma_alunos', db.Column('turma_id', db.Integer, db.ForeignKey('turmas.id'), primary_key=True), db.Column('aluno_id', db.Integer, db.ForeignKey('usuarios.id'), primary_key=True)) turma_prof = db.Table( 'turma_professor', db.Column('turma_id', db.Integer, db.ForeignKey('turmas.id'), primary_key=True), db.Column('prof_id', db.Integer, db.ForeignKey('usuarios.id'), primary_key=True)) class Turma(db.Model): __tablename__ = 'turmas'
class Administrador(Usuario): adm_info = db.Column(db.String) __mapper_args__ = { 'polymorphic_identity': 'adm', }
class Usuario(UserMixin,db.Model): __tablename__ = 'usuarios' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String, nullable=False) papel = db.Column(db.String) nome = db.Column(db.String, nullable=False) hashSenha = db.Column(db.String, nullable=False) __mapper_args__ = { 'polymorphic_identity':'usuario', 'polymorphic_on':papel } def __init__(self, email, papel, nome, senha): self.email = email self.papel = papel self.nome = nome self.hashSenha = generate_password_hash(senha) def validarSenha(self, senha): return check_password_hash(self.hashSenha, senha) def updateSenha(self, newSenha): self.hashSenha = generate_password_hash(newSenha) db.session.commit() def dbAddUser(self): db.session.add(self) db.session.commit() def dbUpdateUser(self, newEmail, newSenha, newNome, newPapel): self.email = newEmail if (newSenha != None): self.updateSenha(newSenha) self.nome = newNome self.papel = newPapel db.session.commit() def dbUpdateEmail(self, newEmail): self.email = newEmail db.session.commit() def dbUpdateNome(self, newNome): self.nome = newNome db.session.commit() def dbDeleteUser(id): usuario = Usuario.dbGetUser(id) db.session.delete(usuario) db.session.commit() def dbGetAllUsers(): return Usuario.query.all() def dbGetUser(id): return Usuario.query.filter_by(id=id).first_or_404() def dbGetUserNomeEmail(identificador): # Retorna o usuário via nome ou email userNome = Usuario.query.filter_by(nome=identificador).first() userMail = Usuario.query.filter_by(email=identificador).first() if (userMail is None and userNome is None): return None elif userNome is not None: return userNome else: return userMail