def __create_feira(self, data, codigo): bairro = clean_data_str(data['bairro']).capitalize() bairro_model = BairroModel.search_by_name(bairro) if bairro_model is None: return { 'message': 'the bairro {} does not exists in our data base'.format(bairro) }, 400 dict_location = { 'lat': data['latitude'], 'long': data['longitude'], 'setor_cens': data['setor_censitario'], 'area_pond': data['area_ponderada'], 'endereco': clean_data_str(data['endereco']), 'numero': clean_data_str(data['numero']), 'referencia': clean_data_str(data['referencia']), 'bairro_id': bairro_model.id } location_model = LocationModel(**dict_location) try: location_model.save_to_db() except Exception as e: print(f'at saving to db, the following error show up: {repr(e)}', file=sys.stderr) return {'message': 'Something has go wrong with internally '}, 500 location_id = location_model.id data['name_feira'] = clean_data_str(data['name_feira']).capitalize() dict_feira = { 'name_feira': data['name_feira'], 'cod_registro': codigo, 'location_id': location_id } feira = FeiraLivreModel(**dict_feira) try: feira.save_to_db() except Exception as e: print(f'at saving to db, the following error show up: {repr(e)}', file=sys.stderr) return { 'message': 'Something has go wrong with internal api ' }, 500 return feira
def get(self, name): name = clean_data_str(name).capitalize() result = [1,1] try: result = Base.session.query(FeiraLivreModel).filter(FeiraLivreModel.name_feira == name) except Exception as e: import sys print(repr(e), file=sys.stderr) all_feiras = [feira.json() for feira in result] if all_feiras == []: return {'message': 'no feiras with the name {}'.format(name)} return {'feiras': all_feiras}, 200
def put(self, codigo): data = FeiraLivre.parser.parse_args() codigo = clean_data_str(codigo) fields_validations = self.__fields_validation(data, codigo) if fields_validations != True: return fields_validations feira = self.__create_feira(data, codigo) tup = ({}, 200) if type(feira) == type(tup): print(f'{type(feira)} == {type(FeiraLivreModel)}', file=sys.stderr) return feira return feira.json()
def post(self, codigo): data = FeiraLivre.parser.parse_args() codigo = clean_data_str(codigo) fields_validations = self.__fields_validation(data, codigo) if fields_validations != True: return fields_validations if len(data['endereco']) < 1: return { 'endereco': 'this field {} can not be left blank'.format(data['endereco']) }, 400 if FeiraLivreModel.search_feira_by_codigo(codigo) is not None: return { 'message': 'the feira {} already exists'.format(codigo) }, 409 feira = self.__create_feira(data, codigo) tup = ({}, 200) if type(feira) == type(tup): return feira return feira.json(), 201
def importCsv2SqliteTable(path2csv, path2db=url_db): print('Starting Importation', file=stderr) dict_result = {'nu_sev': 0, 'log': '', 'is_ok': False} path2csv = hf.check_exists_file(path2csv) if path2csv['exists'] == False: dict_result['nu_sev'] = 3 dict_result['log'] += '\r\n enter an valid CSV path' if dict_result['nu_sev'] == 3: return dict_result if __validate_csvHeaderXlines(path2csv): try: df_csv = pd.read_csv(filepath_or_buffer=path2csv['pathFound'], encoding="ISO-8859-1") from Importation.config_importation import dict_crossCsvHeader2dbHeaders dict_config_importation = dict_crossCsvHeader2dbHeaders some_engine = create_engine(path2db) Session = sessionmaker(bind=some_engine) session = Session() Base.metadata.create_all(bind=some_engine) for index, row in df_csv.iterrows(): cell = None for config in dict_config_importation: cell = row[config['NameHeaderInCSV']] if config['expected_type'] == type(cell) or None == type( cell): if config['NameHeaderInCSV'] == 'LONG' or config[ 'NameHeaderInCSV'] == 'LAT': len_latLong = len(str(cell)) if len_latLong != 9: pass else: cell = re.sub(r'(\-\d{2})(\d+)', r'\1.\2', str(cell)) if config['NameHeaderInCSV'] == 'NUMERO': if len(str(cell)) == 0 or re.search( r'^\s+$', cell) is not None: cell = 'N/M' if config['expected_type'] == str: cell = hf.clean_data_str(cell) if re.search(r'\b[A-Z]{2,4}\b', cell, re.IGNORECASE): cell = __sub_shorten_word(cell) cell = cell.capitalize() row[config['NameHeaderInCSV']] = cell else: convert = None try: convert = config['expected_type']( row[config['NameHeaderInCSV']]) except Exception as e: print( f'Tried to convert the cell: {cell} to the type {config["expected_type"]},' f'the following exception was given:\n{repr(e)}\n,please TYPE the correct ' f'data for line {index}, column {config["NameHeaderInCSV"]}', file=stderr) convert = input() row[config['NameHeaderInCSV']] = convert __insertRowInSql(row=row, session=session) session.close() dict_result['is_ok'] = True dict_result['log'] = 'The Convertion went sucessfull' print('Ending Importation', file=stderr) return dict_result except Exception as e: dict_result['nu_sev'] = 3 dict_result[ 'log'] = f'The convertion of the csv {path2csv} has go wrong with pd.read_csv, given the Exception:{repr(e)}' return dict_result else: dict_result['nu_sev'] = 3 dict_result[ 'log'] += '\r\n the csv data does not have the same numbers of positions compared with the headers' return dict_result