Flask and supabase form

My client asked me to create a Flask backend using Supabase as the database, here’s the problem:

Form data: {'employee': 'Alexandre Martins', 'company': 'test', 'area': 'test', 'sector': 'test', 'city': 'test', 'service': '2024-04-10', 'observations': 'Ragdgh', 'initial_complaint': 'dgfh', 'psychologist': '1'}
These are the data I’m receiving from the frontend, and from the beginning, there was a problem. I could either process the employee query or the company query, but I couldn’t fetch the IDs of both. So, I decided to separate the queries into two separate functions of the endpoint. I also decided to separate the data submission, so the structure became:

Find the company ID.
Find the employee ID.
Endpoint to register the frontend response.
Endpoint to insert into Supabase.

The responses continue to return 500 errors even though the data corresponds to the Supabase tables and matches between the frontend and backend. I never manage to query both employee and company IDs. What should I do? I know my code is messy, but I’ve tried everything to make it work and I’m already lost.

def consultar_id_empresa(nome_empresa):
    response_empresa = Client.from_('empresas').select('id_empresa').eq('empresa', nome_empresa).execute()
    print("Resposta da consulta de empresa:", response_empresa)

    if 'data' not in response_empresa or len(response_empresa['data']) == 0:
        raise Exception("Empresa não encontrada")

    empresa = response_empresa['data'][0]['id_empresa']
    return empresa

def consultar_id_colaborador(nome_colaborador, id_empresa):
    response_colaborador = Client.from_('colaboradores').select('id_colaborador').eq('nome', nome_colaborador).eq('id_empresa', id_empresa).execute()
    print("Resposta da consulta de colaborador:", response_colaborador)

    if 'data' not in response_colaborador or len(response_colaborador['data']) == 0:
        raise Exception("Colaborador não encontrado")

    colaborador = response_colaborador['data'][0]['id_colaborador']
    return colaborador

@app.route('/inserir-atendimento', methods=['POST'])
def inserir_atendimento():
    global dados_formulario

    try:
        # Receber os dados do formulário
        dados_formulario = request.json

        # Chamar o endpoint para processar os dados e inserir na tabela de atendimentos
        response = processar_dados_e_inserir()
        return response

    except Exception as e:
        return {'error': str(e)}, 500

@app.route('/processar-dados-e-inserir', methods=['POST'])
def processar_dados_e_inserir():
    global dados_formulario

    try:
        # Extrair dados do formulário
        empresa_nome = dados_formulario['empresa']
        colaborador_nome = dados_formulario['colaborador']
        psicologo = dados_formulario['psicologo']
        area = dados_formulario['area']
        setor = dados_formulario['setor']
        cidade = dados_formulario['cidade']
        atendimento = dados_formulario['atendimento']
        observacoes = dados_formulario['observacoes']
        queixa_inicial = dados_formulario['queixa_inicial']

        print("Dados do formulário:", dados_formulario)
        print("Empresa:", empresa_nome)
        print("Colaborador:", colaborador_nome)

        # Consultar o ID da empresa
        id_empresa = consultar_id_empresa(empresa_nome)
        print("ID da empresa:", id_empresa)

        # Consultar o ID do colaborador
        id_colaborador = consultar_id_colaborador(colaborador_nome, id_empresa)
        print("ID do colaborador:", id_colaborador)

        # Inserir na tabela de atendimentos
        response_inserir = Client.from_('atendimentos').insert([
            {'colaborador': id_colaborador, 'psicologo': psicologo, 'empresa': id_empresa, 'area': area,
             'setor': setor, 'cidade': cidade, 'atendimento': atendimento, 'observacoes': observacoes,
             'queixa_inicial': queixa_inicial}
        ]).execute()

        if 'error' in response_inserir:
            raise Exception(response_inserir['error'])

        return {'message': 'Atendimento inserido com sucesso'}, 200

    except Exception as e:
        return {'error': str(e)}, 500

New contributor

Alexandre Martins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật