DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -786,28 +786,23 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
786 |
conn = sqlite3.connect(db_name)
|
787 |
cursor = conn.cursor()
|
788 |
|
789 |
-
# Получение email пользователя из данных
|
790 |
email = user_data.get('email')
|
791 |
if not email:
|
792 |
logging.error(f"User data missing email: {user_data}")
|
793 |
return
|
794 |
|
795 |
logging.debug(f"Processing user with email: {email}")
|
796 |
-
|
797 |
-
# Проверка существования пользователя в базе данных по email
|
798 |
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
799 |
user = cursor.fetchone()
|
800 |
logging.debug(f"User found: {user}")
|
801 |
|
802 |
-
|
803 |
-
web_st_value = 1 # Инициализация значения web_st
|
804 |
if user:
|
805 |
-
# Проверка текущего значения web_st и его инкрементация
|
806 |
current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
|
807 |
web_st_value = int(current_web_st) + 1
|
808 |
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
809 |
|
810 |
-
# Обновление значения web_st
|
811 |
cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
|
812 |
conn.commit()
|
813 |
conn.close()
|
@@ -816,21 +811,18 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
816 |
conn.close()
|
817 |
logging.debug(f"User {email} not found, proceeding with insert")
|
818 |
|
819 |
-
# Открываем соединение снова для остальных операций
|
820 |
conn = sqlite3.connect(db_name)
|
821 |
cursor = conn.cursor()
|
822 |
|
823 |
-
# Преобразование данных пользователя на основе шаблона сопоставления
|
824 |
transformed_data = {}
|
825 |
for json_key, db_column in mapping_template.items():
|
826 |
value = user_data.get(json_key, "")
|
827 |
if isinstance(value, list):
|
828 |
-
transformed_data[db_column] =
|
829 |
else:
|
830 |
transformed_data[db_column] = str(value)
|
831 |
logging.debug(f"Transformed data: {transformed_data}")
|
832 |
|
833 |
-
# Заполнение обязательных полей значениями по умолчанию
|
834 |
required_fields = [
|
835 |
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
836 |
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
@@ -842,7 +834,6 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
842 |
transformed_data[field] = ""
|
843 |
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
844 |
|
845 |
-
# Обработка номера телефона, если он есть
|
846 |
if 'phone' in user_data:
|
847 |
phone = user_data['phone']
|
848 |
if phone.startswith('+'):
|
@@ -850,10 +841,8 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
850 |
transformed_data['phone'] = phone
|
851 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
852 |
|
853 |
-
# Добавление значения web_st в данные для вставки
|
854 |
transformed_data['web_st'] = web_st_value
|
855 |
|
856 |
-
# Обновление данных пользователя в базе данных
|
857 |
if user:
|
858 |
update_query = "UPDATE contacts SET "
|
859 |
update_values = []
|
@@ -872,11 +861,12 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
872 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
873 |
cursor.execute(insert_query, insert_values)
|
874 |
|
875 |
-
# Подтверждение изменений и закрытие соединения
|
876 |
conn.commit()
|
877 |
conn.close()
|
878 |
logging.debug(f"User with email {email} processed successfully")
|
879 |
|
|
|
|
|
880 |
@app.route('/send_get_request', methods=['GET'])
|
881 |
def send_get_request():
|
882 |
token = request.args.get('token')
|
@@ -905,10 +895,9 @@ def send_get_request():
|
|
905 |
|
906 |
users_meta = report_json.get('usersMeta', {})
|
907 |
|
908 |
-
# Обновление или добавление каждого пользователя в базу данных data_gc.db
|
909 |
processed_emails = set()
|
910 |
for user_id, user_data in users_meta.items():
|
911 |
-
user_data['messages'] =
|
912 |
email = user_data.get('email')
|
913 |
if email not in processed_emails:
|
914 |
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
|
|
786 |
conn = sqlite3.connect(db_name)
|
787 |
cursor = conn.cursor()
|
788 |
|
|
|
789 |
email = user_data.get('email')
|
790 |
if not email:
|
791 |
logging.error(f"User data missing email: {user_data}")
|
792 |
return
|
793 |
|
794 |
logging.debug(f"Processing user with email: {email}")
|
795 |
+
|
|
|
796 |
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
797 |
user = cursor.fetchone()
|
798 |
logging.debug(f"User found: {user}")
|
799 |
|
800 |
+
web_st_value = 1
|
|
|
801 |
if user:
|
|
|
802 |
current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
|
803 |
web_st_value = int(current_web_st) + 1
|
804 |
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
805 |
|
|
|
806 |
cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
|
807 |
conn.commit()
|
808 |
conn.close()
|
|
|
811 |
conn.close()
|
812 |
logging.debug(f"User {email} not found, proceeding with insert")
|
813 |
|
|
|
814 |
conn = sqlite3.connect(db_name)
|
815 |
cursor = conn.cursor()
|
816 |
|
|
|
817 |
transformed_data = {}
|
818 |
for json_key, db_column in mapping_template.items():
|
819 |
value = user_data.get(json_key, "")
|
820 |
if isinstance(value, list):
|
821 |
+
transformed_data[db_column] = json.dumps(value) if value else ""
|
822 |
else:
|
823 |
transformed_data[db_column] = str(value)
|
824 |
logging.debug(f"Transformed data: {transformed_data}")
|
825 |
|
|
|
826 |
required_fields = [
|
827 |
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
828 |
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
|
|
834 |
transformed_data[field] = ""
|
835 |
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
836 |
|
|
|
837 |
if 'phone' in user_data:
|
838 |
phone = user_data['phone']
|
839 |
if phone.startswith('+'):
|
|
|
841 |
transformed_data['phone'] = phone
|
842 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
843 |
|
|
|
844 |
transformed_data['web_st'] = web_st_value
|
845 |
|
|
|
846 |
if user:
|
847 |
update_query = "UPDATE contacts SET "
|
848 |
update_values = []
|
|
|
861 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
862 |
cursor.execute(insert_query, insert_values)
|
863 |
|
|
|
864 |
conn.commit()
|
865 |
conn.close()
|
866 |
logging.debug(f"User with email {email} processed successfully")
|
867 |
|
868 |
+
app = Flask(__name__)
|
869 |
+
|
870 |
@app.route('/send_get_request', methods=['GET'])
|
871 |
def send_get_request():
|
872 |
token = request.args.get('token')
|
|
|
895 |
|
896 |
users_meta = report_json.get('usersMeta', {})
|
897 |
|
|
|
898 |
processed_emails = set()
|
899 |
for user_id, user_data in users_meta.items():
|
900 |
+
user_data['messages'] = messages.get(user_id, [])
|
901 |
email = user_data.get('email')
|
902 |
if email not in processed_emails:
|
903 |
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|