DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1933,74 +1933,6 @@ def se_upl_csv_b():
|
|
1933 |
|
1934 |
|
1935 |
|
1936 |
-
|
1937 |
-
DATABASE = 'data_gc.db'
|
1938 |
-
|
1939 |
-
# Функция для очистки номера телефона
|
1940 |
-
def clean_phone_number(phone_number):
|
1941 |
-
return re.sub(r'\D', '', phone_number)
|
1942 |
-
|
1943 |
-
# Функция для вставки данных в базу данных
|
1944 |
-
def insert_data(data):
|
1945 |
-
conn = sqlite3.connect(DATABASE) # Подключаемся к базе данных
|
1946 |
-
cursor = conn.cursor()
|
1947 |
-
|
1948 |
-
for row in data:
|
1949 |
-
name = row.get('name', '')
|
1950 |
-
phone = row.get('phone', '').lstrip('+')
|
1951 |
-
email = row.get('email', '')
|
1952 |
-
data_t = row.get('data_t', '').strip('"')
|
1953 |
-
|
1954 |
-
# Очистка номера телефона
|
1955 |
-
phone = clean_phone_number(phone)
|
1956 |
-
|
1957 |
-
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
1958 |
-
user_exists = cursor.fetchone()
|
1959 |
-
|
1960 |
-
if user_exists:
|
1961 |
-
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
1962 |
-
continue
|
1963 |
-
|
1964 |
-
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']
|
1965 |
-
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', ''), data_t, row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', '')]
|
1966 |
-
|
1967 |
-
placeholders = ', '.join(['?' for _ in columns])
|
1968 |
-
columns_str = ', '.join(columns)
|
1969 |
-
|
1970 |
-
query = f'''
|
1971 |
-
INSERT INTO contacts ({columns_str})
|
1972 |
-
VALUES ({placeholders})
|
1973 |
-
'''
|
1974 |
-
|
1975 |
-
try:
|
1976 |
-
cursor.execute(query, values)
|
1977 |
-
except Exception as e:
|
1978 |
-
print(f"Error inserting row: {row}")
|
1979 |
-
print(f"Error message: {str(e)}")
|
1980 |
-
conn.rollback()
|
1981 |
-
continue
|
1982 |
-
|
1983 |
-
conn.commit()
|
1984 |
-
conn.close()
|
1985 |
-
|
1986 |
-
# Маршрут для загрузки JSON-файла
|
1987 |
-
@app.route('/upload_json', methods=['POST'])
|
1988 |
-
def upload_json():
|
1989 |
-
if 'file' not in request.files:
|
1990 |
-
return jsonify({"error": "No file part"}), 400
|
1991 |
-
file = request.files['file']
|
1992 |
-
if file.filename == '':
|
1993 |
-
return jsonify({"error": "No selected file"}), 400
|
1994 |
-
if file and file.filename.endswith('.json'):
|
1995 |
-
data = json.load(file)
|
1996 |
-
insert_data(data)
|
1997 |
-
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1998 |
-
return jsonify({"error": "Invalid file format"}), 400
|
1999 |
-
|
2000 |
-
# Маршрут для отображения формы загрузки JSON
|
2001 |
-
@app.route('/upl_json', methods=['GET'])
|
2002 |
-
def display_form():
|
2003 |
-
return render_template('upload_json.html')
|
2004 |
|
2005 |
|
2006 |
|
|
|
1933 |
|
1934 |
|
1935 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1936 |
|
1937 |
|
1938 |
|