def obtener_mascotas_usuario():
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user = UserModel.select().where(UserModel.email == auth['payload']).get()
    mascotasUsuario = MascotaModel.select().where(
        MascotaModel.usuario_id == user.id).execute()
    return mascotas_schema.dumps(mascotasUsuario), 200
def list_agendas_veterinario():
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user = UserModel.select().where(UserModel.email == auth['payload']).get()
    agendas = AgendaModel.select().where(AgendaModel.veterinario == user.id,
                                         AgendaModel.eliminado.is_null(True))
    return agendas_schema.dumps(agendas), 200
def obtener_usuario():
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user_token = UserModel.select().where(
        UserModel.email == auth['payload']).get()

    user = UserModel.get_or_none(id=user_token.id)

    return user_schema.dump(user), 200
def subir_imagen_usuario():
    f = request.files['img']
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user = UserModel.select().where(UserModel.email == auth['payload']).get()
    currentUser = str(user.id)
    ruta = "BACKEND/imagen/perfil_" + currentUser + "/" + currentUser + ".jpg"
    os.mkdir('imagen/' + 'perfil_' + currentUser)
    f.save(
        os.path.join('imagen/' + 'perfil_' + currentUser,
                     secure_filename(currentUser + ".jpg")))
    return ruta
Beispiel #5
0
def create_user():
    data = request.get_json()
    try:
        schema = user_schema.load(data)
    except:
        abort(make_response(jsonify(message="Dato inválido", error=True), 422))
    try:
        user = UserModel.create(rol_id=3, **schema)
    except:
        abort(make_response(jsonify(message="Dato inválido", error=True), 422))

    user: UserModel = UserModel.login(email=data['email'],
                                      password=data['password'])
    user.create_jwt()
    #user = UserModel.select(UserModel, RolModel).join(RolModel).where(UserModel.email == user.email).get()
    user.rol.get()

    return user_schema.dump(user), 201
def eliminar_usuario(id):
    user = UserModel.get_or_none(id=id)

    if user is None:
        abort(
            make_response(jsonify(message="Usuario no existe", error=True),
                          404))

    user.delete()
    return user_schema.dump(user)
Beispiel #7
0
def sesion_login():
    data = request.get_json()
    try:
        schema = sesion_schema.load(data)
    except ValidationError as err:
        abort(
            make_response(
                jsonify(
                    message=
                    'Lo sentimos, sus credenciales no son correctas. Verifique o regístrese antes de volver a intentarlo',
                    err=err.messages), 422))

    user: UserModel = UserModel.login(email=data['email'],
                                      password=data['password'])
    user.create_jwt()
    user = UserModel.select(
        UserModel,
        RolModel).join(RolModel).where(UserModel.email == data['email']).get()

    return user_schema.dump(user), 202
def create_mascota():
    j = request.get_json()
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user = UserModel.select().where(UserModel.email == auth['payload']).get()
    try:
        schema = mascota_schema.load(j)
    except ValidationError as err:
        abort(
            make_response(
                jsonify(message="Dato inválido",
                        error=True,
                        errors=err.messages), 422))

    try:
        mascota = MascotaModel.create(usuario_id=user.id, **schema)
    except IntegrityError as err:
        return {"errors": f'{err}'}, 422

    return make_response(mascota_schema.dump(mascota)), 201
def crear_usuario():
    data = request.get_json()
    try:
        schema = user_schema.load(data)
    except:
        abort(make_response(jsonify(message="Dato inválido", error=True), 422))
    try:
        user = UserModel.create(rol_id=2, **schema)
    except IntegrityError as err:
        return {"errors": f'{err}'}, 422

    return user_schema.dump(user), 201
def actualizar_usuario():
    j = request.get_json()
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user = UserModel.select().where(UserModel.email == auth['payload']).get()
    try:
        schema = user_schema.load(j)
    except ValidationError as error:
        abort(
            make_response(
                jsonify(message="Dato inválido",
                        error=True,
                        errors=error.messages), 422))
    try:
        usuario = UserModel.update(
            actualizado=datetime.now(),
            **schema).where(UserModel.id == user.id).execute()
    except IntegrityError as err:
        return {"errors": f'{err}'}, 422

    usuario = UserModel.get(id=user.id)
    return user_schema.dump(usuario), 202
def actualizar_agenda(id):
    j = request.get_json()
    token = request.headers.get('Authorization')
    auth = UserModel.decode_jwt(token[7:])
    user = UserModel.select().where(UserModel.email == auth['payload']).get()
    try:
        schema = agenda_schema.load(j)
    except ValidationError as err:
        abort(
            make_response(
                jsonify(message="Dato no válido",
                        error=True,
                        errors=err.messages), 404))

    try:
        agenda = AgendaModel.update(actualizado=datetime.now()).where(
            AgendaModel.id_agenda == id).execute()
        agenda = AgendaModel.update(
            usuario=user.id,
            **schema).where(AgendaModel.id_agenda == id).execute()
    except IntegrityError as err:
        return {"errors": f'{err}'}, 422

    return agenda_schema.dump(agenda), 202
    def wrappers(*arg, **kwargs):
        token = request.headers.get('Authorization')

        try:
            auth = jwt.decode(token,
                              key=app.config.get('SECRET_KEY'),
                              algorithms=['HS256'])
        except:
            abort(
                make_response(
                    jsonify(
                        message=
                        'No tienes permisos para hacer este tipo de funciones',
                        error=True), 401))

        user = UserModel.get_by_id(auth['sub'])

        if user.rol == 1:
            return {'msg': 'sin permiso'}

        return wrappers(*arg, **kwargs)
def list_clientes():
    duenos = UserModel.select().join(RolModel).where(UserModel.rol_id == 1)
    return make_response(users_schema.dumps(duenos)), 200
def list_usuarios():
    users = UserModel.select()
    return users_schema.dumps(users), 200
def obtener_datos_usuario(id):
    usuario = UserModel.get_or_none(id=id)
    return make_response(jsonify(user_schema.dump(usuario))), 200
Beispiel #16
0
def cerrar_sesion():
    user: UserModel = UserModel.select(UserModel.id)
    user.remember_token = Update(None)
    user.get()

    return make_response(jsonify(message="Sesión cerrada"))
def usu_inhabilitados():
    usuarios = UserModel.select().join(RolModel).where(
        UserModel.eliminado.is_null(False))
    return users_schema.dumps(usuarios), 200
def list_veterinarios():
    veterinarios = UserModel.select().join(RolModel).where(
        UserModel.rol_id == 2, UserModel.eliminado.is_null(True))
    return make_response(users_schema.dumps(veterinarios)), 200
def list_empleados():
    usuarios = UserModel.select().join(RolModel).where(UserModel.rol_id != 1)
    return make_response(users_schema.dumps(usuarios)), 200