DmitrMakeev commited on
Commit
b05d0d1
1 Parent(s): a3dd876

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -57
app.py CHANGED
@@ -1677,18 +1677,15 @@ def add_data_ver_cur():
1677
  DATABASE2 = 'data_gc.db'
1678
 
1679
  def verify_phone_number(phone_number):
1680
- if verifikation_start == "1":
1681
- full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
1682
- payload = {"phoneNumber": phone_number}
1683
- headers = {'Content-Type': 'application/json'}
1684
- response = requests.post(full_url_ver, headers=headers, json=payload)
1685
- if response.status_code == 200:
1686
- response_body = response.json()
1687
- return response_body.get('existsWhatsapp', 'false')
1688
- else:
1689
- return "Error"
1690
  else:
1691
- return "false"
1692
 
1693
  def parse_csv_data(data):
1694
  parsed_data = []
@@ -1699,73 +1696,37 @@ def parse_csv_data(data):
1699
  parsed_data.append(dict(zip(headers, row)))
1700
  return parsed_data
1701
 
1702
- def insert_data(data):
1703
  global current_curator_index
1704
  conn = sqlite3.connect(DATABASE2)
1705
  cursor = conn.cursor()
1706
 
1707
  for row in data:
1708
- # Проверка наличия ключей перед их использованием и предоставление значений по умолчанию
1709
  name = row.get('Name', '')
1710
-
1711
- # Удаление ведущего плюсика из номера телефона, если он есть
1712
  phone = row.get('Phone', '').lstrip('+')
1713
-
1714
  email = row.get('Email', '')
1715
- vk_id = row.get('vk_id', '')
1716
- chat_id = row.get('chat_id', '')
1717
- ws_st = row.get('ws_st', '')
1718
- ws_stop = row.get('ws_stop', '')
1719
- web_st = row.get('web_st', 0)
1720
- fin_prog = row.get('fin_prog', 0)
1721
- b_city = row.get('b_city', '')
1722
- b_fin = row.get('b_fin', '')
1723
- b_ban = row.get('b_ban', '')
1724
- b_ign = row.get('b_ign', '')
1725
- b_baners = row.get('b_baners', '')
1726
- b_butt = row.get('b_butt', '')
1727
- b_mess = row.get('b_mess', '')
1728
- shop_st = row.get('shop_st', '')
1729
- curator = row.get('curator', '')
1730
- pr1 = row.get('pr1', '')
1731
- pr2 = row.get('pr2', '')
1732
- pr3 = row.get('pr3', '')
1733
- pr4 = row.get('pr4', '')
1734
- pr5 = row.get('pr5', '')
1735
- ad_url = row.get('ad_url', '')
1736
- key_pr = row.get('key_pr', '')
1737
- n_con = row.get('n_con', '')
1738
- canal = row.get('canal', '')
1739
- utm_source = row.get('utm_source', '')
1740
- utm_medium = row.get('utm_medium', '')
1741
- utm_campaign = row.get('utm_campaign', '')
1742
- utm_term = row.get('utm_term', '')
1743
- utm_content = row.get('utm_content', '')
1744
-
1745
- # Взятие даты из столбца CSV-таблицы и удаление кавычек
1746
  data_t = row.get('Date', '').strip('"')
1747
 
1748
- # Проверка существования пользователя в базе данных по email или телефону
1749
  cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1750
  user_exists = cursor.fetchone()
1751
 
1752
- # Если пользователь существует, пропускаем вставку
1753
  if user_exists:
1754
  print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1755
  continue
1756
 
1757
- # Добавление куратора, если curator_on_off == "1"
1758
- if curator_on_off == "1":
1759
  curator = curators[current_curator_index]
1760
  current_curator_index = (current_curator_index + 1) % len(curators)
 
 
1761
 
1762
- # Верификация номера телефона, если verifikation_start == "1"
1763
- if verifikation_start == "1":
1764
  ws_st = verify_phone_number(phone)
 
 
1765
 
1766
- # Создание динамического SQL-запроса для вставки данных
1767
  columns = ['name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog', 'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator', 'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']
1768
- values = [name, phone, email, vk_id, chat_id, ws_st, ws_stop, web_st, fin_prog, b_city, b_fin, b_ban, b_ign, b_baners, b_butt, b_mess, shop_st, curator, pr1, pr2, pr3, pr4, pr5, ad_url, key_pr, n_con, canal, data_t, utm_source, utm_medium, utm_campaign, utm_term, utm_content]
1769
 
