DmitrMakeev
commited on
Commit
•
c761bac
1
Parent(s):
1a36f68
Update app.py
Browse files
app.py
CHANGED
@@ -1180,6 +1180,15 @@ DATABASE2 = 'data_gc.db'
|
|
1180 |
|
1181 |
|
1182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1183 |
def insert_data(data):
|
1184 |
conn = sqlite3.connect(DATABASE2)
|
1185 |
cursor = conn.cursor()
|
@@ -1191,9 +1200,9 @@ def insert_data(data):
|
|
1191 |
data_t = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1192 |
|
1193 |
# Проверка наличия ключей перед их использованием и предоставление значений по умолчанию
|
1194 |
-
name = row.get('
|
1195 |
-
phone = row.get('
|
1196 |
-
email = row.get('
|
1197 |
vk_id = row.get('vk_id', '')
|
1198 |
chat_id = row.get('chat_id', '')
|
1199 |
ws_st = row.get('ws_st', '')
|
@@ -1219,16 +1228,19 @@ def insert_data(data):
|
|
1219 |
n_con = row.get('n_con', '')
|
1220 |
canal = row.get('canal', '')
|
1221 |
|
1222 |
-
# Создание динамического SQL-запроса
|
1223 |
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', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t']
|
1224 |
values = [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, ad_url, key_pr, n_con, canal, data_t]
|
1225 |
|
1226 |
placeholders = ', '.join(['?' for _ in columns])
|
1227 |
columns_str = ', '.join(columns)
|
|
|
1228 |
|
1229 |
query = f'''
|
1230 |
INSERT INTO contacts ({columns_str})
|
1231 |
VALUES ({placeholders})
|
|
|
|
|
1232 |
'''
|
1233 |
|
1234 |
try:
|
@@ -1253,7 +1265,8 @@ def upload_csv():
|
|
1253 |
stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
|
1254 |
csv_input = csv.DictReader(stream)
|
1255 |
data = [row for row in csv_input]
|
1256 |
-
|
|
|
1257 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1258 |
return jsonify({"error": "Invalid file format"}), 400
|
1259 |
|
|
|
1180 |
|
1181 |
|
1182 |
|
1183 |
+
def parse_csv_data(data):
|
1184 |
+
parsed_data = []
|
1185 |
+
for item in data:
|
1186 |
+
for key, value in item.items():
|
1187 |
+
headers = key.split(';')
|
1188 |
+
row = value.split(';')
|
1189 |
+
parsed_data.append(dict(zip(headers, row)))
|
1190 |
+
return parsed_data
|
1191 |
+
|
1192 |
def insert_data(data):
|
1193 |
conn = sqlite3.connect(DATABASE2)
|
1194 |
cursor = conn.cursor()
|
|
|
1200 |
data_t = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1201 |
|
1202 |
# Проверка наличия ключей перед их использованием и предоставление значений по умолчанию
|
1203 |
+
name = row.get('Name', '')
|
1204 |
+
phone = row.get('Phone', '')
|
1205 |
+
email = row.get('Email', '')
|
1206 |
vk_id = row.get('vk_id', '')
|
1207 |
chat_id = row.get('chat_id', '')
|
1208 |
ws_st = row.get('ws_st', '')
|
|
|
1228 |
n_con = row.get('n_con', '')
|
1229 |
canal = row.get('canal', '')
|
1230 |
|
1231 |
+
# Создание динамического SQL-запроса с использованием INSERT ... ON CONFLICT ... DO UPDATE
|
1232 |
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', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t']
|
1233 |
values = [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, ad_url, key_pr, n_con, canal, data_t]
|
1234 |
|
1235 |
placeholders = ', '.join(['?' for _ in columns])
|
1236 |
columns_str = ', '.join(columns)
|
1237 |
+
update_str = ', '.join([f'{col} = excluded.{col}' for col in columns])
|
1238 |
|
1239 |
query = f'''
|
1240 |
INSERT INTO contacts ({columns_str})
|
1241 |
VALUES ({placeholders})
|
1242 |
+
ON CONFLICT(email) DO UPDATE SET
|
1243 |
+
{update_str}
|
1244 |
'''
|
1245 |
|
1246 |
try:
|
|
|
1265 |
stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
|
1266 |
csv_input = csv.DictReader(stream)
|
1267 |
data = [row for row in csv_input]
|
1268 |
+
parsed_data = parse_csv_data(data)
|
1269 |
+
insert_data(parsed_data)
|
1270 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1271 |
return jsonify({"error": "Invalid file format"}), 400
|
1272 |
|