class Morador(db.Model): __tablename__ = 'moradores' __num = db.Column(db.BigInteger, name="num", autoincrement=True, primary_key=True) num_ap = db.Column(db.Integer, default=False) bloco = db.Column(db.String(3), nullable=False) usuario_id = db.Column(db.String(50), db.ForeignKey('usuarios.id'), name="usuario_id", nullable=False) user = db.relationship("User", backref="moradores", cascade="all, delete-orphan", single_parent=True) comentarios = db.relationship("ComentariosItemPauta", backref="moradores", cascade="all, delete-orphan") def __init__(self, num_ap, bloco, user_id): super(Morador, self).__init__() self.num_ap = num_ap self.bloco = bloco self.usuario_id = user_id @property ## atributo "num" eh somente leitura def num(self): return self.__num
class Role(db.Model): __tablename__ = 'roles' __id = db.Column(db.Integer, primary_key=True, name="id") nome = db.Column(db.String(50), unique=True, nullable=False) permissoes_da_role = db.relationship("Permissao", backref="roles", cascade="all, delete-orphan", secondary=permissoes_roles, single_parent=True) usuarios = db.relationship("User", backref="roles", cascade="all, delete-orphan") def __init__(self, nome): super(Role, self).__init__() self.nome = nome def __repr__(self): return "[Id: "+self.__id+",Nome: "+self.nome+"Permissões: "\ +self.permissoes_da_role+",Usuários: "+self.usuarios @property def id(self): return self.__id
class ItemPauta(db.Model): __tablename__ = 'itemspautas' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") status = db.Column(db.String(70), server_default='CRIADO') assembleia = db.Column(db.BigInteger, db.ForeignKey('assembleias.num'), nullable=False, name="num_assembleia") sugestao_itempauta = db.Column(db.BigInteger, db.ForeignKey('sugestoes_itempauta.num'), name="num_sugestao", nullable=False) comentarios = db.relationship("ComentariosItemPauta", backref="itemspautas", cascade="all, delete-orphan") def __init__(self, assembleia, sugestao): super(ItemPauta, self).__init__() self.assembleia = assembleia self.sugestao_itempauta = sugestao @property def get_obj_sugestao(self): return SugestaoItemPauta.query.get(self.sugestao_itempauta) @property def num(self): return self.__num
class ItemPauta(db.Model): __tablename__ = 'itemspautas' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") status = db.Column(db.String(70), server_default='CRIADO') assembleia = db.Column(db.BigInteger, db.ForeignKey('assembleias.num'), nullable=False, name="num_assembleia") sug_itempauta_num = db.Column(db.BigInteger, db.ForeignKey('sugestoes_itempauta.num'), name="num_sugestao", nullable=False) sug_itempauta = db.relationship( "SugestaoItemPauta", backref=db.backref("itemspautas", uselist=False)) # obrigatorio em one-to-one comentarios = db.relationship("ComentariosItemPauta", backref="itemspautas", cascade="all, delete-orphan") votos = db.relationship("Voto", backref="itemspautas", cascade="all, delete-orphan") def __init__(self, assembleia, sugestao): super(ItemPauta, self).__init__() self.assembleia = assembleia self.sug_itempauta_num = sugestao @property def get_obj_sugestao(self): return SugestaoItemPauta.query.get(self.sug_itempauta_num) @property def num(self): return self.__num @property def nomeAutor(self): u = User.query.get(self.sug_itempauta.autor) return u.nome + ' ' + u.sobrenome
class Permissao(db.Model): __tablename__ = 'permissoes' __id = db.Column(db.Integer, primary_key=True, name="id") nome = db.Column(db.String(30), nullable=False) roles_da_permissao = db.relationship("Role", backref="permissoes", cascade="all, delete-orphan", secondary=permissoes_roles, single_parent=True) def __init__(self, nome): super(Permissao, self).__init__() self.nome = nome def __repr__(self): return "[Id: " + self.__id + ",Nome: " + self.nome + "Roles: " \ + self.roles_da_permissao
class OpcaoVoto(db.Model): __tablename__ = 'opcoes_voto' __num = db.Column(db.Integer, primary_key=True, autoincrement=True, name="num") nome = db.Column(db.String(255), unique=True, nullable=False) sugestao_itempauta = db.relationship("SugestaoItemPauta", backref="opcoes_voto", cascade="all, delete-orphan") def __init__(self, nome): super(OpcaoVoto, self).__init__() self.nome = nome @property ## atributo "num" eh somente leitura def num(self): return self.__num
class SugestaoItemPauta(db.Model): __tablename__ = 'sugestoes_itempauta' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") titulo = db.Column(db.String(100), nullable=False) descricao = db.Column(TEXT, nullable=False) status = db.Column(db.String(70), server_default='NAO_AVALIADA') justif_reprovacao = db.Column(db.String(255)) autor = db.Column( db.String(70), db.ForeignKey('usuarios.id'), name="email_autor", nullable=True ) # para permitir deletar um usuario sem deletar suas sugestoes op_voto = db.Column(db.Integer, db.ForeignKey('opcoes_voto.num'), name="opcao_voto", nullable=False) anexos = db.relationship("AnexoModel", backref="itemspautas", cascade="all, delete-orphan", lazy='subquery') def __init__(self, titulo, autor, desc, opcoes_voto=None, anexos=[]): super(SugestaoItemPauta, self).__init__() self.titulo = titulo self.autor = autor self.descricao = desc self.op_voto = opcoes_voto self.anexos = anexos @property def num(self): return self.__num
class User(db.Model, UserMixin): __tablename__ = 'usuarios' __id = db.Column(db.String(50), name="id", primary_key=True) ## email do usuario nome = db.Column(db.String(20), nullable=False) sobrenome = db.Column(db.String(20), nullable=False) dataNascimento = db.Column(db.String(10), nullable=False, name="dt_nascimento") genero = db.Column(db.String(1), nullable=False) hash_senha = db.Column(db.String(128), nullable=False) confirmado = db.Column(db.Boolean, default=False) __role = db.Column(db.Integer, db.ForeignKey('roles.id'), name="role_id", nullable=False) morador = db.relationship("Morador", backref="usuarios", cascade="all, delete-orphan") def __init__(self, email, password, nome, sobrenome, dataNascimento, genero, role, num=None, bloco=None): super(User, self).__init__() self.__id = email self.nome = nome self.sobrenome = sobrenome self.dataNascimento = dataNascimento self.genero = genero self.__role = role self.hash_senha = generate_password_hash(password) if num and bloco: self.morador = Morador(num_ap=num, bloco=bloco, user_id=self.__id) @property def role(self): return self.__role @property def role_nome(self): return Role.query.get(self.__role).nome @property def id(self): return self.__id def alterar(self, email, nome, sobrenome, dt_nascimento, genero, num_ap, bloco, role): if self.__id != email: self.__id = email if self.nome != nome: self.nome = nome if self.sobrenome != sobrenome: self.sobrenome = sobrenome if self.dataNascimento != dt_nascimento: self.dataNascimento = dt_nascimento if self.genero != genero: self.genero = genero if self.role != role: self.__role = role if self.morador.num_ap != num_ap: self.morador.num_ap = num_ap if self.morador.bloco != bloco: self.morador.bloco = bloco db.session.add(self) db.session.commit() def pode(self, task): return self.__role is not None and task in self.__role.permissoes_da_role def is_administrador(self): return self.pode(ConsensusTask.ADMINISTRAR_SISTEMA) def is_senha_correta(self, password): return check_password_hash(self.hash_senha, password) @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) @property def is_morador(self): eh_morador = Morador.query.filter( Morador.usuario_id == self.__id).all() if eh_morador is None: return "Não" else: return "Sim" @property def is_confirmado(self): if not self.confirmado: return "Não" else: return "Sim" def gerar_token_confirmacao(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'confirm': self.__id}) def confirm(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False if data.get('confirm') != self.__id: return False self.confirmado = True return True
class Assembleia(db.Model): __tablename__ = 'assembleias' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") # # no MySql, nao é permitido utilizar funções como valores DEFAULT. Com exceção da CURRENT_TIMESTAMP # somente para colunas TIMESTAMP(na versao 5.5) e tb para para DATETIME (na versao > 5.6) # # http: // dev.mysql.com / doc / refman / 5.7 / en / data - type - defaults.html # __dataHoraCriacao = db.Column( db.TIMESTAMP, name="dt_hora_criacao", nullable=False, server_default=db.func.now( )) ## invoca function "CURRENT_TIMESTAMP" especifica do mysql status = db.Column(db.String(20), server_default='CRIADA') __dataHoraInicio = db.Column(db.String(30), name="dt_hora_inicio", nullable=False) __dataHoraFim = db.Column(db.String(30), name="dt_hora_fim", nullable=False) itemsPautas = db.relationship("ItemPauta", backref="assembleias", cascade="all, delete-orphan") def __init__(self, dtinicio, dtfim): super(Assembleia, self).__init__() self.__dataHoraInicio = dtinicio self.__dataHoraFim = dtfim @property def sugestoesDosItensPauta(self): sugestoes = [(SugestaoItemPauta.query.get(it.sugestao_itempauta)) for it in self.itemsPautas] return sugestoes @property ## atributo "num" eh somente leitura def num(self): return self.__num @property ## recupera a data no formato 'DD-MM-YY hh:mm:ss' e retorna STRING def dataHoraCriacao(self): local_datetime = self.__dataHoraCriacao.strftime('%d-%m-%Y %H:%M:%S') return local_datetime @property def dataHoraCriacaoAsDate(self): return self.__dataHoraCriacao @property def dataHoraInicio(self): return self.__dataHoraInicio @property def dataHoraFim(self): return self.__dataHoraFim