DmitrMakeev commited on
Commit
4507679
·
verified ·
1 Parent(s): aa35094

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -73
app.py CHANGED
@@ -1840,7 +1840,7 @@ current_curator_index = 0
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,96 +1851,81 @@ def j_verify_phone_number(phone_number):
1851
  else:
1852
  return "Error"
1853
 
1854
- def j_parse_json_data(data):
1855
- return json.loads(data)
1856
-
1857
- def j_clean_phone_number(phone_number):
1858
  return re.sub(r'\D', '', phone_number)
1859
 
1860
- def j_insert_data(data, verify_phone, add_curator):
1861
  global current_curator_index
1862
- for db_name in DATABASES5:
1863
- conn = sqlite3.connect(db_name)
1864
- cursor = conn.cursor()
1865
-
1866
- for row in data:
1867
- name = row.get('Имя', '')
1868
- phone = row.get('WhatsApp', '').lstrip('+')
1869
- email = row.get('Email', '')
1870
- data_t = row.get('Дата', '').strip('"')
1871
 
1872
- phone = j_clean_phone_number(phone)
 
 
 
 
1873
 
1874
- cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1875
- user_exists = cursor.fetchone()
1876
 
1877
- if user_exists:
1878
- logging.warning(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1879
- continue
1880
 
1881
- if add_curator == "1":
1882
- curator = curators[current_curator_index]
1883
- current_curator_index = (current_curator_index + 1) % len(curators)
1884
- else:
1885
- curator = row.get('Куратор', '')
1886
 
1887
- if verify_phone == "1":
1888
- ws_st = j_verify_phone_number(phone)
1889
- else:
1890
- ws_st = row.get('ws_st', '')
1891
-
1892
- 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']
1893
- 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', '')]
1894
 
1895
- placeholders = ', '.join(['?' for _ in columns])
1896
- columns_str = ', '.join(columns)
 
 
1897
 
1898
- query = f'''
1899
- INSERT INTO contacts ({columns_str})
1900
- VALUES ({placeholders})
1901
- '''
1902
 
1903
- try:
1904
- cursor.execute(query, values)
1905
- user_data = dict(zip(columns, values))
1906
- send_to_google_forms(user_data, gog_url)
1907
- except Exception as e:
1908
- logging.error(f"Error inserting row: {row}")
1909
- logging.error(f"Error message: {str(e)}")
1910
- conn.rollback()
1911
- raise
1912
 
1913
- conn.commit()
1914
- conn.close()
 
 
1915
 
1916
- @app.route('/j_upload_json', methods=['POST'])
1917
- def j_upload_json():
1918
- if 'file' not in request.files:
1919
- return jsonify({"error": "No file part"}), 400
1920
- file = request.files['file']
1921
- if file.filename == '':
1922
- return jsonify({"error": "No selected file"}), 400
1923
- if file and file.filename.endswith('.json'):
1924
  try:
1925
- data = file.read().decode("UTF8")
1926
- parsed_data = j_parse_json_data(data)
1927
- verify_phone = request.form.get('verify_phone', '0')
1928
- add_curator = request.form.get('add_curator', '0')
1929
- logging.debug(f"Verify Phone: {verify_phone}")
1930
- logging.debug(f"Add Curator: {add_curator}")
1931
- j_insert_data(parsed_data, verify_phone, add_curator)
1932
- return jsonify({"message": "Data uploaded and inserted successfully"})
1933
- except json.JSONDecodeError as e:
1934
- return jsonify({"error": "Invalid JSON format"}), 400
1935
- return jsonify({"error": "Invalid file format"}), 400
1936
 
1937
- @app.route('/j_upl_json', methods=['GET'])
1938
- def j_se_upl_json():
 
 
 
 
 
 
 
 
 
 
 
1939
  api_sys_control = request.args.get('api_sys')
1940
  if api_sys_control != api_key_sys:
1941
  return "EUR 22", 200
1942
- return render_template('j_upl_json.html')
1943
-
1944
 
1945
 
1946
 
 
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
  return re.sub(r'\D', '', phone_number)
1856
 
1857
+ def J_insert_data(data, verify_phone, add_curator):
1858
  global current_curator_index
1859
+ conn = sqlite3.connect(DATABASES5)
1860
+ cursor = conn.cursor()
 
 
 
 
 
 
 
1861
 
1862
+ for row in data:
1863
+ name = row.get('name', '')
1864
+ phone = row.get('phone', '').lstrip('+')
1865
+ email = row.get('email', '')
1866
+ data_t = row.get('data_t', '').strip('"')
1867
 
1868
+ phone = J_clean_phone_number(phone)
 
1869
 
1870
+ cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1871
+ user_exists = cursor.fetchone()
 
1872
 
1873
+ if user_exists:
1874
+ print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1875
+ continue
 
 
1876
 
1877
+ if add_curator == "1":
1878
+ curator = curators[current_curator_index]
1879
+ current_curator_index = (current_curator_index + 1) % len(curators)
1880
+ else:
1881
+ curator = row.get('curator', '')
 
 
1882
 
1883
+ if verify_phone == "1":
1884
+ ws_st = J_verify_phone_number(phone)
1885
+ else:
1886
+ ws_st = row.get('ws_st', '')
1887
 
1888
+ 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']
1889
+ 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('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', '')]
 
 
1890
 
1891
+ placeholders = ', '.join(['?' for _ in columns])
1892
+ columns_str = ', '.join(columns)
 
 
 
 
 
 
 
1893
 
1894
+ query = f'''
1895
+ INSERT INTO contacts ({columns_str})
1896
+ VALUES ({placeholders})
1897
+ '''
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:
1904
+ print(f"Error inserting row: {row}")
1905
+ print(f"Error message: {str(e)}")
1906
+ conn.rollback()
1907
+ raise
1908
+
1909
+ conn.commit()
1910
+ conn.close()
1911
 
1912
+ @app.route('/upload_json', methods=['POST'])
1913
+ def J_upload_json():
1914
+ data = request.get_json()
1915
+ if not data:
1916
+ return jsonify({"error": "Invalid JSON format"}), 400
1917
+ verify_phone = request.form.get('verify_phone', '0')
1918
+ add_curator = request.form.get('add_curator', '0')
1919
+ print(f"JSON Data: {json.dumps(data, ensure_ascii=False, indent=4)}")
1920
+ J_insert_data(data, verify_phone, add_curator)
1921
+ return jsonify({"message": "Data uploaded and inserted successfully"})
1922
+
1923
+ @app.route('/upl_json', methods=['GET'])
1924
+ def J_upl_json():
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('upl_json.html')
 
1929
 
1930
 
1931