DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -771,54 +771,54 @@ def send_request():
|
|
771 |
|
772 |
DATABASE_NAME = 'data_gc.db'
|
773 |
|
774 |
-
def update_or_insert_user(
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
|
|
|
|
|
|
788 |
else:
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
817 |
-
insert_values = list(transformed_data.values()) + [0]
|
818 |
-
cursor.execute(insert_query, insert_values)
|
819 |
|
820 |
-
|
821 |
-
|
822 |
|
823 |
@app.route('/send_get_request', methods=['GET'])
|
824 |
def send_get_request():
|
@@ -848,10 +848,10 @@ def send_get_request():
|
|
848 |
|
849 |
users_meta = report_json.get('usersMeta', {})
|
850 |
|
851 |
-
# Обновление или добавление каждого пользователя в базу данных
|
852 |
for user_id, user_data in users_meta.items():
|
853 |
user_data['messages'] = messages_json
|
854 |
-
update_or_insert_user(
|
855 |
|
856 |
return jsonify({'status': 'User data saved successfully'})
|
857 |
else:
|
|
|
771 |
|
772 |
DATABASE_NAME = 'data_gc.db'
|
773 |
|
774 |
+
def update_or_insert_user(email, user_data):
|
775 |
+
for db in DATABASES:
|
776 |
+
conn = sqlite3.connect(db)
|
777 |
+
cursor = conn.cursor()
|
778 |
+
cursor.execute('SELECT * FROM contacts WHERE email = ?', (email,))
|
779 |
+
existing_user = cursor.fetchone()
|
780 |
+
|
781 |
+
if existing_user:
|
782 |
+
# Обновление данных пользователя
|
783 |
+
update_data = {}
|
784 |
+
for key, value in user_data.items():
|
785 |
+
if key in mapping_template:
|
786 |
+
update_data[mapping_template[key]] = value
|
787 |
+
update_data['b_baners'] = -1 if user_data.get('banners', []) else 0
|
788 |
+
update_data['b_butt'] = -1 if user_data.get('buttons', []) else 0
|
789 |
+
update_query = 'UPDATE contacts SET ' + ', '.join([f'{k} = ?' for k in update_data.keys()]) + ' WHERE email = ?'
|
790 |
+
cursor.execute(update_query, list(update_data.values()) + [email])
|
791 |
else:
|
792 |
+
# Добавление нового пользователя
|
793 |
+
insert_data = {
|
794 |
+
"name": user_data.get('username', ''),
|
795 |
+
"phone": user_data.get('phone', ''),
|
796 |
+
"email": user_data.get('email', ''),
|
797 |
+
"b_city": user_data.get('city', ''),
|
798 |
+
"b_fin": user_data.get('finished', ''),
|
799 |
+
"b_ban": user_data.get('ban', ''),
|
800 |
+
"b_ign": user_data.get('ignore', ''),
|
801 |
+
"b_baners": -1 if user_data.get('banners', []) else 0,
|
802 |
+
"b_butt": -1 if user_data.get('buttons', []) else 0,
|
803 |
+
"b_mess": user_data.get('messages', ''),
|
804 |
+
"shop_st": '',
|
805 |
+
"curator": '',
|
806 |
+
"pr1": '',
|
807 |
+
"pr2": '',
|
808 |
+
"pr3": '',
|
809 |
+
"pr4": '',
|
810 |
+
"pr5": '',
|
811 |
+
"ad_url": '',
|
812 |
+
"key_pr": '',
|
813 |
+
"n_con": '',
|
814 |
+
"canal": '',
|
815 |
+
"data_t": ''
|
816 |
+
}
|
817 |
+
insert_query = 'INSERT INTO contacts (' + ', '.join(insert_data.keys()) + ') VALUES (' + ', '.join(['?'] * len(insert_data)) + ')'
|
818 |
+
cursor.execute(insert_query, list(insert_data.values()))
|
|
|
|
|
|
|
819 |
|
820 |
+
conn.commit()
|
821 |
+
conn.close()
|
822 |
|
823 |
@app.route('/send_get_request', methods=['GET'])
|
824 |
def send_get_request():
|
|
|
848 |
|
849 |
users_meta = report_json.get('usersMeta', {})
|
850 |
|
851 |
+
# Обновление или добавление каждого пользователя в базу данных
|
852 |
for user_id, user_data in users_meta.items():
|
853 |
user_data['messages'] = messages_json
|
854 |
+
update_or_insert_user(user_data.get('email'), user_data)
|
855 |
|
856 |
return jsonify({'status': 'User data saved successfully'})
|
857 |
else:
|