Update app.py
Browse files
app.py
CHANGED
|
@@ -1840,7 +1840,7 @@ current_curator_index = 0
|
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
-
def
|
| 1844 |
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
| 1845 |
payload = {"phoneNumber": phone_number}
|
| 1846 |
headers = {'Content-Type': 'application/json'}
|
|
@@ -1851,12 +1851,13 @@ def J_verify_phone_number(phone_number):
|
|
| 1851 |
else:
|
| 1852 |
return "Error"
|
| 1853 |
|
| 1854 |
-
def
|
|
|
|
| 1855 |
return re.sub(r'\D', '', phone_number)
|
| 1856 |
|
| 1857 |
-
def
|
| 1858 |
global current_curator_index
|
| 1859 |
-
conn = sqlite3.connect(
|
| 1860 |
cursor = conn.cursor()
|
| 1861 |
|
| 1862 |
for row in data:
|
|
@@ -1865,7 +1866,8 @@ def J_insert_data(data, verify_phone, add_curator):
|
|
| 1865 |
email = row.get('email', '')
|
| 1866 |
data_t = row.get('data_t', '').strip('"')
|
| 1867 |
|
| 1868 |
-
|
|
|
|
| 1869 |
|
| 1870 |
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
| 1871 |
user_exists = cursor.fetchone()
|
|
@@ -1881,7 +1883,7 @@ def J_insert_data(data, verify_phone, add_curator):
|
|
| 1881 |
curator = row.get('curator', '')
|
| 1882 |
|
| 1883 |
if verify_phone == "1":
|
| 1884 |
-
ws_st =
|
| 1885 |
else:
|
| 1886 |
ws_st = row.get('ws_st', '')
|
| 1887 |
|
|
@@ -1898,6 +1900,7 @@ def J_insert_data(data, verify_phone, add_curator):
|
|
| 1898 |
|
| 1899 |
try:
|
| 1900 |
cursor.execute(query, values)
|
|
|
|
| 1901 |
user_data = dict(zip(columns, values))
|
| 1902 |
send_to_google_forms(user_data, gog_url)
|
| 1903 |
except Exception as e:
|
|
@@ -1909,23 +1912,36 @@ def J_insert_data(data, verify_phone, add_curator):
|
|
| 1909 |
conn.commit()
|
| 1910 |
conn.close()
|
| 1911 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1912 |
@app.route('/upload_json', methods=['POST'])
|
| 1913 |
-
def
|
| 1914 |
-
|
| 1915 |
-
|
| 1916 |
-
|
| 1917 |
-
|
| 1918 |
-
|
| 1919 |
-
|
| 1920 |
-
|
| 1921 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1922 |
|
| 1923 |
@app.route('/upl_json', methods=['GET'])
|
| 1924 |
-
def
|
| 1925 |
api_sys_control = request.args.get('api_sys')
|
| 1926 |
if api_sys_control != api_key_sys:
|
| 1927 |
return "EUR 22", 200
|
| 1928 |
-
return render_template('
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
|
|
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
+
def j_verify_phone_number(phone_number):
|
| 1844 |
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
| 1845 |
payload = {"phoneNumber": phone_number}
|
| 1846 |
headers = {'Content-Type': 'application/json'}
|
|
|
|
| 1851 |
else:
|
| 1852 |
return "Error"
|
| 1853 |
|
| 1854 |
+
def j_clean_phone_number(phone_number):
|
| 1855 |
+
# Удаляем все символы, кроме цифр
|
| 1856 |
return re.sub(r'\D', '', phone_number)
|
| 1857 |
|
| 1858 |
+
def j_insert_data(data, verify_phone, add_curator):
|
| 1859 |
global current_curator_index
|
| 1860 |
+
conn = sqlite3.connect(DATABASE5) # Обновлено на DATABASE5
|
| 1861 |
cursor = conn.cursor()
|
| 1862 |
|
| 1863 |
for row in data:
|
|
|
|
| 1866 |
email = row.get('email', '')
|
| 1867 |
data_t = row.get('data_t', '').strip('"')
|
| 1868 |
|
| 1869 |
+
# Очистка номера телефона
|
| 1870 |
+
phone = j_clean_phone_number(phone)
|
| 1871 |
|
| 1872 |
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
| 1873 |
user_exists = cursor.fetchone()
|
|
|
|
| 1883 |
curator = row.get('curator', '')
|
| 1884 |
|
| 1885 |
if verify_phone == "1":
|
| 1886 |
+
ws_st = j_verify_phone_number(phone)
|
| 1887 |
else:
|
| 1888 |
ws_st = row.get('ws_st', '')
|
| 1889 |
|
|
|
|
| 1900 |
|
| 1901 |
try:
|
| 1902 |
cursor.execute(query, values)
|
| 1903 |
+
# Отправка данных в Google Forms
|
| 1904 |
user_data = dict(zip(columns, values))
|
| 1905 |
send_to_google_forms(user_data, gog_url)
|
| 1906 |
except Exception as e:
|
|
|
|
| 1912 |
conn.commit()
|
| 1913 |
conn.close()
|
| 1914 |
|
| 1915 |
+
def send_to_google_forms(user_data, gog_url):
|
| 1916 |
+
headers = {'Content-Type': 'application/json'}
|
| 1917 |
+
response = requests.post(gog_url, headers=headers, json=user_data)
|
| 1918 |
+
if response.status_code != 200:
|
| 1919 |
+
print(f"Failed to send data to Google Forms. Status code: {response.status_code}")
|
| 1920 |
+
print(f"Response: {response.text}")
|
| 1921 |
+
|
| 1922 |
@app.route('/upload_json', methods=['POST'])
|
| 1923 |
+
def j_upload_json():
|
| 1924 |
+
if 'file' not in request.files:
|
| 1925 |
+
return jsonify({"error": "No file part"}), 400
|
| 1926 |
+
file = request.files['file']
|
| 1927 |
+
if file.filename == '':
|
| 1928 |
+
return jsonify({"error": "No selected file"}), 400
|
| 1929 |
+
if file and file.filename.endswith('.json'):
|
| 1930 |
+
data = json.load(file)
|
| 1931 |
+
verify_phone = request.form.get('verify_phone', '0')
|
| 1932 |
+
add_curator = request.form.get('add_curator', '0')
|
| 1933 |
+
print(f"Verify Phone: {verify_phone}")
|
| 1934 |
+
print(f"Add Curator: {add_curator}")
|
| 1935 |
+
j_insert_data(data, verify_phone, add_curator)
|
| 1936 |
+
return jsonify({"message": "Data uploaded and inserted successfully"})
|
| 1937 |
+
return jsonify({"error": "Invalid file format"}), 400
|
| 1938 |
|
| 1939 |
@app.route('/upl_json', methods=['GET'])
|
| 1940 |
+
def j_se_upl_json():
|
| 1941 |
api_sys_control = request.args.get('api_sys')
|
| 1942 |
if api_sys_control != api_key_sys:
|
| 1943 |
return "EUR 22", 200
|
| 1944 |
+
return render_template('upload_json.html')
|
| 1945 |
|
| 1946 |
|
| 1947 |
|