DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -768,26 +768,25 @@ def send_request():
|
|
768 |
|
769 |
|
770 |
|
|
|
|
|
771 |
def update_or_insert_user(db_name, user_data, mapping_template):
|
772 |
conn = sqlite3.connect(db_name)
|
773 |
cursor = conn.cursor()
|
774 |
-
|
775 |
email = user_data.get('email')
|
776 |
|
777 |
-
# Проверка существования пользователя
|
778 |
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
779 |
user = cursor.fetchone()
|
780 |
|
781 |
-
# Преобразование данных пользователя на основе шаблона
|
782 |
transformed_data = {}
|
783 |
for json_key, db_column in mapping_template.items():
|
784 |
value = user_data.get(json_key, "")
|
785 |
if isinstance(value, list):
|
786 |
transformed_data[db_column] = "-1" if value else "0"
|
787 |
else:
|
788 |
-
transformed_data[db_column] = str(value)
|
789 |
|
790 |
-
# Установите значения по умолчанию для всех обязательных полей
|
791 |
required_fields = [
|
792 |
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
793 |
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
@@ -800,7 +799,6 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
800 |
transformed_data[field] = ""
|
801 |
|
802 |
if user:
|
803 |
-
# Обновление существующего пользователя
|
804 |
web_st_value = user[0] + 1
|
805 |
update_query = "UPDATE contacts SET "
|
806 |
update_values = []
|
@@ -811,7 +809,6 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
811 |
update_values.extend([web_st_value, email])
|
812 |
cursor.execute(update_query, update_values)
|
813 |
else:
|
814 |
-
# Добавление нового пользователя
|
815 |
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
816 |
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
817 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
@@ -849,12 +846,10 @@ def send_get_request():
|
|
849 |
|
850 |
users_meta = report_json.get('usersMeta', {})
|
851 |
|
852 |
-
# Обновление или добавление каждого пользователя
|
853 |
for user_id, user_data in users_meta.items():
|
854 |
user_data['messages'] = messages_json
|
855 |
-
|
856 |
-
for db in DATABASES:
|
857 |
-
update_or_insert_user(db, user_data, template)
|
858 |
|
859 |
return jsonify({'status': 'User data saved successfully'})
|
860 |
else:
|
@@ -880,6 +875,7 @@ def send_get_request():
|
|
880 |
|
881 |
|
882 |
|
|
|
883 |
|
884 |
|
885 |
@app.route('/biz_v', methods=['GET'])
|
|
|
768 |
|
769 |
|
770 |
|
771 |
+
DATABASE_NAME = 'data_gc.db'
|
772 |
+
|
773 |
def update_or_insert_user(db_name, user_data, mapping_template):
|
774 |
conn = sqlite3.connect(db_name)
|
775 |
cursor = conn.cursor()
|
776 |
+
|
777 |
email = user_data.get('email')
|
778 |
|
|
|
779 |
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
780 |
user = cursor.fetchone()
|
781 |
|
|
|
782 |
transformed_data = {}
|
783 |
for json_key, db_column in mapping_template.items():
|
784 |
value = user_data.get(json_key, "")
|
785 |
if isinstance(value, list):
|
786 |
transformed_data[db_column] = "-1" if value else "0"
|
787 |
else:
|
788 |
+
transformed_data[db_column] = str(value)
|
789 |
|
|
|
790 |
required_fields = [
|
791 |
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
792 |
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
|
|
799 |
transformed_data[field] = ""
|
800 |
|
801 |
if user:
|
|
|
802 |
web_st_value = user[0] + 1
|
803 |
update_query = "UPDATE contacts SET "
|
804 |
update_values = []
|
|
|
809 |
update_values.extend([web_st_value, email])
|
810 |
cursor.execute(update_query, update_values)
|
811 |
else:
|
|
|
812 |
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
813 |
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
814 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
|
|
846 |
|
847 |
users_meta = report_json.get('usersMeta', {})
|
848 |
|
849 |
+
# Обновление или добавление каждого пользователя в базу данных data_gc.db
|
850 |
for user_id, user_data in users_meta.items():
|
851 |
user_data['messages'] = messages_json
|
852 |
+
update_or_insert_user(DATABASE_NAME, user_data, template)
|
|
|
|
|
853 |
|
854 |
return jsonify({'status': 'User data saved successfully'})
|
855 |
else:
|
|
|
875 |
|
876 |
|
877 |
|
878 |
+
|
879 |
|
880 |
|
881 |
@app.route('/biz_v', methods=['GET'])
|