class UsersControllerByRegion(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.interface = FirebaseInterface()

    def get(self, region):
        dic = {"data": self.interface.getDataByField("users", "pais", region)}

        data = json.dumps(dic)
        data_json = json.loads(data)
        return data_json
Example #2
0
class OfficeControllerByUser(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.interface = FirebaseInterface()

    def get(self, cpf):
        dic = {"data": self.interface.getDataByField("offices", "cpf", cpf)}

        data = json.dumps(dic)
        data_json = json.loads(data)
        return data_json
Example #3
0
    def buildObject(self, req):
        interface = FirebaseInterface()

        user_id = req["id_usuario"]
        self.mecanico = interface.getData("users", user_id)

        truck_board = req["placa_caminhao"]
        self.caminhao = interface.getDataByField("trucks", "placa", truck_board)

        self.data = req["data"]

        self.titulo = req["titulo"]
class UsersControllerByJob(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.interface = FirebaseInterface()

    def get(self, job):
        dic = {"data": self.interface.getDataByField("users", "cargo", job)}

        data = json.dumps(dic)
        data_json = json.loads(data)
        http_return_code = 200

        return data_json, http_return_code
class UsersController(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.interface = FirebaseInterface()

    def get(self):
        dic = {"data": self.interface.getData("users")}

        data = json.dumps(dic)
        data_json = json.loads(data)
        return data_json

    def post(self):
        http_return_code = 201
        result = request.get_json()

        try:
            User(result)
            self.interface.addData(result, "users", result["email"])
        except Exception as e:
            http_return_code = 400
            result = str(e)

        return result, http_return_code

    def put(self):
        http_return_code = 200
        result = request.get_json()

        cpf = result["cpf"]
        status = result["status"]

        try:
            user = self.interface.getDataByField("users", "cpf", cpf)

            if user:
                user[0]["status"] = status
                self.interface.updateData(user[0], "users", user[0]["id"])
                result = "Status atualizado com sucesso"
            else:
                result = "Usuario não encontrado"
                http_return_code = 400

        except Exception as e:
            http_return_code = 400
            result = str(e)

        return result, http_return_code
Example #6
0
class Office:
    def __init__(self, dict_office_data):
        self.interface = FirebaseInterface()
        self.office_keys = {
            'cpf': False,
            'endereco': {
                'CEP': False,
                'bairro': False,
                'cidade': False,
                'complemento': False,
                'estado': False,
                'numero': False,
                'pais': False,
                'rua': False
            },
            'nome': False,
            'telefone': False
        }

        self.validateOfficeData(dict_office_data)

    def validateOfficeData(self, dict_office_data):
        for key, value in dict_office_data.items():
            if key in self.office_keys:
                self.office_keys[key] = True
            else:
                raise Exception(
                    'Oficina possui um campo inválido: {}'.format(key))

        not_found_values = []
        for key, value in self.office_keys.items():
            if not value:
                not_found_values.append(key)

        if len(not_found_values) > 0:
            raise Exception(
                'Usuario deve conter os campos: {}'.format(not_found_values))

        self.validateCPF(dict_office_data['cpf'])
        self.validateTel(dict_office_data['telefone'])
        self.validateCep(dict_office_data['endereco']['CEP'])
        self.validateName(dict_office_data['endereco']['bairro'])
        self.validateName(dict_office_data['endereco']['cidade'])
        self.validateName(dict_office_data['endereco']['estado'])
        self.validateNumber(dict_office_data['endereco']['numero'])
        self.validatePais(dict_office_data['endereco']['pais'])
        self.validateName(dict_office_data['endereco']['rua'])

    def validateCPF(self, cpf):
        if not cpfcnpj.validate(cpf):
            raise Exception('CPF inválido')
        else:
            allow = ["supervisor", "gerente", "diretor"]
            user = self.interface.getDataByField('users', 'cpf', cpf)
            if not user:
                raise Exception('Não foi encontrado usuário com esse cpf')
            print (user)
            c = 0
            for a in allow:
                if a in str(user):
                    c += 1
            if c == 0:
                raise Exception('Esse usuário não tem permissão')

    def validateName(self, name):
        if not re.search("^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]+$",
                         name):
            raise Exception('Campo {} é inválido'.format(name))

    def validateNumber(self, num):
        if not re.search("\d+", num):
            raise Exception('Número inválido'.format(num))

    def validateTel(self, tel):
        if not re.search("(\(?\d{2}\)?\s?)(\d{4,5}\-?\d{4})", tel):
            raise Exception('Telefone inválido'.format(tel))

    def validateCep(self, cep):
        if not re.search("\d{5}-\d{3}", cep):
            raise Exception('CEP inválido'.format(cep))

    def validatePais(self, pais):
        list_paises = self.interface.getData('const_data', 'countries')

        if pais not in list_paises['available']:
            raise Exception('País inválido {}'.format(pais))