DmitrMakeev commited on
Commit
6f1f043
·
verified ·
1 Parent(s): 482d1c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -2
app.py CHANGED
@@ -2053,7 +2053,8 @@ def from_allow():
2053
  if not email or not phone:
2054
  return json.dumps({"error": "Email and phone are required"}), 400
2055
 
2056
- phone = clean_phone_number_1(phone)
 
2057
 
2058
  conn = sqlite3.connect(DATABASE6)
2059
  cursor = conn.cursor()
@@ -2062,47 +2063,71 @@ def from_allow():
2062
  result = cursor.fetchone()
2063
 
2064
  if result:
 
2065
  pr5 = result[21] if result[21] else '{}'
2066
  pr5_data = json.loads(pr5)
2067
  print(f"Existing record found. Loaded JSON: {pr5_data}")
2068
  else:
 
2069
  pr5_data = {}
2070
  print("No existing record found. Initialized new JSON.")
2071
 
 
 
 
 
 
2072
  if del_flag == '1':
 
2073
  if module in pr5_data:
2074
  del pr5_data[module]
2075
  print(f"Key '{module}' found and deleted from JSON.")
2076
  else:
2077
  print(f"Key '{module}' not found in JSON.")
2078
  elif module and status:
 
2079
  pr5_data[module] = status
2080
  print(f"Updated JSON with key '{module}' and status '{status}'.")
2081
 
2082
  pr5_json = json.dumps(pr5_data)
2083
  print(f"Updated JSON to be saved: {pr5_json}")
2084
 
 
2085
  utc_now = datetime.utcnow()
2086
  msk_tz = pytz.timezone('Europe/Moscow')
2087
  msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
2088
  data_on = msk_now.strftime('%Y-%m-%d %H:%M:%S')
2089
 
 
2090
  columns_to_update = ['name', 'phone', 'email', 'pr5', 'n_con', 'data_on']
2091
  values_to_update = [name, phone, email, pr5_json, n_con_flag, data_on]
2092
 
2093
  if result:
 
2094
  set_clause = ', '.join([f"{col} = ?" for col in columns_to_update])
2095
  query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?"
2096
  cursor.execute(query, values_to_update + [email, phone])
2097
  print("Record updated in database.")
2098
  else:
 
2099
  query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})"
2100
  cursor.execute(query, values_to_update)
2101
  print("New record inserted into database.")
2102
 
2103
  conn.commit()
2104
 
2105
- replace_null_with_empty_1(conn)
 
 
 
 
 
 
 
 
 
 
 
2106
  print("NULL values replaced with empty strings if any.")
2107
 
2108
  conn.close()
@@ -2118,6 +2143,7 @@ def from_allow():
2118
 
2119
 
2120
 
 
2121
  @app.route('/gc_in', methods=['GET'])
2122
  def add_data_gc_in():
2123
  global current_curator_index
 
2053
  if not email or not phone:
2054
  return json.dumps({"error": "Email and phone are required"}), 400
2055
 
2056
+ # Очистка номера телефона
2057
+ phone = clean_phone_number_ss(phone)
2058
 
2059
  conn = sqlite3.connect(DATABASE6)
2060
  cursor = conn.cursor()
 
2063
  result = cursor.fetchone()
2064
 
2065
  if result:
2066
+ # Если есть запись, загружаем текущий JSON из поля pr5
2067
  pr5 = result[21] if result[21] else '{}'
2068
  pr5_data = json.loads(pr5)
2069
  print(f"Existing record found. Loaded JSON: {pr5_data}")
2070
  else:
2071
+ # Если записи нет, создаем пустой словарь
2072
  pr5_data = {}
2073
  print("No existing record found. Initialized new JSON.")
2074
 
2075
+ # Логирование значений параметров
2076
+ print(f"Module: {module}")
2077
+ print(f"Status: {status}")
2078
+ print(f"Delete Flag: {del_flag}")
2079
+
2080
  if del_flag == '1':
2081
+ # Удаление ключа из JSON
2082
  if module in pr5_data:
2083
  del pr5_data[module]
2084
  print(f"Key '{module}' found and deleted from JSON.")
2085
  else:
2086
  print(f"Key '{module}' not found in JSON.")
2087
  elif module and status:
2088
+ # Обновление значения существующего ключа или добавление нового
2089
  pr5_data[module] = status
2090
  print(f"Updated JSON with key '{module}' and status '{status}'.")
2091
 
2092
  pr5_json = json.dumps(pr5_data)
2093
  print(f"Updated JSON to be saved: {pr5_json}")
2094
 
2095
+ # Получение текущей даты и времени в Московском часовом поясе
2096
  utc_now = datetime.utcnow()
2097
  msk_tz = pytz.timezone('Europe/Moscow')
2098
  msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
2099
  data_on = msk_now.strftime('%Y-%m-%d %H:%M:%S')
2100
 
2101
+ # Исключаем все столбцы, кроме name, phone, email, pr5, n_con, data_on
2102
  columns_to_update = ['name', 'phone', 'email', 'pr5', 'n_con', 'data_on']
2103
  values_to_update = [name, phone, email, pr5_json, n_con_flag, data_on]
2104
 
2105
  if result:
2106
+ # Обновляем только те поля, которые переданы в запросе
2107
  set_clause = ', '.join([f"{col} = ?" for col in columns_to_update])
2108
  query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?"
2109
  cursor.execute(query, values_to_update + [email, phone])
2110
  print("Record updated in database.")
2111
  else:
2112
+ # Вставляем новые данные
2113
  query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})"
2114
  cursor.execute(query, values_to_update)
2115
  print("New record inserted into database.")
2116
 
2117
  conn.commit()
2118
 
2119
+ # Проверка что данные сохранились правильно
2120
+ cursor.execute("SELECT * FROM contacts WHERE email = ? OR phone = ?", (email, phone))
2121
+ result_check = cursor.fetchone()
2122
+ if result_check:
2123
+ pr5_check = result_check[21] if result_check[21] else '{}'
2124
+ pr5_data_check = json.loads(pr5_check)
2125
+ print(f"Post-commit check. Loaded JSON: {pr5_data_check}")
2126
+ else:
2127
+ print("Post-commit check failed. No record found.")
2128
+
2129
+ # Замена NULL на пустые строки
2130
+ replace_null_with_empty_string(conn)
2131
  print("NULL values replaced with empty strings if any.")
2132
 
2133
  conn.close()
 
2143
 
2144
 
2145
 
2146
+
2147
  @app.route('/gc_in', methods=['GET'])
2148
  def add_data_gc_in():
2149
  global current_curator_index