Update app.py
Browse files
app.py
CHANGED
|
@@ -1688,17 +1688,21 @@ def add_or_update_contact(contact_data):
|
|
| 1688 |
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
| 1689 |
contact = cursor.fetchone()
|
| 1690 |
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
"
|
| 1694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1695 |
|
| 1696 |
if contact:
|
| 1697 |
-
update_query = "UPDATE contacts SET
|
| 1698 |
-
cursor.execute(update_query, (*
|
| 1699 |
else:
|
| 1700 |
-
insert_query = "INSERT INTO contacts (
|
| 1701 |
-
cursor.execute(insert_query, tuple(
|
| 1702 |
|
| 1703 |
conn.commit()
|
| 1704 |
conn.close()
|
|
@@ -1745,36 +1749,7 @@ def add_data_ver():
|
|
| 1745 |
logging.error(f"Error adding/updating contact: {e}")
|
| 1746 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 1747 |
|
| 1748 |
-
def add_or_update_contact(contact_data):
|
| 1749 |
-
conn = sqlite3.connect(DATABASE_NAME)
|
| 1750 |
-
cursor = conn.cursor()
|
| 1751 |
-
|
| 1752 |
-
email = contact_data.get('email')
|
| 1753 |
-
if not email:
|
| 1754 |
-
logging.error(f"Missing email in contact data: {contact_data}")
|
| 1755 |
-
return
|
| 1756 |
-
|
| 1757 |
-
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
| 1758 |
-
contact = cursor.fetchone()
|
| 1759 |
-
|
| 1760 |
-
# List all fields for updating or inserting
|
| 1761 |
-
fields = ["name", "phone", "email", "vk_id", "chat_id", "ws_st", "fin_prog", "b_city", "b_fin", "b_ban",
|
| 1762 |
-
"b_ign", "b_baners", "b_butt", "b_mess", "shop_st", "curator", "pr1", "pr2", "pr3", "pr4",
|
| 1763 |
-
"pr5", "ad_url", "key_pr", "n_con", "canal", "data_t"]
|
| 1764 |
-
|
| 1765 |
-
# Only include fields with non-empty values
|
| 1766 |
-
fields_to_update = [field for field in fields if contact_data.get(field, '') != '']
|
| 1767 |
-
placeholders = ", ".join([f"{field} = ?" for field in fields_to_update])
|
| 1768 |
-
|
| 1769 |
-
if contact:
|
| 1770 |
-
update_query = f"UPDATE contacts SET {placeholders} WHERE id = ?"
|
| 1771 |
-
cursor.execute(update_query, (*[contact_data[field] for field in fields_to_update], contact[0]))
|
| 1772 |
-
else:
|
| 1773 |
-
insert_query = f"INSERT INTO contacts ({', '.join(fields_to_update)}) VALUES ({', '.join(['?' for _ in fields_to_update])})"
|
| 1774 |
-
cursor.execute(insert_query, tuple(contact_data[field] for field in fields_to_update))
|
| 1775 |
|
| 1776 |
-
conn.commit()
|
| 1777 |
-
conn.close()
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
|
|
|
| 1688 |
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
| 1689 |
contact = cursor.fetchone()
|
| 1690 |
|
| 1691 |
+
# List all fields for updating or inserting
|
| 1692 |
+
fields = ["name", "phone", "email", "vk_id", "chat_id", "ws_st", "fin_prog", "b_city", "b_fin", "b_ban",
|
| 1693 |
+
"b_ign", "b_baners", "b_butt", "b_mess", "shop_st", "curator", "pr1", "pr2", "pr3", "pr4",
|
| 1694 |
+
"pr5", "ad_url", "key_pr", "n_con", "canal", "data_t"]
|
| 1695 |
+
|
| 1696 |
+
# Only include fields with non-empty values
|
| 1697 |
+
fields_to_update = [field for field in fields if contact_data.get(field, '') != '']
|
| 1698 |
+
placeholders = ", ".join([f"{field} = ?" for field in fields_to_update])
|
| 1699 |
|
| 1700 |
if contact:
|
| 1701 |
+
update_query = f"UPDATE contacts SET {placeholders} WHERE id = ?"
|
| 1702 |
+
cursor.execute(update_query, (*[contact_data[field] for field in fields_to_update], contact[0]))
|
| 1703 |
else:
|
| 1704 |
+
insert_query = f"INSERT INTO contacts ({', '.join(fields_to_update)}) VALUES ({', '.join(['?' for _ in fields_to_update])})"
|
| 1705 |
+
cursor.execute(insert_query, tuple(contact_data[field] for field in fields_to_update))
|
| 1706 |
|
| 1707 |
conn.commit()
|
| 1708 |
conn.close()
|
|
|
|
| 1749 |
logging.error(f"Error adding/updating contact: {e}")
|
| 1750 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 1751 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1752 |
|
|
|
|
|
|
|
| 1753 |
|
| 1754 |
|
| 1755 |
|