DmitrMakeev
commited on
Commit
•
ea96e06
1
Parent(s):
085167f
Update app.py
Browse files
app.py
CHANGED
@@ -1187,80 +1187,69 @@ def parse_csv_data(data):
|
|
1187 |
parsed_data.append(dict(zip(headers, row)))
|
1188 |
return parsed_data
|
1189 |
|
1190 |
-
|
1191 |
-
|
1192 |
-
conn = sqlite3.connect(db_name)
|
1193 |
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1194 |
|
1195 |
-
|
1196 |
-
|
1197 |
-
|
1198 |
-
|
1199 |
-
|
1200 |
-
|
1201 |
-
|
1202 |
-
|
1203 |
-
|
1204 |
-
|
1205 |
-
|
1206 |
-
|
1207 |
-
|
1208 |
-
|
1209 |
-
|
1210 |
-
|
1211 |
-
|
1212 |
-
|
1213 |
-
|
1214 |
-
|
1215 |
-
|
1216 |
-
|
1217 |
-
|
1218 |
-
|
1219 |
-
|
1220 |
-
|
1221 |
-
|
1222 |
-
|
1223 |
-
|
1224 |
-
|
1225 |
-
|
1226 |
-
|
1227 |
-
|
1228 |
-
|
1229 |
-
|
1230 |
-
|
1231 |
-
|
1232 |
-
|
1233 |
-
|
1234 |
-
|
1235 |
-
"key_pr", "n_con", "canal", "data_t"
|
1236 |
-
]
|
1237 |
-
for field in required_fields:
|
1238 |
-
if field not in transformed_data:
|
1239 |
-
transformed_data[field] = ""
|
1240 |
-
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
1241 |
-
|
1242 |
-
# Обработка номера телефона, если он есть
|
1243 |
-
if 'phone' in user_data:
|
1244 |
-
phone = user_data['phone']
|
1245 |
-
if phone.startswith('+'):
|
1246 |
-
phone = phone[1:]
|
1247 |
-
transformed_data['phone'] = phone
|
1248 |
-
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
1249 |
|
1250 |
-
|
1251 |
-
|
1252 |
-
|
1253 |
-
|
1254 |
-
|
1255 |
-
|
1256 |
-
|
1257 |
|
1258 |
-
# Подтверждение изменений и закрытие соединения
|
1259 |
conn.commit()
|
1260 |
conn.close()
|
1261 |
-
logging.debug(f"User with email {email} inserted successfully")
|
1262 |
-
|
1263 |
-
|
1264 |
|
1265 |
@app.route('/upload_csv', methods=['POST'])
|
1266 |
def upload_csv():
|
@@ -1274,18 +1263,7 @@ def upload_csv():
|
|
1274 |
csv_input = csv.DictReader(stream)
|
1275 |
data = [row for row in csv_input]
|
1276 |
parsed_data = parse_csv_data(data)
|
1277 |
-
|
1278 |
-
# Пример шаблона сопоставления
|
1279 |
-
mapping_template = {
|
1280 |
-
'Email': 'email',
|
1281 |
-
'Name': 'name',
|
1282 |
-
'Phone': 'phone',
|
1283 |
-
# Добавьте остальные поля в шаблон сопоставления
|
1284 |
-
}
|
1285 |
-
|
1286 |
-
for user_data in parsed_data:
|
1287 |
-
update_or_insert_user(DATABASE2, user_data, mapping_template)
|
1288 |
-
|
1289 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1290 |
return jsonify({"error": "Invalid file format"}), 400
|
1291 |
|
@@ -1310,7 +1288,6 @@ def upload_csv():
|
|
1310 |
|
1311 |
|
1312 |
|
1313 |
-
|
1314 |
|
1315 |
|
1316 |
@app.route('/total_users', methods=['GET'])
|
|
|
1187 |
parsed_data.append(dict(zip(headers, row)))
|
1188 |
return parsed_data
|
1189 |
|
1190 |
+
def insert_data(data):
|
1191 |
+
conn = sqlite3.connect(DATABASE2)
|
|
|
1192 |
cursor = conn.cursor()
|
1193 |
+
# Инвертируем порядок данных
|
1194 |
+
data = reversed(data)
|
1195 |
+
for row in data:
|
1196 |
+
# Получение текущего времени в московском часовом поясе
|
1197 |
+
utc_now = datetime.utcnow()
|
1198 |
+
msk_tz = pytz.timezone('Europe/Moscow')
|
1199 |
+
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
1200 |
+
data_t = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1201 |
|
1202 |
+
# Проверка наличия ключей перед их использованием и предоставление значений по умолчанию
|
1203 |
+
name = row.get('Name', '')
|
1204 |
+
phone = row.get('Phone', '')
|
1205 |
+
email = row.get('Email', '')
|
1206 |
+
vk_id = row.get('vk_id', '')
|
1207 |
+
chat_id = row.get('chat_id', '')
|
1208 |
+
ws_st = row.get('ws_st', '')
|
1209 |
+
ws_stop = row.get('ws_stop', '')
|
1210 |
+
web_st = row.get('web_st', 0)
|
1211 |
+
fin_prog = row.get('fin_prog', 0)
|
1212 |
+
b_city = row.get('b_city', '')
|
1213 |
+
b_fin = row.get('b_fin', '')
|
1214 |
+
b_ban = row.get('b_ban', '')
|
1215 |
+
b_ign = row.get('b_ign', '')
|
1216 |
+
b_baners = row.get('b_baners', '')
|
1217 |
+
b_butt = row.get('b_butt', '')
|
1218 |
+
b_mess = row.get('b_mess', '')
|
1219 |
+
shop_st = row.get('shop_st', '')
|
1220 |
+
curator = row.get('curator', '')
|
1221 |
+
pr1 = row.get('pr1', '')
|
1222 |
+
pr2 = row.get('pr2', '')
|
1223 |
+
pr3 = row.get('pr3', '')
|
1224 |
+
pr4 = row.get('pr4', '')
|
1225 |
+
pr5 = row.get('pr5', '')
|
1226 |
+
ad_url = row.get('ad_url', '')
|
1227 |
+
key_pr = row.get('key_pr', '')
|
1228 |
+
n_con = row.get('n_con', '')
|
1229 |
+
canal = row.get('canal', '')
|
1230 |
+
|
1231 |
+
# Создание динамического SQL-запроса с использованием INSERT OR REPLACE
|
1232 |
+
columns = ['name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog', 'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator', 'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t']
|
1233 |
+
values = [name, phone, email, vk_id, chat_id, ws_st, ws_stop, web_st, fin_prog, b_city, b_fin, b_ban, b_ign, b_baners, b_butt, b_mess, shop_st, curator, pr1, pr2, pr3, pr4, pr5, ad_url, key_pr, n_con, canal, data_t]
|
1234 |
+
|
1235 |
+
placeholders = ', '.join(['?' for _ in columns])
|
1236 |
+
columns_str = ', '.join(columns)
|
1237 |
+
|
1238 |
+
query = f'''
|
1239 |
+
INSERT OR REPLACE INTO contacts ({columns_str})
|
1240 |
+
VALUES ({placeholders})
|
1241 |
+
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1242 |
|
1243 |
+
try:
|
1244 |
+
cursor.execute(query, values)
|
1245 |
+
except Exception as e:
|
1246 |
+
print(f"Error inserting row: {row}")
|
1247 |
+
print(f"Error message: {str(e)}")
|
1248 |
+
conn.rollback()
|
1249 |
+
raise
|
1250 |
|
|
|
1251 |
conn.commit()
|
1252 |
conn.close()
|
|
|
|
|
|
|
1253 |
|
1254 |
@app.route('/upload_csv', methods=['POST'])
|
1255 |
def upload_csv():
|
|
|
1263 |
csv_input = csv.DictReader(stream)
|
1264 |
data = [row for row in csv_input]
|
1265 |
parsed_data = parse_csv_data(data)
|
1266 |
+
insert_data(parsed_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1267 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1268 |
return jsonify({"error": "Invalid file format"}), 400
|
1269 |
|
|
|
1288 |
|
1289 |
|
1290 |
|
|
|
1291 |
|
1292 |
|
1293 |
@app.route('/total_users', methods=['GET'])
|