DmitrMakeev commited on
Commit
c10e072
·
verified ·
1 Parent(s): b6bd1aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py CHANGED
@@ -1819,7 +1819,85 @@ def add_user_bot_route():
1819
 
1820
 
1821
 
 
 
1822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1823
 
1824
 
1825
 
 
1819
 
1820
 
1821
 
1822
+ # Список кураторов
1823
+ curators = ["Anna", "Ekaterina", "Ivan"]
1824
 
1825
+ # Переменная для отслеживания текущего куратора
1826
+ current_curator_index = 0
1827
+
1828
+ # Пример шаблона сопоставления для настройки переменных записываемых в базу данных.
1829
+ mapping_template3 = {
1830
+ 'name': 'name',
1831
+ 'phone': 'phone',
1832
+ 'email': 'email'
1833
+ }
1834
+
1835
+ def add_user_bot(db_name, user_data, mapping_template3):
1836
+ global current_curator_index
1837
+
1838
+ conn = sqlite3.connect(db_name)
1839
+ cursor = conn.cursor()
1840
+
1841
+ # Проверка наличия пользователя по email
1842
+ email = user_data.get('email')
1843
+ cursor.execute("SELECT 1 FROM contacts WHERE email = ?", (email,))
1844
+ if cursor.fetchone() is not None:
1845
+ logging.warning(f"User with email {email} already exists. Skipping insert.")
1846
+ conn.close()
1847
+ return
1848
+
1849
+ # Преобразование данных пользователя на основе шаблона сопоставления
1850
+ transformed_data = {db_column: user_data.get(json_key, "") for json_key, db_column in mapping_template3.items()}
1851
+
1852
+ # Добавление недостающих полей с пустыми строками
1853
+ required_fields = [
1854
+ "ad_url", "b_ban", "b_baners", "b_butt", "b_city", "b_fin", "b_ign", "b_mess",
1855
+ "canal", "chat_id", "curator", "data_t", "fin_prog", "key_pr", "n_con",
1856
+ "pr1", "pr2", "pr3", "pr4", "pr5", "shop_st", "vk_id", "web_st", "ws_st", "ws_stop"
1857
+ ]
1858
+ for field in required_fields:
1859
+ if field not in transformed_data:
1860
+ transformed_data[field] = ""
1861
+
1862
+ # Назначение куратора
1863
+ transformed_data['curator'] = curators[current_curator_index]
1864
+ current_curator_index = (current_curator_index + 1) % len(curators)
1865
+
1866
+ columns = ', '.join(transformed_data.keys())
1867
+ placeholders = ', '.join('?' for _ in transformed_data)
1868
+ insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
1869
+ insert_values = list(transformed_data.values())
1870
+
1871
+ cursor.execute(insert_query, insert_values)
1872
+ conn.commit()
1873
+ conn.close()
1874
+
1875
+ @app.route('/add_user_bot', methods=['GET'])
1876
+ def add_user_bot_route():
1877
+ db_name = request.args.get('db_name', 'data_gc.db') # Имя базы данных из параметра запроса
1878
+ user_data = {key: request.args.get(key, "") for key in mapping_template3.keys()}
1879
+
1880
+ logging.debug(f"User data: {user_data}")
1881
+
1882
+ try:
1883
+ add_user_bot(db_name, user_data, mapping_template3)
1884
+ return jsonify({'status': 'success', 'message': f'User added to {db_name}'})
1885
+ except Exception as e:
1886
+ logging.error(f"Error adding user: {e}")
1887
+ return jsonify({'status': 'error', 'message': str(e)}), 500
1888
+
1889
+ @app.route('/add_user_cur', methods=['POST'])
1890
+ def add_user_cur_route():
1891
+ global current_curator_index
1892
+
1893
+ data = request.json
1894
+ curator_name = data.get('curator_name')
1895
+
1896
+ if curator_name not in curators:
1897
+ return jsonify({'status': 'error', 'message': f'Curator {curator_name} not found in the list of curators'}), 400
1898
+
1899
+ current_curator_index = curators.index(curator_name)
1900
+ return jsonify({'status': 'success', 'message': f'Current curator set to {curator_name}'})
1901
 
1902
 
1903