Update app.py
Browse files
app.py
CHANGED
|
@@ -1671,59 +1671,61 @@ def clean_phone_number(phone_number):
|
|
| 1671 |
|
| 1672 |
def insert_data(data, verify_phone, add_curator):
|
| 1673 |
global current_curator_index
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
| 1677 |
-
|
| 1678 |
-
|
| 1679 |
-
|
| 1680 |
-
|
| 1681 |
-
|
|
|
|
| 1682 |
|
| 1683 |
-
|
|
|
|
| 1684 |
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
|
| 1688 |
-
|
| 1689 |
-
|
| 1690 |
-
|
| 1691 |
|
| 1692 |
-
|
| 1693 |
-
|
| 1694 |
-
|
| 1695 |
-
|
| 1696 |
-
|
| 1697 |
-
|
| 1698 |
-
if verify_phone == "1":
|
| 1699 |
-
ws_st = verify_phone_number(phone)
|
| 1700 |
-
else:
|
| 1701 |
-
ws_st = row.get('ws_st', '')
|
| 1702 |
|
| 1703 |
-
|
| 1704 |
-
|
| 1705 |
-
|
|
|
|
| 1706 |
|
| 1707 |
-
|
| 1708 |
-
|
| 1709 |
|
| 1710 |
-
|
| 1711 |
-
|
| 1712 |
-
VALUES ({placeholders})
|
| 1713 |
-
'''
|
| 1714 |
|
| 1715 |
-
|
| 1716 |
-
|
| 1717 |
-
|
| 1718 |
-
|
| 1719 |
-
except Exception as e:
|
| 1720 |
-
print(f"Error inserting row: {row}")
|
| 1721 |
-
print(f"Error message: {str(e)}")
|
| 1722 |
-
conn.rollback()
|
| 1723 |
-
raise
|
| 1724 |
|
| 1725 |
-
|
| 1726 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1727 |
|
| 1728 |
@app.route('/upload_csv', methods=['POST'])
|
| 1729 |
def upload_csv():
|
|
@@ -1752,6 +1754,16 @@ def se_upl_csv():
|
|
| 1752 |
return "EUR 22", 200
|
| 1753 |
return render_template('upl_csv.html')
|
| 1754 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
|
|
|
| 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():
|
|
|
|
| 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 |
|