DmitrMakeev commited on
Commit
83897a4
1 Parent(s): 73ce73e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -90
app.py CHANGED
@@ -1636,6 +1636,18 @@ def add_data_ver_cur():
1636
 
1637
 
1638
 
 
 
 
 
 
 
 
 
 
 
 
 
1639
 
1640
 
1641
 
@@ -1661,71 +1673,62 @@ def verify_phone_number(phone_number):
1661
  def parse_csv_data(data):
1662
  parsed_data = []
1663
  for item in data:
1664
- headers = list(item.keys())
1665
- row = list(item.values())
1666
- parsed_data.append(dict(zip(headers, row)))
 
1667
  return parsed_data
1668
 
1669
- def clean_phone_number(phone_number):
1670
- return re.sub(r'\D', '', phone_number)
1671
-
1672
  def insert_data(data, verify_phone, add_curator):
1673
  global current_curator_index
1674
- for db_name in DATABASES:
1675
- conn = sqlite3.connect(db_name)
1676
- cursor = conn.cursor()
1677
-
1678
- for row in data:
1679
- name = row.get('Имя', '')
1680
- phone = row.get('WhatsApp', '').lstrip('+')
1681
- email = row.get('Email', '')
1682
- data_t = row.get('Дата', '').strip('"')
1683
 
1684
- # Очистка номера телефона
1685
- phone = clean_phone_number(phone)
 
 
 
1686
 
1687
- cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1688
- user_exists = cursor.fetchone()
1689
 
1690
- if user_exists:
1691
- print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1692
- continue
1693
 
1694
- if add_curator == "1":
1695
- curator = curators[current_curator_index]
1696
- current_curator_index = (current_curator_index + 1) % len(curators)
1697
- else:
1698
- curator = row.get('Куратор', '')
1699
 
1700
- if verify_phone == "1":
1701
- ws_st = verify_phone_number(phone)
1702
- else:
1703
- ws_st = row.get('ws_st', '')
1704
 
1705
- 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', 'gc_url', 'key_pr', 'n_con', 'canal', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']
1706
- values = [name, phone, email, row.get('ВКонтакте', ''), row.get('Телеграм', ''), ws_st, row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('Город', ''), 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('gc_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', '')]
1707
 
1708
- placeholders = ', '.join(['?' for _ in columns])
1709
- columns_str = ', '.join(columns)
1710
 
1711
- query = f'''
1712
- INSERT INTO contacts ({columns_str})
1713
- VALUES ({placeholders})
1714
- '''
1715
 
1716
- try:
1717
- cursor.execute(query, values)
1718
- # Отправка данных в Google Forms
1719
- user_data = dict(zip(columns, values))
1720
- send_to_google_forms(user_data, gog_url)
1721
- except Exception as e:
1722
- print(f"Error inserting row: {row}")
1723
- print(f"Error message: {str(e)}")
1724
- conn.rollback()
1725
- raise
1726
-
1727
- conn.commit()
1728
- conn.close()
1729
 
1730
  @app.route('/upload_csv', methods=['POST'])
1731
  def upload_csv():
@@ -1747,44 +1750,6 @@ def upload_csv():
1747
  return jsonify({"message": "Data uploaded and inserted successfully"})
1748
  return jsonify({"error": "Invalid file format"}), 400
1749
 
1750
- @app.route('/upl_csv', methods=['GET'])
1751
- def se_upl_csv():
1752
- api_sys_control = request.args.get('api_sys')
1753
- if api_sys_control != api_key_sys:
1754
- return "EUR 22", 200
1755
- return render_template('upl_csv.html')
1756
-
1757
- @app.route('/download_csv', methods=['GET'])
1758
- def download_csv():
1759
- data = io.StringIO()
1760
- writer = csv.writer(data)
1761
- writer.writerow(['Имя', 'WhatsApp', 'Email', 'Город']) # Заголовки для CSV
1762
- # Заполнение CSV данными, здесь можно добавить логику получения данных
1763
- writer.writerow(['Олег', '79033456555', '[email protected]', 'Москва'])
1764
-
1765
- data.seek(0)
1766
- return send_file(data, mimetype='text/csv', attachment_filename='download.csv', as_attachment=True)
1767
-
1768
-
1769
-
1770
-
1771
-
1772
-
1773
-
1774
-
1775
-
1776
-
1777
-
1778
-
1779
-
1780
-
1781
-
1782
-
1783
-
1784
-
1785
-
1786
-
1787
-
1788
 
1789
 
1790
 
 
1636
 
1637
 
1638
 
1639
+
1640
+
1641
+
1642
+
1643
+
1644
+
1645
+
1646
+
1647
+
1648
+
1649
+
1650
+
1651
 
1652
 
1653
 
 
1673
  def parse_csv_data(data):
1674
  parsed_data = []
1675
  for item in data:
1676
+ for key, value in item.items():
1677
+ headers = key.split(';')
1678
+ row = value.split(';')
1679
+ parsed_data.append(dict(zip(headers, row)))
1680
  return parsed_data
1681
 
 
 
 
1682
  def insert_data(data, verify_phone, add_curator):
1683
  global current_curator_index
1684
+ conn = sqlite3.connect(DATABASE2)
1685
+ cursor = conn.cursor()
 
 
 
 
 
 
 
1686
 
1687
+ for row in data:
1688
+ name = row.get('Name', '')
1689
+ phone = row.get('Phone', '').lstrip('+')
1690
+ email = row.get('Email', '')
1691
+ data_t = row.get('Date', '').strip('"')
1692
 
1693
+ cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1694
+ user_exists = cursor.fetchone()
1695
 
1696
+ if user_exists:
1697
+ print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1698
+ continue
1699
 
1700
+ if add_curator == "1":
1701
+ curator = curators[current_curator_index]
1702
+ current_curator_index = (current_curator_index + 1) % len(curators)
1703
+ else:
1704
+ curator = row.get('curator', '')
1705
 
1706
+ if verify_phone == "1":
1707
+ ws_st = verify_phone_number(phone)
1708
+ else:
1709
+ ws_st = row.get('ws_st', '')
1710
 
1711
+ 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']
1712
+ 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', '')]
1713
 
1714
+ placeholders = ', '.join(['?' for _ in columns])
1715
+ columns_str = ', '.join(columns)
1716
 
1717
+ query = f'''
1718
+ INSERT INTO contacts ({columns_str})
1719
+ VALUES ({placeholders})
1720
+ '''
1721
 
1722
+ try:
1723
+ cursor.execute(query, values)
1724
+ except Exception as e:
1725
+ print(f"Error inserting row: {row}")
1726
+ print(f"Error message: {str(e)}")
1727
+ conn.rollback()
1728
+ raise
1729
+
1730
+ conn.commit()
1731
+ conn.close()
 
 
 
1732
 
1733
  @app.route('/upload_csv', methods=['POST'])
1734
  def upload_csv():
 
1750
  return jsonify({"message": "Data uploaded and inserted successfully"})
1751
  return jsonify({"error": "Invalid file format"}), 400
1752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1753
 
1754
 
1755