DmitrMakeev
commited on
Commit
•
2021172
1
Parent(s):
3231b45
Update app.py
Browse files
app.py
CHANGED
@@ -723,57 +723,31 @@ gog_url = "https://docs.google.com/forms/d/e/1FAIpQLSc-JbmXvgpgGq6KrkXsYSsfMACVM
|
|
723 |
|
724 |
DATABASE_NAME = 'data_gc.db'
|
725 |
|
726 |
-
def update_or_insert_user(db_name, user_data, mapping_template):
|
727 |
conn = sqlite3.connect(db_name)
|
728 |
cursor = conn.cursor()
|
729 |
|
730 |
-
# Получение email пользователя из данных
|
731 |
email = user_data.get('email')
|
732 |
if not email:
|
733 |
logging.error(f"User data missing email: {user_data}")
|
734 |
return
|
735 |
|
736 |
logging.debug(f"Processing user with email: {email}")
|
737 |
-
|
738 |
-
|
739 |
-
cursor.execute("SELECT web_st, ws_st FROM contacts WHERE email = ?", (email,))
|
740 |
user = cursor.fetchone()
|
741 |
logging.debug(f"User found: {user}")
|
742 |
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
if user:
|
747 |
-
# Проверка текущего значения web_st и его инкрементация
|
748 |
-
current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
|
749 |
-
web_st_value = int(current_web_st) + 1
|
750 |
-
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
751 |
-
|
752 |
-
# Получение текущего значения ws_st
|
753 |
-
current_ws_st = user[1] if user[1] is not None and user[1] != "" else None
|
754 |
-
|
755 |
-
# Обновление значения web_st
|
756 |
-
cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
|
757 |
-
conn.commit()
|
758 |
-
conn.close()
|
759 |
-
logging.debug(f"User {email} web_st updated to {web_st_value}")
|
760 |
-
else:
|
761 |
-
conn.close()
|
762 |
-
logging.debug(f"User {email} not found, proceeding with insert")
|
763 |
-
|
764 |
-
# Открываем соединение снова для остальных операций
|
765 |
-
conn = sqlite3.connect(db_name)
|
766 |
-
cursor = conn.cursor()
|
767 |
|
768 |
-
# Преобразование данных пользователя на основе шаблона сопоставления
|
769 |
transformed_data = {}
|
770 |
for json_key, db_column in mapping_template.items():
|
771 |
value = user_data.get(json_key, "")
|
772 |
-
|
773 |
if isinstance(value, list):
|
774 |
-
# Проверяем тип элементов списка
|
775 |
if all(isinstance(item, str) for item in value):
|
776 |
-
transformed_data[db_column] = "; ".join(value)
|
777 |
else:
|
778 |
logging.error(f"Expected list of strings for key {json_key}, but got: {value}")
|
779 |
transformed_data[db_column] = ""
|
@@ -781,11 +755,10 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
781 |
transformed_data[db_column] = str(value)
|
782 |
logging.debug(f"Transformed data: {transformed_data}")
|
783 |
|
784 |
-
# Заполнение обязательных полей значениями по умолчанию
|
785 |
required_fields = [
|
786 |
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
787 |
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
788 |
-
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "
|
789 |
"key_pr", "n_con", "canal", "data_t", 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'
|
790 |
]
|
791 |
for field in required_fields:
|
@@ -793,7 +766,6 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
793 |
transformed_data[field] = ""
|
794 |
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
795 |
|
796 |
-
# Обработка номера телефона, если он есть
|
797 |
if 'phone' in user_data:
|
798 |
phone = user_data['phone']
|
799 |
if phone.startswith('+'):
|
@@ -801,16 +773,25 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
801 |
transformed_data['phone'] = phone
|
802 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
803 |
|
804 |
-
#
|
805 |
-
|
806 |
-
|
807 |
-
# Проверка наличия ws_st в базе данных и не перезаписываем его, если оно уже существует
|
808 |
-
if current_ws_st is not None:
|
809 |
transformed_data['ws_st'] = current_ws_st
|
810 |
else:
|
811 |
transformed_data['ws_st'] = user_data.get('ws_st', "")
|
812 |
|
813 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
814 |
if user:
|
815 |
update_query = "UPDATE contacts SET "
|
816 |
update_values = []
|
@@ -829,11 +810,13 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
829 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
830 |
cursor.execute(insert_query, insert_values)
|
831 |
|
832 |
-
# Подтверждение изменений и закрытие соединения
|
833 |
conn.commit()
|
834 |
conn.close()
|
835 |
logging.debug(f"User with email {email} processed successfully")
|
836 |
|
|
|
|
|
|
|
837 |
|
838 |
@app.route('/send_request', methods=['POST'])
|
839 |
def send_request():
|
|
|
723 |
|
724 |
DATABASE_NAME = 'data_gc.db'
|
725 |
|
726 |
+
def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
|
727 |
conn = sqlite3.connect(db_name)
|
728 |
cursor = conn.cursor()
|
729 |
|
|
|
730 |
email = user_data.get('email')
|
731 |
if not email:
|
732 |
logging.error(f"User data missing email: {user_data}")
|
733 |
return
|
734 |
|
735 |
logging.debug(f"Processing user with email: {email}")
|
736 |
+
|
737 |
+
cursor.execute("SELECT web_st, ws_st, b_mess FROM contacts WHERE email = ?", (email,))
|
|
|
738 |
user = cursor.fetchone()
|
739 |
logging.debug(f"User found: {user}")
|
740 |
|
741 |
+
current_web_st = user[0] if user else None
|
742 |
+
current_ws_st = user[1] if user else None
|
743 |
+
current_messages = user[2] if user else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
744 |
|
|
|
745 |
transformed_data = {}
|
746 |
for json_key, db_column in mapping_template.items():
|
747 |
value = user_data.get(json_key, "")
|
|
|
748 |
if isinstance(value, list):
|
|
|
749 |
if all(isinstance(item, str) for item in value):
|
750 |
+
transformed_data[db_column] = "; ".join(value)
|
751 |
else:
|
752 |
logging.error(f"Expected list of strings for key {json_key}, but got: {value}")
|
753 |
transformed_data[db_column] = ""
|
|
|
755 |
transformed_data[db_column] = str(value)
|
756 |
logging.debug(f"Transformed data: {transformed_data}")
|
757 |
|
|
|
758 |
required_fields = [
|
759 |
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
760 |
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
761 |
+
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "gc_url",
|
762 |
"key_pr", "n_con", "canal", "data_t", 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'
|
763 |
]
|
764 |
for field in required_fields:
|
|
|
766 |
transformed_data[field] = ""
|
767 |
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
768 |
|
|
|
769 |
if 'phone' in user_data:
|
770 |
phone = user_data['phone']
|
771 |
if phone.startswith('+'):
|
|
|
773 |
transformed_data['phone'] = phone
|
774 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
775 |
|
776 |
+
# Проверяем наличие ws_st в базе данных и не перезаписываем его, если оно уже существует
|
777 |
+
if current_ws_st is not None and current_ws_st != "":
|
|
|
|
|
|
|
778 |
transformed_data['ws_st'] = current_ws_st
|
779 |
else:
|
780 |
transformed_data['ws_st'] = user_data.get('ws_st', "")
|
781 |
|
782 |
+
# Увеличиваем значение web_st на 1, если пользователь уже существует в базе данных
|
783 |
+
if current_web_st is not None and current_web_st != "":
|
784 |
+
transformed_data['web_st'] = int(current_web_st) + 1
|
785 |
+
else:
|
786 |
+
transformed_data['web_st'] = 1
|
787 |
+
|
788 |
+
new_messages = transformed_data.get('b_mess', "")
|
789 |
+
if current_messages:
|
790 |
+
transformed_data['b_mess'] = current_messages + "; " + new_messages
|
791 |
+
else:
|
792 |
+
transformed_data['b_mess'] = new_messages
|
793 |
+
logging.debug(f"Transformed data after message processing: {transformed_data}")
|
794 |
+
|
795 |
if user:
|
796 |
update_query = "UPDATE contacts SET "
|
797 |
update_values = []
|
|
|
810 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
811 |
cursor.execute(insert_query, insert_values)
|
812 |
|
|
|
813 |
conn.commit()
|
814 |
conn.close()
|
815 |
logging.debug(f"User with email {email} processed successfully")
|
816 |
|
817 |
+
# Отправка данных в Google Forms
|
818 |
+
send_to_google_forms(transformed_data, gog_url)
|
819 |
+
|
820 |
|
821 |
@app.route('/send_request', methods=['POST'])
|
822 |
def send_request():
|