class Voto(db.Model): __tablename__ = 'votos' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") nome = db.Column(db.String(70), nullable=False) autor = db.Column(db.String(70), db.ForeignKey('usuarios.id'), name="email_autor", nullable=False) itempauta = db.Column(db.BigInteger, db.ForeignKey('itemspautas.num'), name="num_itempauta", nullable=False) @property def num(self): return self.__num def __init__(self, autor_email, itempauta_num, op_nome): self.autor = autor_email self.itempauta = itempauta_num self.nome = op_nome
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 ComentariosItemPauta(db.Model): __tablename__ = 'comentarios' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") texto = db.Column(db.String(255), unique=True, nullable=False) __dataHoraCriacao = db.Column(db.TIMESTAMP, name="dt_hora_criacao", nullable=False, server_default=db.func.now()) gravatar_src = db.Column(db.String(255), nullable=False) itempauta = db.Column(db.BigInteger, db.ForeignKey('itemspautas.num'), nullable=False, name="num_itempauta") autor = db.Column(db.BigInteger, db.ForeignKey('moradores.num'), nullable=True, name="num_morador") def __init__(self, texto, it, autor, gravatar_src): super(ComentariosItemPauta, self).__init__() self.texto = texto self.itempauta = it self.autor = autor self.gravatar_src = gravatar_src @property ## atributo "num" eh somente leitura def num(self): return self.__num @property def gravatar_img_src(self): return self.gravatar_src @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 nomeAutor(self): morador = Morador.query.get(self.autor) u = User.query.get(morador.usuario_id) return u.nome + ' ' + u.sobrenome
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 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 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 Morador(db.Model): __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) 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
class ComentariosItemPauta(db.Model): __tablename__ = 'comentarios' __num = db.Column(db.BigInteger, primary_key=True, autoincrement=True, name="num") texto = db.Column(db.String(255), unique=True, nullable=False) itempauta = db.Column(db.BigInteger, db.ForeignKey('itemspautas.num'), nullable=False, name="num_itempauta") def __init__(self, texto, it): super(ComentariosItemPauta, self).__init__() self.texto = texto self.itempauta = it
class AnexoModel(db.Model): __tablename__ = 'anexos' __num = db.Column(db.BigInteger, name="num", autoincrement=True, primary_key=True) ## O usuario pode mudar o nome do arquivo depois no Google Drive e portanto, invalidar a URL. ## Portanto,é melhor gravar o usuário do google drive,e na hora de exibir, conectar e trazer os arquivos salvos. ## Na sugestao de item pauta, colocar um botao do "google sign in" usuarioGoogleDrive = db.Column(db.String(70), name="usuario_google_drive") sugestao_itempauta = db.Column(db.BigInteger, db.ForeignKey('sugestoes_itempauta.num'), nullable=False) nome = db.Column(db.String(100), nullable=False) url_download = db.Column(db.String(255), nullable=False) def __init__(self, url, nome, usuarioGoogleDrive=None): super(AnexoModel, self).__init__() self.usuarioGoogleDrive = usuarioGoogleDrive self.url_download = url self.nome = nome
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
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 permissoes_roles = db.Table( 'permissoes_roles', db.metadata, db.Column('role_id', db.Integer, db.ForeignKey('roles.id')), db.Column('permissao_id', db.Integer, db.ForeignKey('permissoes.id'))) 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)