Update app.py
Browse files
app.py
CHANGED
|
@@ -1764,7 +1764,107 @@ def display_form():
|
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1767 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
|
|
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
+
DATABASE6 = 'data_gc.db'
|
| 1768 |
+
# Функция для очистки номера телефона
|
| 1769 |
+
def clean_phone_number_ss(phone_number):
|
| 1770 |
+
return re.sub(r'\D', '', phone_number)
|
| 1771 |
+
|
| 1772 |
+
# Функция для вставки данных в базу данных
|
| 1773 |
+
def insert_data_ss(data):
|
| 1774 |
+
conn = sqlite3.connect(DATABASE6) # Подключаемся к базе данных
|
| 1775 |
+
cursor = conn.cursor()
|
| 1776 |
+
|
| 1777 |
+
for row in data:
|
| 1778 |
+
name = row.get('name', '')
|
| 1779 |
+
phone = row.get('phone', '').lstrip('+')
|
| 1780 |
+
email = row.get('email', '')
|
| 1781 |
+
data_t = row.get('data_t', '').strip('"')
|
| 1782 |
+
|
| 1783 |
+
# Очистка номера телефона
|
| 1784 |
+
phone = clean_phone_number_ss(phone)
|
| 1785 |
+
|
| 1786 |
+
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
| 1787 |
+
user_exists = cursor.fetchone()
|
| 1788 |
+
|
| 1789 |
+
if user_exists:
|
| 1790 |
+
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
| 1791 |
+
continue
|
| 1792 |
+
|
| 1793 |
+
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_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc']
|
| 1794 |
+
values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), row.get('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', ''), row.get('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', ''), row.get('data_on', ''), row.get('data_t', ''), row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', ''), row.get('gcpc', '')]
|
| 1795 |
+
|
| 1796 |
+
# Исключение столбцов
|
| 1797 |
+
excluded_columns = ['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_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc']
|
| 1798 |
+
columns = [col for col in columns if col not in excluded_columns]
|
| 1799 |
+
values = [values[columns.index(col)] for col in columns]
|
| 1800 |
+
|
| 1801 |
+
placeholders = ', '.join(['?' for _ in columns])
|
| 1802 |
+
columns_str = ', '.join(columns)
|
| 1803 |
+
|
| 1804 |
+
query = f'''
|
| 1805 |
+
INSERT INTO contacts ({columns_str})
|
| 1806 |
+
VALUES ({placeholders})
|
| 1807 |
+
'''
|
| 1808 |
+
|
| 1809 |
+
try:
|
| 1810 |
+
cursor.execute(query, values)
|
| 1811 |
+
except Exception as e:
|
| 1812 |
+
print(f"Error inserting row: {row}")
|
| 1813 |
+
print(f"Error message: {str(e)}")
|
| 1814 |
+
conn.rollback()
|
| 1815 |
+
continue
|
| 1816 |
+
|
| 1817 |
+
conn.commit()
|
| 1818 |
+
conn.close()
|
| 1819 |
+
|
| 1820 |
+
# Маршрут для приема GET запроса
|
| 1821 |
+
@app.route('/order', methods=['GET'])
|
| 1822 |
+
def from_shop_st():
|
| 1823 |
+
try:
|
| 1824 |
+
api_sys_control = request.args.get('api_sys')
|
| 1825 |
+
|
| 1826 |
+
if api_sys_control != api_key_sys:
|
| 1827 |
+
return json.dumps({"error": "Unauthorized access"}), 403
|
| 1828 |
+
|
| 1829 |
+
email = request.args.get('email', '')
|
| 1830 |
+
order = request.args.get('order', '')
|
| 1831 |
+
status = request.args.get('status', '')
|
| 1832 |
+
del_flag = request.args.get('del', '')
|
| 1833 |
+
|
| 1834 |
+
if not email:
|
| 1835 |
+
return json.dumps({"error": "Email is required"}), 400
|
| 1836 |
|
| 1837 |
+
conn = sqlite3.connect(DATABASE6)
|
| 1838 |
+
cursor = conn.cursor()
|
| 1839 |
+
|
| 1840 |
+
cursor.execute("SELECT shop_st FROM contacts WHERE email = ?", (email,))
|
| 1841 |
+
result = cursor.fetchone()
|
| 1842 |
+
|
| 1843 |
+
if result:
|
| 1844 |
+
shop_st = result[0] if result[0] else '{}'
|
| 1845 |
+
shop_st_data = json.loads(shop_st)
|
| 1846 |
+
else:
|
| 1847 |
+
shop_st_data = {"opn_st": "1"}
|
| 1848 |
+
|
| 1849 |
+
if del_flag == '1':
|
| 1850 |
+
shop_st_data = {"opn_st": "1"}
|
| 1851 |
+
elif order and status:
|
| 1852 |
+
shop_st_data[order] = status
|
| 1853 |
+
|
| 1854 |
+
shop_st_json = json.dumps(shop_st_data)
|
| 1855 |
+
|
| 1856 |
+
if result:
|
| 1857 |
+
cursor.execute("UPDATE contacts SET shop_st = ? WHERE email = ?", (shop_st_json, email))
|
| 1858 |
+
else:
|
| 1859 |
+
cursor.execute("INSERT INTO contacts (email, shop_st) VALUES (?, ?)", (email, shop_st_json))
|
| 1860 |
+
|
| 1861 |
+
conn.commit()
|
| 1862 |
+
conn.close()
|
| 1863 |
+
|
| 1864 |
+
return json.dumps(shop_st_data), 200
|
| 1865 |
+
|
| 1866 |
+
except Exception as e:
|
| 1867 |
+
return json.dumps({"error": str(e)}), 500
|
| 1868 |
|
| 1869 |
|
| 1870 |
|