def data_gsm_all(request): '''Import unchecked GSM data. ''' response = 'Success' ptt_pattern = re.compile('[0]*(?P<platform>[0-9]+)g') try: file = request.POST['file'].file filename = request.POST['file'].filename platform = int(ptt_pattern.search(filename).group('platform')) query = select([DataGsm.date]).where(DataGsm.platform_ == platform) # Read dates that are already in the database df = pd.DataFrame.from_records(DBSession.execute(query).fetchall(), index=DataGsm.date.name, columns=[DataGsm.date.name]) # Load raw csv data csv_data = pd.read_csv(file, sep='\t', index_col=0, parse_dates=True, # Read those values as NaN na_values=['No Fix', 'Batt Drain', 'Low Voltage'], # Only import the first 8 columns usecols=range(9) ) # Remove the lines containing NaN csv_data.dropna(inplace=True) # Filter data with no elevation by converting the column to numeric type csv_data[DataGsm.ele.name] = csv_data[DataGsm.ele.name].convert_objects(convert_numeric=True) # Get the data to insert data_to_insert = csv_data[~csv_data.index.isin(df.index)] # Add the platform to the DataFrame data_to_insert[DataGsm.platform_.name] = platform # Write into the database data_to_insert.to_sql(DataGsm.__table__.name, DBSession.get_bind(), if_exists='append') except: response = 'An error occured.' request.response.status_code = 500 return response