DmitrMakeev commited on
Commit
1502d60
·
verified ·
1 Parent(s): c3ba89f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -29
app.py CHANGED
@@ -100,7 +100,7 @@ for db in DATABASES:
100
 
101
 
102
 
103
- template = {
104
  "username": "name",
105
  "phone": "phone",
106
  "email": "email",
@@ -122,7 +122,6 @@ template = {
122
 
123
 
124
 
125
-
126
  # Функция для выполнения HTTP запросов
127
  def fetch(url):
128
  try:
@@ -781,41 +780,30 @@ def update_or_insert_user(db_name, user_data, mapping_template):
781
  cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
782
  user = cursor.fetchone()
783
 
784
- transformed_data = {}
785
- for json_key, db_column in mapping_template.items():
786
- value = user_data.get(json_key, "")
787
- if isinstance(value, list):
788
- transformed_data[db_column] = "-1" if value else "0"
789
- else:
790
- transformed_data[db_column] = str(value)
791
-
792
- required_fields = [
793
- "vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
794
- "b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
795
- "shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
796
- "key_pr", "n_con", "canal", "data_t"
797
- ]
798
-
799
- for field in required_fields:
800
- if field not in transformed_data:
801
- transformed_data[field] = ""
802
-
803
  if user:
804
  web_st_value = int(user[0]) + 1
805
-
806
  update_query = "UPDATE contacts SET "
807
  update_values = []
808
- for column, value in transformed_data.items():
809
- update_query += f"{column} = ?, "
810
- update_values.append(value)
 
 
 
811
  update_query += "web_st = ? WHERE email = ?"
812
  update_values.extend([web_st_value, email])
813
  cursor.execute(update_query, update_values)
814
  else:
815
- columns = ', '.join(transformed_data.keys()) + ", web_st"
816
- placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
817
  insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
818
- insert_values = list(transformed_data.values()) + [0]
 
 
 
 
 
 
819
  cursor.execute(insert_query, insert_values)
820
 
821
  conn.commit()
@@ -852,7 +840,7 @@ def send_get_request():
852
  # Обновление или добавление каждого пользователя в базу данных data_gc.db
853
  for user_id, user_data in users_meta.items():
854
  user_data['messages'] = messages_json
855
- update_or_insert_user(DATABASE_NAME, user_data, template)
856
 
857
  return jsonify({'status': 'User data saved successfully'})
858
  else:
 
100
 
101
 
102
 
103
+ mapping_template = {
104
  "username": "name",
105
  "phone": "phone",
106
  "email": "email",
 
122
 
123
 
124
 
 
125
  # Функция для выполнения HTTP запросов
126
  def fetch(url):
127
  try:
 
780
  cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
781
  user = cursor.fetchone()
782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
  if user:
784
  web_st_value = int(user[0]) + 1
 
785
  update_query = "UPDATE contacts SET "
786
  update_values = []
787
+ for json_key, db_column in mapping_template.items():
788
+ value = user_data.get(json_key, "")
789
+ if isinstance(value, list):
790
+ value = "-1" if value else "0"
791
+ update_query += f"{db_column} = ?, "
792
+ update_values.append(str(value))
793
  update_query += "web_st = ? WHERE email = ?"
794
  update_values.extend([web_st_value, email])
795
  cursor.execute(update_query, update_values)
796
  else:
797
+ columns = ', '.join(mapping_template.values()) + ", web_st"
798
+ placeholders = ', '.join('?' for _ in mapping_template) + ", ?"
799
  insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
800
+ insert_values = []
801
+ for json_key, db_column in mapping_template.items():
802
+ value = user_data.get(json_key, "")
803
+ if isinstance(value, list):
804
+ value = "-1" if value else "0"
805
+ insert_values.append(str(value))
806
+ insert_values.append(0)
807
  cursor.execute(insert_query, insert_values)
808
 
809
  conn.commit()
 
840
  # Обновление или добавление каждого пользователя в базу данных data_gc.db
841
  for user_id, user_data in users_meta.items():
842
  user_data['messages'] = messages_json
843
+ update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
844
 
845
  return jsonify({'status': 'User data saved successfully'})
846
  else: