DmitrMakeev commited on
Commit
81dbe8b
1 Parent(s): 2cccc76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1705,15 +1705,38 @@ def add_or_update_contact(contact_data):
1705
 
1706
  @app.route('/add_data_ver', methods=['GET'])
1707
  def add_data_ver():
 
1708
  contact_data = {
1709
- 'name': request.args.get('name'),
1710
- 'phone': request.args.get('phone'),
1711
- 'email': request.args.get('email')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1712
  }
1713
 
 
1714
  phone_verification_response = verify_phone_number(contact_data['phone'])
1715
  if phone_verification_response is not None:
1716
- contact_data['ws_st'] = phone_verification_response # Сохраняем значение в поле ws_st
1717
 
1718
  try:
1719
  add_or_update_contact(contact_data)
@@ -1722,6 +1745,37 @@ def add_data_ver():
1722
  logging.error(f"Error adding/updating contact: {e}")
1723
  return jsonify({'status': 'error', 'message': str(e)}), 500
1724
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1725
 
1726
 
1727
 
 
1705
 
1706
  @app.route('/add_data_ver', methods=['GET'])
1707
  def add_data_ver():
1708
+ # Define all possible fields
1709
  contact_data = {
1710
+ 'name': request.args.get('name', ''),
1711
+ 'phone': request.args.get('phone', ''),
1712
+ 'email': request.args.get('email', ''),
1713
+ 'ws_st': '', # Initialize ws_st and other optional fields
1714
+ 'fin_prog': '',
1715
+ 'b_city': '',
1716
+ 'b_fin': '',
1717
+ 'b_ban': '',
1718
+ 'b_ign': '',
1719
+ 'b_baners': '',
1720
+ 'b_butt': '',
1721
+ 'b_mess': '',
1722
+ 'shop_st': '',
1723
+ 'curator': '',
1724
+ 'pr1': '',
1725
+ 'pr2': '',
1726
+ 'pr3': '',
1727
+ 'pr4': '',
1728
+ 'pr5': '',
1729
+ 'ad_url': '',
1730
+ 'key_pr': '',
1731
+ 'n_con': '',
1732
+ 'canal': '',
1733
+ 'data_t': ''
1734
  }
1735
 
1736
+ # Verify phone number and update contact_data
1737
  phone_verification_response = verify_phone_number(contact_data['phone'])
1738
  if phone_verification_response is not None:
1739
+ contact_data['ws_st'] = phone_verification_response # Update ws_st with verification response
1740
 
1741
  try:
1742
  add_or_update_contact(contact_data)
 
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
 
1781