1770
  placeholders = ', '.join(['?' for _ in columns])
1771
  columns_str = ', '.join(columns)
@@ -1798,7 +1759,11 @@ def upload_csv():
1798
  csv_input = csv.DictReader(stream)
1799
  data = [row for row in csv_input]
1800
  parsed_data = parse_csv_data(data)
1801
- insert_data(parsed_data)
 
 
 
 
1802
  return jsonify({"message": "Data uploaded and inserted successfully"})
1803
  return jsonify({"error": "Invalid file format"}), 400
1804
 
@@ -1807,7 +1772,6 @@ def upload_csv():
1807
 
1808
 
1809
 
1810
-
1811
  @app.route('/upl_csv', methods=['GET'])
1812
  def se_upl_csv():
1813
  api_sys_control = request.args.get('api_sys')
 
1677
  DATABASE2 = 'data_gc.db'
1678
 
1679
  def verify_phone_number(phone_number):
1680
+ full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
1681
+ payload = {"phoneNumber": phone_number}
1682
+ headers = {'Content-Type': 'application/json'}
1683
+ response = requests.post(full_url_ver, headers=headers, json=payload)
1684
+ if response.status_code == 200:
1685
+ response_body = response.json()
1686
+ return response_body.get('existsWhatsapp', 'false')
 
 
 
1687
  else:
1688
+ return "Error"
1689
 
1690
  def parse_csv_data(data):
1691
  parsed_data = []
 
1696
  parsed_data.append(dict(zip(headers, row)))
1697
  return parsed_data
1698
 
1699
+ def insert_data(data, verify_phone, add_curator):
1700
  global current_curator_index
1701
  conn = sqlite3.connect(DATABASE2)
1702
  cursor = conn.cursor()
1703
 
1704
  for row in data:
 
1705
  name = row.get('Name', '')
 
 
1706
  phone = row.get('Phone', '').lstrip('+')
 
1707
  email = row.get('Email', '')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1708
  data_t = row.get('Date', '').strip('"')
1709
 
 
1710
  cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1711
  user_exists = cursor.fetchone()
1712
 
 
1713
  if user_exists:
1714
  print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1715
  continue
1716
 
1717
+ if add_curator == "1":
 
1718
  curator = curators[current_curator_index]
1719
  current_curator_index = (current_curator_index + 1) % len(curators)
1720
+ else:
1721
+ curator = row.get('curator', '')
1722
 
1723
+ if verify_phone == "1":
 
1724
  ws_st = verify_phone_number(phone)
1725
+ else:
1726
+ ws_st = row.get('ws_st', '')
1727
 
 
1728
  columns = ['name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog', 'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator', 'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']
1729
+ values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), ws_st, row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('b_city', ''), row.get('b_fin', ''), row.get('b_ban', ''), row.get('b_ign', ''), row.get('b_baners', ''), row.get('b_butt', ''), row.get('b_mess', ''), row.get('shop_st', ''), curator, row.get('pr1', ''), row.get('pr2', ''), row.get('pr3', ''), row.get('pr4', ''), row.get('pr5', ''), row.get('ad_url', ''), row.get('key_pr', ''), row.get('n_con', ''), row.get('canal', ''), data_t, row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', '')]
1730
 
1731
  placeholders = ', '.join(['?' for _ in columns])
1732
  columns_str = ', '.join(columns)
 
1759
  csv_input = csv.DictReader(stream)
1760
  data = [row for row in csv_input]
1761
  parsed_data = parse_csv_data(data)
1762
+ verify_phone = request.form.get('verify_phone', '0')
1763
+ add_curator = request.form.get('add_curator', '0')
1764
+ print(f"Verify Phone: {verify_phone}")
1765
+ print(f"Add Curator: {add_curator}")
1766
+ insert_data(parsed_data, verify_phone, add_curator)
1767
  return jsonify({"message": "Data uploaded and inserted successfully"})
1768
  return jsonify({"error": "Invalid file format"}), 400
1769
 
 
1772
 
1773
 
1774
 
 
1775
  @app.route('/upl_csv', methods=['GET'])
1776
  def se_upl_csv():
1777
  api_sys_control = request.args.get('api_sys')