DmitrMakeev commited on
Commit
8f9b89a
1 Parent(s): 24ca449

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -737,9 +737,11 @@ def send_to_google_forms(user_data, gog_url):
737
 
738
 
739
  def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
 
740
  conn = sqlite3.connect(db_name)
741
  cursor = conn.cursor()
742
 
 
743
  email = user_data.get('email')
744
  if not email:
745
  logging.error(f"User data missing email: {user_data}")
@@ -747,6 +749,7 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
747
 
748
  logging.debug(f"Processing user with email: {email}")
749
 
 
750
  cursor.execute("SELECT web_st, ws_st, b_mess FROM contacts WHERE email = ?", (email,))
751
  user = cursor.fetchone()
752
  logging.debug(f"User found: {user}")
@@ -756,6 +759,7 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
756
  current_messages = user[2] if user else ""
757
  logging.debug(f"Current web_st: {current_web_st}, current_ws_st: {current_ws_st}, current_messages: {current_messages}")
758
 
 
759
  transformed_data = {}
760
  for json_key, db_column in mapping_template.items():
761
  value = user_data.get(json_key, "")
@@ -769,6 +773,7 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
769
  transformed_data[db_column] = str(value)
770
  logging.debug(f"Transformed data: {transformed_data}")
771
 
 
772
  required_fields = [
773
  "vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
774
  "b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
@@ -780,6 +785,7 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
780
  transformed_data[field] = ""
781
  logging.debug(f"Transformed data after adding required fields: {transformed_data}")
782
 
 
783
  if 'phone' in user_data:
784
  phone = user_data['phone']
785
  if phone.startswith('+'):
@@ -790,13 +796,12 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
790
  # Столбцы, которые не нужно перезаписывать
791
  no_overwrite_columns = ['ws_st', 'curator'] # Добавляйте сюда столбцы
792
 
793
- # Проверяем наличие ws_st в базе данных и не перезаписываем его, если оно уже существует
794
  if current_ws_st is not None and current_ws_st != "":
795
  transformed_data['ws_st'] = current_ws_st
796
  else:
797
  transformed_data['ws_st'] = user_data.get('ws_st', "")
798
 
799
- # Увеличиваем значение web_st на 1, если пользователь уже существует в базе данных
800
  if current_web_st is not None and current_web_st != "":
801
  transformed_data['web_st'] = int(current_web_st) + 1
802
  else:
@@ -809,11 +814,12 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
809
  transformed_data['b_mess'] = new_messages
810
  logging.debug(f"Transformed data after message processing: {transformed_data}")
811
 
 
812
  if user:
813
  update_query = "UPDATE contacts SET "
814
  update_values = []
815
  for column, value in transformed_data.items():
816
- if column not in no_overwrite_columns:
817
  update_query += f"{column} = ?, "
818
  update_values.append(value)
819
  update_query = update_query.rstrip(", ") + " WHERE email = ?"
@@ -829,13 +835,17 @@ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
829
  cursor.execute(insert_query, insert_values)
830
 
831
  conn.commit()
 
 
 
 
 
 
832
  conn.close()
833
  logging.debug(f"User with email {email} processed successfully")
834
 
835
  # Отправка данных в Google Forms
836
- send_to_google_forms(transformed_data, gog_url)
837
-
838
-
839
 
840
 
841
 
 
737
 
738
 
739
  def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
740
+ # Подключение к базе данных
741
  conn = sqlite3.connect(db_name)
742
  cursor = conn.cursor()
743
 
744
+ # Получение email пользователя
745
  email = user_data.get('email')
746
  if not email:
747
  logging.error(f"User data missing email: {user_data}")
 
749
 
750
  logging.debug(f"Processing user with email: {email}")
751
 
752
+ # Извлечение текущих данных пользователя из базы данных
753
  cursor.execute("SELECT web_st, ws_st, b_mess FROM contacts WHERE email = ?", (email,))
754
  user = cursor.fetchone()
755
  logging.debug(f"User found: {user}")
 
759
  current_messages = user[2] if user else ""
760
  logging.debug(f"Current web_st: {current_web_st}, current_ws_st: {current_ws_st}, current_messages: {current_messages}")
761
 
762
+ # Трансформация данных
763
  transformed_data = {}
764
  for json_key, db_column in mapping_template.items():
765
  value = user_data.get(json_key, "")
 
773
  transformed_data[db_column] = str(value)
774
  logging.debug(f"Transformed data: {transformed_data}")
775
 
776
+ # Добавление обязательных полей
777
  required_fields = [
778
  "vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
779
  "b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
 
785
  transformed_data[field] = ""
786
  logging.debug(f"Transformed data after adding required fields: {transformed_data}")
787
 
788
+ # Обработка номера телефона
789
  if 'phone' in user_data:
790
  phone = user_data['phone']
791
  if phone.startswith('+'):
 
796
  # Столбцы, которые не нужно перезаписывать
797
  no_overwrite_columns = ['ws_st', 'curator'] # Добавляйте сюда столбцы
798
 
799
+ # Проверка и подготовка значений столбцов
800
  if current_ws_st is not None and current_ws_st != "":
801
  transformed_data['ws_st'] = current_ws_st
802
  else:
803
  transformed_data['ws_st'] = user_data.get('ws_st', "")
804
 
 
805
  if current_web_st is not None and current_web_st != "":
806
  transformed_data['web_st'] = int(current_web_st) + 1
807
  else:
 
814
  transformed_data['b_mess'] = new_messages
815
  logging.debug(f"Transformed data after message processing: {transformed_data}")
816
 
817
+ # Запись данных в базу данных
818
  if user:
819
  update_query = "UPDATE contacts SET "
820
  update_values = []
821
  for column, value in transformed_data.items():
822
+ if column not in no_overwrite_columns: # Исключаем столбцы, которые не нужно перезаписывать
823
  update_query += f"{column} = ?, "
824
  update_values.append(value)
825
  update_query = update_query.rstrip(", ") + " WHERE email = ?"
 
835
  cursor.execute(insert_query, insert_values)
836
 
837
  conn.commit()
838
+
839
+ # Извлечение обновленных данных из базы данных
840
+ cursor.execute("SELECT * FROM contacts WHERE email = ?", (email,))
841
+ updated_user = cursor.fetchone()
842
+ updated_data = dict(zip([column[0] for column in cursor.description], updated_user))
843
+
844
  conn.close()
845
  logging.debug(f"User with email {email} processed successfully")
846
 
847
  # Отправка данных в Google Forms
848
+ send_to_google_forms(updated_data, gog_url)
 
 
849
 
850
 
851