DmitrMakeev
commited on
Commit
•
259ebdf
1
Parent(s):
e40dcb7
Update app.py
Browse files
app.py
CHANGED
@@ -772,39 +772,42 @@ def send_request():
|
|
772 |
DATABASE_NAME = 'data_gc.db'
|
773 |
|
774 |
def update_or_insert_user(db_name, user_data, mapping_template):
|
775 |
-
conn = sqlite3.connect(db_name
|
776 |
cursor = conn.cursor()
|
777 |
|
778 |
email = user_data.get('email')
|
779 |
-
vk_id = user_data.get('vk_id', 'default_vk_id') # Используйте значение по умолчанию для vk_id
|
780 |
-
|
781 |
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
782 |
user = cursor.fetchone()
|
783 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
784 |
if user:
|
785 |
web_st_value = int(user[0]) + 1
|
|
|
786 |
update_query = "UPDATE contacts SET "
|
787 |
update_values = []
|
788 |
-
for
|
789 |
-
|
790 |
-
|
791 |
-
value = "-1" if value else "0"
|
792 |
-
update_query += f"{db_column} = ?, "
|
793 |
-
update_values.append(str(value))
|
794 |
update_query += "web_st = ? WHERE email = ?"
|
795 |
update_values.extend([web_st_value, email])
|
796 |
cursor.execute(update_query, update_values)
|
797 |
else:
|
798 |
-
columns = ', '.join(
|
799 |
-
placeholders = ', '.join('?' for _ in
|
800 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
801 |
-
insert_values = []
|
802 |
-
for json_key, db_column in mapping_template.items():
|
803 |
-
value = user_data.get(json_key, "")
|
804 |
-
if isinstance(value, list):
|
805 |
-
value = "-1" if value else "0"
|
806 |
-
insert_values.append(str(value))
|
807 |
-
insert_values.extend([0, vk_id])
|
808 |
cursor.execute(insert_query, insert_values)
|
809 |
|
810 |
conn.commit()
|
@@ -864,7 +867,6 @@ def send_get_request():
|
|
864 |
|
865 |
|
866 |
|
867 |
-
|
868 |
|
869 |
|
870 |
@app.route('/biz_v', methods=['GET'])
|
|
|
772 |
DATABASE_NAME = 'data_gc.db'
|
773 |
|
774 |
def update_or_insert_user(db_name, user_data, mapping_template):
|
775 |
+
conn = sqlite3.connect(db_name)
|
776 |
cursor = conn.cursor()
|
777 |
|
778 |
email = user_data.get('email')
|
|
|
|
|
779 |
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
780 |
user = cursor.fetchone()
|
781 |
|
782 |
+
transformed_data = {db_column: user_data.get(json_key, "") for json_key, db_column in mapping_template.items()}
|
783 |
+
|
784 |
+
required_fields = [
|
785 |
+
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
786 |
+
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
787 |
+
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
|
788 |
+
"key_pr", "n_con", "canal", "data_t"
|
789 |
+
]
|
790 |
+
|
791 |
+
for field in required_fields:
|
792 |
+
if field not in transformed_data:
|
793 |
+
transformed_data[field] = ""
|
794 |
+
|
795 |
if user:
|
796 |
web_st_value = int(user[0]) + 1
|
797 |
+
|
798 |
update_query = "UPDATE contacts SET "
|
799 |
update_values = []
|
800 |
+
for column, value in transformed_data.items():
|
801 |
+
update_query += f"{column} = ?, "
|
802 |
+
update_values.append(value)
|
|
|
|
|
|
|
803 |
update_query += "web_st = ? WHERE email = ?"
|
804 |
update_values.extend([web_st_value, email])
|
805 |
cursor.execute(update_query, update_values)
|
806 |
else:
|
807 |
+
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
808 |
+
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
809 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
810 |
+
insert_values = list(transformed_data.values()) + [0]
|
|
|
|
|
|
|
|
|
|
|
|
|
811 |
cursor.execute(insert_query, insert_values)
|
812 |
|
813 |
conn.commit()
|
|
|
867 |
|
868 |
|
869 |
|
|
|
870 |
|
871 |
|
872 |
@app.route('/biz_v', methods=['GET'])
|