Update app.py
Browse files
app.py
CHANGED
|
@@ -748,6 +748,7 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
|
|
| 748 |
|
| 749 |
logging.debug(f"Processing user with email: {email}")
|
| 750 |
|
|
|
|
| 751 |
cursor.execute("SELECT web_st, ws_st, b_mess FROM contacts WHERE email = ?", (email,))
|
| 752 |
user = cursor.fetchone()
|
| 753 |
logging.debug(f"User found: {user}")
|
|
@@ -755,7 +756,15 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
|
|
| 755 |
current_web_st = user[0] if user else None
|
| 756 |
current_ws_st = user[1] if user else None
|
| 757 |
current_messages = user[2] if user else ""
|
| 758 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
|
| 760 |
transformed_data = {}
|
| 761 |
for json_key, db_column in mapping_template.items():
|
|
@@ -770,18 +779,7 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
|
|
| 770 |
transformed_data[db_column] = str(value)
|
| 771 |
logging.debug(f"Transformed data: {transformed_data}")
|
| 772 |
|
| 773 |
-
|
| 774 |
-
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
| 775 |
-
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
| 776 |
-
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "gc_url",
|
| 777 |
-
"key_pr", "n_con", "canal", "data_t", 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'
|
| 778 |
-
]
|
| 779 |
-
for field in required_fields:
|
| 780 |
-
if field not in transformed_data:
|
| 781 |
-
transformed_data[field] = ""
|
| 782 |
-
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
| 783 |
-
|
| 784 |
-
# Обработка номера телефона
|
| 785 |
if 'phone' in user_data:
|
| 786 |
phone = user_data['phone']
|
| 787 |
if phone.startswith('+'):
|
|
@@ -789,27 +787,16 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
|
|
| 789 |
transformed_data['phone'] = phone
|
| 790 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
| 791 |
|
| 792 |
-
# Обработка статуса WhatsApp
|
| 793 |
-
new_ws_st = user_data.get('ws_st', "").strip().upper()
|
| 794 |
-
if new_ws_st == 'TRUE':
|
| 795 |
-
new_ws_st = '1'
|
| 796 |
-
elif new_ws_st == 'FALSE':
|
| 797 |
-
new_ws_st = '0'
|
| 798 |
-
else:
|
| 799 |
-
new_ws_st = ""
|
| 800 |
-
|
| 801 |
-
# Проверяем наличие ws_st в базе данных и не перезаписываем его, если оно уже существует
|
| 802 |
-
if current_ws_st is not None and current_ws_st != "":
|
| 803 |
-
transformed_data['ws_st'] = current_ws_st
|
| 804 |
-
else:
|
| 805 |
-
transformed_data['ws_st'] = new_ws_st
|
| 806 |
-
|
| 807 |
# Увеличиваем значение web_st на 1, если пользователь уже существует в базе данных
|
| 808 |
if current_web_st is not None and current_web_st != "":
|
| 809 |
transformed_data['web_st'] = int(current_web_st) + 1
|
| 810 |
else:
|
| 811 |
transformed_data['web_st'] = 1
|
| 812 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
new_messages = transformed_data.get('b_mess', "")
|
| 814 |
if current_messages:
|
| 815 |
transformed_data['b_mess'] = current_messages + "; " + new_messages
|
|
|
|
| 748 |
|
| 749 |
logging.debug(f"Processing user with email: {email}")
|
| 750 |
|
| 751 |
+
# Получаем текущее значение из базы данных, если оно существует
|
| 752 |
cursor.execute("SELECT web_st, ws_st, b_mess FROM contacts WHERE email = ?", (email,))
|
| 753 |
user = cursor.fetchone()
|
| 754 |
logging.debug(f"User found: {user}")
|
|
|
|
| 756 |
current_web_st = user[0] if user else None
|
| 757 |
current_ws_st = user[1] if user else None
|
| 758 |
current_messages = user[2] if user else ""
|
| 759 |
+
|
| 760 |
+
# Преобразование статуса WhatsApp
|
| 761 |
+
new_ws_st = user_data.get('ws_st', "").strip().upper()
|
| 762 |
+
if new_ws_st == 'TRUE':
|
| 763 |
+
new_ws_st = '1'
|
| 764 |
+
elif new_ws_st == 'FALSE':
|
| 765 |
+
new_ws_st = '0'
|
| 766 |
+
else:
|
| 767 |
+
new_ws_st = ""
|
| 768 |
|
| 769 |
transformed_data = {}
|
| 770 |
for json_key, db_column in mapping_template.items():
|
|
|
|
| 779 |
transformed_data[db_column] = str(value)
|
| 780 |
logging.debug(f"Transformed data: {transformed_data}")
|
| 781 |
|
| 782 |
+
# Обработка телефона
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
if 'phone' in user_data:
|
| 784 |
phone = user_data['phone']
|
| 785 |
if phone.startswith('+'):
|
|
|
|
| 787 |
transformed_data['phone'] = phone
|
| 788 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
| 789 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 790 |
# Увеличиваем значение web_st на 1, если пользователь уже существует в базе данных
|
| 791 |
if current_web_st is not None and current_web_st != "":
|
| 792 |
transformed_data['web_st'] = int(current_web_st) + 1
|
| 793 |
else:
|
| 794 |
transformed_data['web_st'] = 1
|
| 795 |
|
| 796 |
+
# Записываем новое значение ws_st, независимо от существующего значения
|
| 797 |
+
transformed_data['ws_st'] = new_ws_st
|
| 798 |
+
|
| 799 |
+
# Обработка сообщений
|
| 800 |
new_messages = transformed_data.get('b_mess', "")
|
| 801 |
if current_messages:
|
| 802 |
transformed_data['b_mess'] = current_messages + "; " + new_messages
|