class Usuario(db.Model): __tablename__ = "tb_usuario" idusuario = db.Column(db.Integer, primary_key=True) nombre = db.Column(db.String(50), unique=False, nullable=False) clave = db.Column(db.String(10), unique=False, nullable=False) estado = db.Column(db.String(1), unique=False, nullable=False) def __init__(self, form): self.usuario = form.get("usuario") self.clave = form.get("clave") self.nombre = form.get("nombre") self.estado = form.get("estado") def to_full_json(self): json_usuario = { "status": 200, "idusuario": self.idusuario, "nombre": self.nombre, "clave": self.clave, "estado": self.estado, } return json_usuario @staticmethod def from_json(json_post): idusuario = json_post.get("idusuario") nombre = json_post.get("nombre") clave = json_post.get("clave") estado = json_post.get("estado") usuario = Usuario( idusuario=idusuario, nombre=nombre, clave=clave, estado=estado ) return usuario
class Post(SearchableMixin, db.Model): __searchable__ = ['body'] id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) language = db.Column(db.String(5)) def __repr__(self): return '<Post {}>'.format(self.body)
class Vehiculo(db.Model): __tablename__ = "tb_vehiculos" idvehiculo = db.Column(db.Integer, primary_key=True) descripcion = db.Column(db.String(100), unique=False, nullable=False) marca = db.Column(db.String(50), unique=False, nullable=False) modelo = db.Column(db.String(50), unique=False, nullable=False) def __init__(self, form): self.descripcion = form.get("descripcion") self.marca = form.get("marca") self.modelo = form.get("modelo")
class VehiculoRuta(db.Model): __tablename__ = "tb_vehiculorutas" idvehiculoruta = db.Column(db.Integer, primary_key=True) idvehiculo = db.Column(db.Integer, db.ForeignKey("tb_vehiculos.idvehiculo")) idruta = db.Column(db.Integer, db.ForeignKey("tb_rutas.idruta")) vehiculo = db.relationship("Vehiculo") ruta = db.relationship("Ruta") def __init__(self, form): self.idvehiculo = form.get("idvehiculo") self.idruta = form.get("idruta")
class Turno(db.Model): __tablename__ = "tb_turno" idturno = db.Column(db.Integer, primary_key=True) descripcion = db.Column(db.String(100), unique=False, nullable=False) def __init__(self, form): self.descripcion = form.get("descripcion") def to_full_json(self): json_turno = { "idturno": self.idturno, "descripcion": self.descripcion, } return json_turno @staticmethod def from_json(json_post): idturno = json_post.get("idturno") descripcion = json_post.get("descripcion") turno = Turno(idturno=idturno, descripcion=descripcion) return turno
class Conductor(db.Model): __tablename__ = "tb_conductores" idconductor = db.Column(db.Integer, primary_key=True) dni = db.Column(db.String(8), unique=False, nullable=False) nombre = db.Column(db.String(50), unique=False, nullable=False) apellido = db.Column(db.String(50), unique=False, nullable=False) fechanac = db.Column(db.String(10), unique=False, nullable=False) licencia = db.Column(db.String(10), unique=False, nullable=False) def __init__(self, form): self.dni = form.get("dni") self.nombre = form.get("nombre") self.apellido = form.get("apellido") self.fechanac = form.get("fechanac") self.licencia = form.get("licencia")
class VehiculoConductor(db.Model): __tablename__ = "tb_vehiculoconductor" idvehiculoconductor = db.Column(db.Integer, primary_key=True) idvehiculoruta = db.Column( db.Integer, db.ForeignKey("tb_vehiculorutas.idvehiculoruta")) idconductor = db.Column(db.Integer, db.ForeignKey("tb_conductores.idconductor")) idturno = db.Column(db.Integer, db.ForeignKey("tb_turno.idturno")) fechainicio = db.Column(db.Date, nullable=False) fechafin = db.Column(db.Date, nullable=False) vehiculoruta = db.relationship("VehiculoRuta") conductor = db.relationship("Conductor") turno = db.relationship("Turno") def __init__(self, form): self.idvehiculoruta = form.get("idvehiculoruta") self.idconductor = form.get("idconductor") self.idturno = form.get("idturno") self.fechainicio = form.get("fechainicio") self.fechafin = form.get("fechafin")
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(61204), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) #image_file = db.Column(db.LargeBinary) followed = db.relationship('User', secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic') def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def get_reset_password_token(self, expires_in=600): return jwt.encode( { 'reset_password': self.id, 'exp': time() + expires_in }, app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8') @staticmethod def verify_reset_password_token(token): try: id = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password'] except: return return User.query.get(id) def __repr__(self): return '<User {}>'.format(self.username) """ def avatar(self, size): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( digest, size) """ def follow(self, user): if not self.is_following(user): self.followed.append(user) def unfollow(self, user): if self.is_following(user): self.followed.remove(user) def is_following(self, user): return self.followed.filter( followers.c.followed_id == user.id).count() > 0 #... return followed posts def followed_posts(self): followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id)).filter( followers.c.follower_id == self.id) # get user's own post own = Post.query.filter_by(user_id=self.id) # add both queries and order them based on time return followed.union(own).order_by(Post.timestamp.desc())
if isinstance(obj, SearchableMixin): remove_from_index(obj.__tablename__, obj) session._changes = None @classmethod def reindex(cls): for obj in cls.query: add_to_index(cls.__tablename__, obj) db.event.listen(db.session, 'before_commit', SearchableMixin.before_commit) db.event.listen(db.session, 'after_commit', SearchableMixin.after_commit) #aux table for many-to-many relationship followers = db.Table( 'followers', db.Column('follower_id', db.Integer, db.ForeignKey('user.id')), db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))) class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(61204), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) #image_file = db.Column(db.LargeBinary) followed = db.relationship('User', secondary=followers, primaryjoin=(followers.c.follower_id == id),