DmitrMakeev
commited on
Commit
•
7bcc53e
1
Parent(s):
682e755
Update app.py
Browse files
app.py
CHANGED
@@ -1680,6 +1680,20 @@ def add_data_ver_cur():
|
|
1680 |
|
1681 |
DATABASE2 = 'data_gc.db'
|
1682 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1683 |
def parse_csv_data(data):
|
1684 |
parsed_data = []
|
1685 |
for item in data:
|
@@ -1689,17 +1703,12 @@ def parse_csv_data(data):
|
|
1689 |
parsed_data.append(dict(zip(headers, row)))
|
1690 |
return parsed_data
|
1691 |
|
1692 |
-
def insert_data(data
|
1693 |
-
|
|
|
1694 |
cursor = conn.cursor()
|
1695 |
|
1696 |
for row in data:
|
1697 |
-
# Получение текущего времени в московском часовом поясе
|
1698 |
-
utc_now = datetime.utcnow()
|
1699 |
-
msk_tz = pytz.timezone('Europe/Moscow')
|
1700 |
-
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
1701 |
-
data_t = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1702 |
-
|
1703 |
# Проверка наличия ключей перед их использованием и предоставление значений по умолчанию
|
1704 |
name = row.get('Name', '')
|
1705 |
|
@@ -1737,6 +1746,9 @@ def insert_data(data, db_name):
|
|
1737 |
utm_term = row.get('utm_term', '')
|
1738 |
utm_content = row.get('utm_content', '')
|
1739 |
|
|
|
|
|
|
|
1740 |
# Проверка существования пользователя в базе данных по email или телефону
|
1741 |
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
1742 |
user_exists = cursor.fetchone()
|
@@ -1746,6 +1758,15 @@ def insert_data(data, db_name):
|
|
1746 |
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
1747 |
continue
|
1748 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1749 |
# Создание динамического SQL-запроса для вставки данных
|
1750 |
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']
|
1751 |
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]
|
@@ -1781,8 +1802,7 @@ def upload_csv():
|
|
1781 |
csv_input = csv.DictReader(stream)
|
1782 |
data = [row for row in csv_input]
|
1783 |
parsed_data = parse_csv_data(data)
|
1784 |
-
|
1785 |
-
insert_data(parsed_data, db_name)
|
1786 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1787 |
return jsonify({"error": "Invalid file format"}), 400
|
1788 |
|
|
|
1680 |
|
1681 |
DATABASE2 = 'data_gc.db'
|
1682 |
|
1683 |
+
def verify_phone_number(phone_number):
|
1684 |
+
if verifikation_start == "1":
|
1685 |
+
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
1686 |
+
payload = {"phoneNumber": phone_number}
|
1687 |
+
headers = {'Content-Type': 'application/json'}
|
1688 |
+
response = requests.post(full_url_ver, headers=headers, json=payload)
|
1689 |
+
if response.status_code == 200:
|
1690 |
+
response_body = response.json()
|
1691 |
+
return response_body.get('existsWhatsapp', 'false')
|
1692 |
+
else:
|
1693 |
+
return "Error"
|
1694 |
+
else:
|
1695 |
+
return "false"
|
1696 |
+
|
1697 |
def parse_csv_data(data):
|
1698 |
parsed_data = []
|
1699 |
for item in data:
|
|
|
1703 |
parsed_data.append(dict(zip(headers, row)))
|
1704 |
return parsed_data
|
1705 |
|
1706 |
+
def insert_data(data):
|
1707 |
+
global current_curator_index
|
1708 |
+
conn = sqlite3.connect(DATABASE2)
|
1709 |
cursor = conn.cursor()
|
1710 |
|
1711 |
for row in data:
|
|
|
|
|
|
|
|
|
|
|
|
|
1712 |
# Проверка наличия ключей перед их использованием и предоставление значений по умолчанию
|
1713 |
name = row.get('Name', '')
|
1714 |
|
|
|
1746 |
utm_term = row.get('utm_term', '')
|
1747 |
utm_content = row.get('utm_content', '')
|
1748 |
|
1749 |
+
# Взятие даты из столбца CSV-таблицы
|
1750 |
+
data_t = row.get('Date', '')
|
1751 |
+
|
1752 |
# Проверка существования пользователя в базе данных по email или телефону
|
1753 |
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
1754 |
user_exists = cursor.fetchone()
|
|
|
1758 |
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
1759 |
continue
|
1760 |
|
1761 |
+
# Добавление куратора, если curator_on_off == "1"
|
1762 |
+
if curator_on_off == "1":
|
1763 |
+
curator = curators[current_curator_index]
|
1764 |
+
current_curator_index = (current_curator_index + 1) % len(curators)
|
1765 |
+
|
1766 |
+
# Верификация номера телефона, если verifikation_start == "1"
|
1767 |
+
if verifikation_start == "1":
|
1768 |
+
ws_st = verify_phone_number(phone)
|
1769 |
+
|
1770 |
# Создание динамического SQL-запроса для вставки данных
|
1771 |
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']
|
1772 |
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]
|
|
|
1802 |
csv_input = csv.DictReader(stream)
|
1803 |
data = [row for row in csv_input]
|
1804 |
parsed_data = parse_csv_data(data)
|
1805 |
+
insert_data(parsed_data)
|
|
|
1806 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1807 |
return jsonify({"error": "Invalid file format"}), 400
|
1808 |
|