46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
# We will import db from extensions, but let's define extensions properly
|
|
from app.extensions import db, bcrypt
|
|
|
|
class User(db.Model):
|
|
__tablename__ = 'users'
|
|
|
|
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
nome = db.Column(db.String(120), nullable=False)
|
|
email = db.Column(db.String(180), unique=True, index=True, nullable=False)
|
|
password_hash = db.Column(db.String(255), nullable=False)
|
|
role = db.Column(db.String(20), nullable=False, default='user') # 'admin' or 'user'
|
|
ativo = db.Column(db.Boolean, default=True, nullable=False)
|
|
criado_em = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
|
|
atualizado_em = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
|
|
|
# Relationships
|
|
leads_responsaveis = db.relationship('Lead', backref='usuario_responsavel', lazy='dynamic')
|
|
interacoes = db.relationship('LeadInteracao', backref='usuario', lazy='dynamic')
|
|
|
|
def set_password(self, password: str):
|
|
self.password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
|
|
|
|
def check_password(self, password: str) -> bool:
|
|
return bcrypt.check_password_hash(self.password_hash, password)
|
|
|
|
def is_admin(self) -> bool:
|
|
return self.role == 'admin'
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'nome': self.nome,
|
|
'email': self.email,
|
|
'role': self.role,
|
|
'ativo': self.ativo,
|
|
'criado_em': self.criado_em.isoformat() if self.criado_em else None,
|
|
'atualizado_em': self.atualizado_em.isoformat() if self.atualizado_em else None
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f'<User {self.email} ({self.role})>'
|