DmitrMakeev commited on
Commit
4412515
1 Parent(s): 5c466f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py CHANGED
@@ -1920,7 +1920,82 @@ def from_shop_st2():
1920
 
1921
 
1922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1923
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1924
 
1925
 
1926
 
 
1920
 
1921
 
1922
 
1923
+ # Маршрут для приема GET запроса
1924
+ @app.route('/order3', methods=['GET'])
1925
+ def from_shop_st3():
1926
+ try:
1927
+ api_sys_control = request.args.get('api_sys')
1928
+
1929
+ if api_sys_control != api_key_sys:
1930
+ return json.dumps({"error": "Unauthorized access"}), 403
1931
+
1932
+ name = request.args.get('name', '')
1933
+ email = request.args.get('email', '')
1934
+ phone = request.args.get('phone', '').lstrip('+')
1935
+ order = request.args.get('order', '')
1936
+ status = request.args.get('status', '')
1937
+ del_flag = request.args.get('del', '')
1938
+ n_con_flag = request.args.get('n_con', '') # Добавлен параметр n_con
1939
+
1940
+ if not email or not phone:
1941
+ return json.dumps({"error": "Email and phone are required"}), 400
1942
+
1943
+ # Очистка номера телефона
1944
+ phone = clean_phone_number_ss(phone)
1945
+
1946
+ conn = sqlite3.connect(DATABASE6)
1947
+ cursor = conn.cursor()
1948
+
1949
+ cursor.execute("SELECT * FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1950
+ result = cursor.fetchone()
1951
+
1952
+ if result:
1953
+ shop_st = result[17] if result[17] else '{}'
1954
+ shop_st_data = json.loads(shop_st)
1955
+ else:
1956
+ shop_st_data = {}
1957
 
1958
+ if del_flag == '1':
1959
+ shop_st_data = {}
1960
+ elif order and status:
1961
+ shop_st_data[order] = status
1962
+
1963
+ shop_st_json = json.dumps(shop_st_data)
1964
+
1965
+ # Исключаем все столбцы, кроме name, phone, email, shop_st, n_con
1966
+ columns_to_update = ['name', 'phone', 'email', 'shop_st', 'n_con']
1967
+ values_to_update = [name, phone, email, shop_st_json, n_con_flag]
1968
+
1969
+ if result:
1970
+ # Обновляем только те поля, которые переданы в запросе
1971
+ set_clause = ', '.join([f"{col} = ?" for col in columns_to_update])
1972
+ query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?"
1973
+ cursor.execute(query, values_to_update + [email, phone])
1974
+ else:
1975
+ # Вставляем новые данные
1976
+ query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})"
1977
+ cursor.execute(query, values_to_update)
1978
+
1979
+ conn.commit()
1980
+
1981
+ # Дополнительный проход для замены NULL на пустые строки
1982
+ cursor.execute("""
1983
+ UPDATE contacts
1984
+ SET name = COALESCE(name, ''),
1985
+ phone = COALESCE(phone, ''),
1986
+ email = COALESCE(email, ''),
1987
+ shop_st = COALESCE(shop_st, ''),
1988
+ n_con = COALESCE(n_con, '')
1989
+ WHERE email = ? OR phone = ?
1990
+ """, (email, phone))
1991
+
1992
+ conn.commit()
1993
+ conn.close()
1994
+
1995
+ return json.dumps(shop_st_data), 200
1996
+
1997
+ except Exception as e:
1998
+ return json.dumps({"error": str(e)}), 500
1999
 
2000
 
2001