Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -117,12 +117,18 @@ for db in DATABASES:
|
|
| 117 |
DATABASE_NEW = 'data_gc.db'
|
| 118 |
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
@app.route('/order_new', methods=['GET'])
|
| 121 |
def shop_order_new():
|
| 122 |
try:
|
|
|
|
| 123 |
api_sys_control = request.args.get('api_sys')
|
| 124 |
|
| 125 |
if api_sys_control != api_key_sys:
|
|
|
|
| 126 |
return json.dumps({"error": "Unauthorized access"}), 403
|
| 127 |
|
| 128 |
name = request.args.get('name', '')
|
|
@@ -131,12 +137,12 @@ def shop_order_new():
|
|
| 131 |
order = request.args.get('order', '')
|
| 132 |
status = request.args.get('status', '')
|
| 133 |
del_flag = request.args.get('del', '')
|
| 134 |
-
n_con_flag = request.args.get('n_con', '')
|
| 135 |
|
| 136 |
if not email or not phone:
|
|
|
|
| 137 |
return json.dumps({"error": "Email and phone are required"}), 400
|
| 138 |
|
| 139 |
-
# Очистка номера телефона
|
| 140 |
phone = clean_phone_number_ss(phone)
|
| 141 |
|
| 142 |
conn = sqlite3.connect(DATABASE_NEW)
|
|
@@ -148,7 +154,7 @@ def shop_order_new():
|
|
| 148 |
if result:
|
| 149 |
shop_st = result[17] if result[17] else '{}'
|
| 150 |
shop_st_data = json.loads(shop_st)
|
| 151 |
-
|
| 152 |
else:
|
| 153 |
shop_st_data = {}
|
| 154 |
|
|
@@ -160,29 +166,24 @@ def shop_order_new():
|
|
| 160 |
|
| 161 |
shop_st_json = json.dumps(shop_st_data)
|
| 162 |
|
| 163 |
-
# Получение текущей даты и времени в Московском часовом поясе
|
| 164 |
utc_now = datetime.utcnow()
|
| 165 |
msk_tz = pytz.timezone('Europe/Moscow')
|
| 166 |
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
| 167 |
data_on = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
| 168 |
|
| 169 |
-
# Исключаем все столбцы, кроме name, phone, email, shop_st, n_con, data_on
|
| 170 |
columns_to_update = ['name', 'phone', 'email', 'shop_st', 'n_con', 'data_on']
|
| 171 |
values_to_update = [name, phone, email, shop_st_json, n_con_flag, data_on]
|
| 172 |
|
| 173 |
if result:
|
| 174 |
-
# Обновляем только те поля, которые переданы в запросе
|
| 175 |
set_clause = ', '.join([f"{col} = ?" for col in columns_to_update])
|
| 176 |
query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?"
|
| 177 |
cursor.execute(query, values_to_update + [email, phone])
|
| 178 |
else:
|
| 179 |
-
# Вставляем новые данные
|
| 180 |
query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})"
|
| 181 |
cursor.execute(query, values_to_update)
|
| 182 |
|
| 183 |
conn.commit()
|
| 184 |
|
| 185 |
-
# Замена NULL на пустые строки
|
| 186 |
replace_null_with_empty_string(conn)
|
| 187 |
|
| 188 |
conn.close()
|
|
@@ -190,6 +191,7 @@ def shop_order_new():
|
|
| 190 |
return json.dumps(shop_st_data), 200
|
| 191 |
|
| 192 |
except Exception as e:
|
|
|
|
| 193 |
return json.dumps({"error": str(e)}), 500
|
| 194 |
|
| 195 |
|
|
@@ -205,8 +207,6 @@ def shop_order_new():
|
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
@app.route('/get_current_time', methods=['GET'])
|
| 211 |
def get_current_time():
|
| 212 |
utc_now = datetime.utcnow()
|
|
|
|
| 117 |
DATABASE_NEW = 'data_gc.db'
|
| 118 |
|
| 119 |
|
| 120 |
+
import logging
|
| 121 |
+
|
| 122 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 123 |
+
|
| 124 |
@app.route('/order_new', methods=['GET'])
|
| 125 |
def shop_order_new():
|
| 126 |
try:
|
| 127 |
+
logging.debug("Starting shop_order_new")
|
| 128 |
api_sys_control = request.args.get('api_sys')
|
| 129 |
|
| 130 |
if api_sys_control != api_key_sys:
|
| 131 |
+
logging.warning("Unauthorized access attempt")
|
| 132 |
return json.dumps({"error": "Unauthorized access"}), 403
|
| 133 |
|
| 134 |
name = request.args.get('name', '')
|
|
|
|
| 137 |
order = request.args.get('order', '')
|
| 138 |
status = request.args.get('status', '')
|
| 139 |
del_flag = request.args.get('del', '')
|
| 140 |
+
n_con_flag = request.args.get('n_con', '')
|
| 141 |
|
| 142 |
if not email or not phone:
|
| 143 |
+
logging.error("Email and phone are required")
|
| 144 |
return json.dumps({"error": "Email and phone are required"}), 400
|
| 145 |
|
|
|
|
| 146 |
phone = clean_phone_number_ss(phone)
|
| 147 |
|
| 148 |
conn = sqlite3.connect(DATABASE_NEW)
|
|
|
|
| 154 |
if result:
|
| 155 |
shop_st = result[17] if result[17] else '{}'
|
| 156 |
shop_st_data = json.loads(shop_st)
|
| 157 |
+
logging.debug(f"Existing record found. Loaded JSON: {shop_st_data}")
|
| 158 |
else:
|
| 159 |
shop_st_data = {}
|
| 160 |
|
|
|
|
| 166 |
|
| 167 |
shop_st_json = json.dumps(shop_st_data)
|
| 168 |
|
|
|
|
| 169 |
utc_now = datetime.utcnow()
|
| 170 |
msk_tz = pytz.timezone('Europe/Moscow')
|
| 171 |
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
| 172 |
data_on = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
| 173 |
|
|
|
|
| 174 |
columns_to_update = ['name', 'phone', 'email', 'shop_st', 'n_con', 'data_on']
|
| 175 |
values_to_update = [name, phone, email, shop_st_json, n_con_flag, data_on]
|
| 176 |
|
| 177 |
if result:
|
|
|
|
| 178 |
set_clause = ', '.join([f"{col} = ?" for col in columns_to_update])
|
| 179 |
query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?"
|
| 180 |
cursor.execute(query, values_to_update + [email, phone])
|
| 181 |
else:
|
|
|
|
| 182 |
query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})"
|
| 183 |
cursor.execute(query, values_to_update)
|
| 184 |
|
| 185 |
conn.commit()
|
| 186 |
|
|
|
|
| 187 |
replace_null_with_empty_string(conn)
|
| 188 |
|
| 189 |
conn.close()
|
|
|
|
| 191 |
return json.dumps(shop_st_data), 200
|
| 192 |
|
| 193 |
except Exception as e:
|
| 194 |
+
logging.error(f"An error occurred: {str(e)}")
|
| 195 |
return json.dumps({"error": str(e)}), 500
|
| 196 |
|
| 197 |
|
|
|
|
| 207 |
|
| 208 |
|
| 209 |
|
|
|
|
|
|
|
| 210 |
@app.route('/get_current_time', methods=['GET'])
|
| 211 |
def get_current_time():
|
| 212 |
utc_now = datetime.utcnow()
|