Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -398,6 +398,56 @@ def upload_csv():
|
|
398 |
|
399 |
|
400 |
# ОБНОВЛЯЕМ CSV JSON-файла
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
@app.route('/upload_json', methods=['POST'])
|
402 |
def upload_json():
|
403 |
if 'file' not in request.files:
|
@@ -411,10 +461,10 @@ def upload_json():
|
|
411 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
412 |
return jsonify({"error": "Invalid file format"}), 400
|
413 |
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
|
419 |
|
420 |
|
|
|
398 |
|
399 |
|
400 |
# ОБНОВЛЯЕМ CSV JSON-файла
|
401 |
+
DATABASE = 'data_gc.db'
|
402 |
+
|
403 |
+
# Функция для очистки номера телефона
|
404 |
+
def clean_phone_number_j(phone_number):
|
405 |
+
return re.sub(r'\D', '', phone_number)
|
406 |
+
|
407 |
+
# Функция для вставки данных в базу данных
|
408 |
+
def insert_data_j(data):
|
409 |
+
conn = sqlite3.connect(DATABASE) # Подключаемся к базе данных
|
410 |
+
cursor = conn.cursor()
|
411 |
+
|
412 |
+
for row in data:
|
413 |
+
name = row.get('name', '')
|
414 |
+
phone = row.get('phone', '').lstrip('+')
|
415 |
+
email = row.get('email', '')
|
416 |
+
data_t = row.get('data_t', '').strip('"')
|
417 |
+
|
418 |
+
# Очистка номера телефона
|
419 |
+
phone = clean_phone_number_j(phone)
|
420 |
+
|
421 |
+
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
422 |
+
user_exists = cursor.fetchone()
|
423 |
+
|
424 |
+
if user_exists:
|
425 |
+
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
426 |
+
continue
|
427 |
+
|
428 |
+
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', 'gc_url', 'key_pr', 'n_con', 'canal', 'data_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc']
|
429 |
+
values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), row.get('ws_st', ''), row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('b_city', ''), row.get('b_fin', ''), row.get('b_ban', ''), row.get('b_ign', ''), row.get('b_baners', ''), row.get('b_butt', ''), row.get('b_mess', ''), row.get('shop_st', ''), row.get('curator', ''), row.get('pr1', ''), row.get('pr2', ''), row.get('pr3', ''), row.get('pr4', ''), row.get('pr5', ''), row.get('gc_url', ''), row.get('key_pr', ''), row.get('n_con', ''), row.get('canal', ''), row.get('data_on', ''), row.get('data_t', ''), row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', ''), row.get('gcpc', '')]
|
430 |
+
|
431 |
+
placeholders = ', '.join(['?' for _ in columns])
|
432 |
+
columns_str = ', '.join(columns)
|
433 |
+
|
434 |
+
query = f'''
|
435 |
+
INSERT INTO contacts ({columns_str})
|
436 |
+
VALUES ({placeholders})
|
437 |
+
'''
|
438 |
+
|
439 |
+
try:
|
440 |
+
cursor.execute(query, values)
|
441 |
+
except Exception as e:
|
442 |
+
print(f"Error inserting row: {row}")
|
443 |
+
print(f"Error message: {str(e)}")
|
444 |
+
conn.rollback()
|
445 |
+
continue
|
446 |
+
|
447 |
+
conn.commit()
|
448 |
+
conn.close()
|
449 |
+
|
450 |
+
# Маршрут для загрузки JSON-файла
|
451 |
@app.route('/upload_json', methods=['POST'])
|
452 |
def upload_json():
|
453 |
if 'file' not in request.files:
|
|
|
461 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
462 |
return jsonify({"error": "Invalid file format"}), 400
|
463 |
|
464 |
+
|
465 |
+
|
466 |
+
|
467 |
+
|
468 |
|
469 |
|
470 |
|