DmitrMakeev
commited on
Commit
•
52128d4
1
Parent(s):
e766c41
Update app.py
Browse files
app.py
CHANGED
@@ -771,54 +771,56 @@ def send_request():
|
|
771 |
|
772 |
DATABASE_NAME = 'data_gc.db'
|
773 |
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
cursor.execute(update_query, list(update_data.values()) + [email])
|
791 |
else:
|
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 |
-
|
817 |
-
|
818 |
-
|
|
|
|
|
|
|
819 |
|
820 |
-
|
821 |
-
|
822 |
|
823 |
@app.route('/send_get_request', methods=['GET'])
|
824 |
def send_get_request():
|
@@ -848,10 +850,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(user_data
|
855 |
|
856 |
return jsonify({'status': 'User data saved successfully'})
|
857 |
else:
|
@@ -873,9 +875,6 @@ def send_get_request():
|
|
873 |
|
874 |
|
875 |
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
@app.route('/biz_v', methods=['GET'])
|
880 |
def biz_v():
|
881 |
api_sys_control = request.args.get('api_sys')
|
|
|
771 |
|
772 |
DATABASE_NAME = 'data_gc.db'
|
773 |
|
774 |
+
DATABASE_NAME = 'data_gc.db'
|
775 |
+
|
776 |
+
def update_or_insert_user(db_name, user_data, mapping_template):
|
777 |
+
conn = sqlite3.connect(db_name)
|
778 |
+
cursor = conn.cursor()
|
779 |
+
|
780 |
+
email = user_data.get('email')
|
781 |
+
|
782 |
+
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
783 |
+
user = cursor.fetchone()
|
784 |
+
|
785 |
+
transformed_data = {}
|
786 |
+
for json_key, db_column in mapping_template.items():
|
787 |
+
value = user_data.get(json_key, "")
|
788 |
+
if isinstance(value, list):
|
789 |
+
transformed_data[db_column] = "1" if value else "0"
|
|
|
790 |
else:
|
791 |
+
transformed_data[db_column] = str(value)
|
792 |
+
|
793 |
+
required_fields = [
|
794 |
+
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
795 |
+
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
796 |
+
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
|
797 |
+
"key_pr", "n_con", "canal", "data_t"
|
798 |
+
]
|
799 |
+
|
800 |
+
for field in required_fields:
|
801 |
+
if field not in transformed_data:
|
802 |
+
transformed_data[field] = ""
|
803 |
+
|
804 |
+
if user:
|
805 |
+
web_st = int(user[0]) + 1
|
806 |
+
|
807 |
+
update_query = "UPDATE contacts SET "
|
808 |
+
update_values = []
|
809 |
+
for column, value in transformed_data.items():
|
810 |
+
update_query += f"{column} = ?, "
|
811 |
+
update_values.append(value)
|
812 |
+
update_query += "web_st = ? WHERE email = ?"
|
813 |
+
update_values.extend([web_st, email])
|
814 |
+
cursor.execute(update_query, update_values)
|
815 |
+
else:
|
816 |
+
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
817 |
+
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
818 |
+
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
819 |
+
insert_values = list(transformed_data.values()) + [0]
|
820 |
+
cursor.execute(insert_query, insert_values)
|
821 |
|
822 |
+
conn.commit()
|
823 |
+
conn.close()
|
824 |
|
825 |
@app.route('/send_get_request', methods=['GET'])
|
826 |
def send_get_request():
|
|
|
850 |
|
851 |
users_meta = report_json.get('usersMeta', {})
|
852 |
|
853 |
+
# Обновление или добавление каждого пользователя в базу данных data_gc.db
|
854 |
for user_id, user_data in users_meta.items():
|
855 |
user_data['messages'] = messages_json
|
856 |
+
update_or_insert_user(DATABASE_NAME, user_data, template)
|
857 |
|
858 |
return jsonify({'status': 'User data saved successfully'})
|
859 |
else:
|
|
|
875 |
|
876 |
|
877 |
|
|
|
|
|
|
|
878 |
@app.route('/biz_v', methods=['GET'])
|
879 |
def biz_v():
|
880 |
api_sys_control = request.args.get('api_sys')
|