DmitrMakeev
commited on
Commit
•
888e945
1
Parent(s):
3d4d1aa
Update app.py
Browse files
app.py
CHANGED
@@ -893,47 +893,15 @@ def send_get_request():
|
|
893 |
messages_user = messages_json.get(user_id, []) # Получение сообщений пользователя
|
894 |
user_data['messages'] = messages_user # Добавление сообщений в данные пользователя
|
895 |
|
896 |
-
# Проверка наличия email и его уникальности
|
897 |
email = user_data.get('email')
|
898 |
if email and email not in processed_emails:
|
899 |
-
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
900 |
-
processed_emails.add(email)
|
901 |
|
902 |
return jsonify({'status': 'User data saved successfully'})
|
903 |
else:
|
904 |
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
905 |
|
906 |
-
def update_or_insert_user(db_name, user_data, mapping_template):
|
907 |
-
""" Обновляет или вставляет данные пользователя в базу данных. """
|
908 |
-
connection = sqlite3.connect(db_name)
|
909 |
-
cursor = connection.cursor()
|
910 |
-
|
911 |
-
# Преобразование данных пользователя на основе шаблона сопоставления
|
912 |
-
transformed_data = {}
|
913 |
-
for json_key, db_column in mapping_template.items():
|
914 |
-
value = user_data.get(json_key, "")
|
915 |
-
if isinstance(value, list):
|
916 |
-
value = ",".join(value)
|
917 |
-
transformed_data[db_column] = value
|
918 |
-
|
919 |
-
# Вставка или обновление записи пользователя
|
920 |
-
try:
|
921 |
-
cursor.execute('''INSERT INTO contacts (email, name, phone, pr1, pr2, pr3, pr4, pr5, messages)
|
922 |
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''',
|
923 |
-
(transformed_data.get('email'), transformed_data.get('name'), transformed_data.get('phone'),
|
924 |
-
transformed_data.get('pr1'), transformed_data.get('pr2'), transformed_data.get('pr3'),
|
925 |
-
transformed_data.get('pr4'), transformed_data.get('pr5'), json.dumps(user_data.get('messages'))))
|
926 |
-
connection.commit()
|
927 |
-
except sqlite3.IntegrityError:
|
928 |
-
cursor.execute('''UPDATE contacts SET name = ?, phone = ?, pr1 = ?, pr2 = ?, pr3 = ?, pr4 = ?, pr5 = ?, messages = ?
|
929 |
-
WHERE email = ?''',
|
930 |
-
(transformed_data.get('name'), transformed_data.get('phone'), transformed_data.get('pr1'),
|
931 |
-
transformed_data.get('pr2'), transformed_data.get('pr3'), transformed_data.get('pr4'),
|
932 |
-
transformed_data.get('pr5'), json.dumps(user_data.get('messages')), transformed_data.get('email')))
|
933 |
-
connection.commit()
|
934 |
-
finally:
|
935 |
-
connection.close()
|
936 |
-
|
937 |
|
938 |
|
939 |
|
|
|
893 |
messages_user = messages_json.get(user_id, []) # Получение сообщений пользователя
|
894 |
user_data['messages'] = messages_user # Добавление сообщений в данные пользователя
|
895 |
|
|
|
896 |
email = user_data.get('email')
|
897 |
if email and email not in processed_emails:
|
898 |
+
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
899 |
+
processed_emails.add(email)
|
900 |
|
901 |
return jsonify({'status': 'User data saved successfully'})
|
902 |
else:
|
903 |
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
904 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
905 |
|
906 |
|
907 |
|