DmitrMakeev commited on
Commit
e766c41
·
verified ·
1 Parent(s): ef4e030

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -771,54 +771,54 @@ def send_request():
771
 
772
  DATABASE_NAME = 'data_gc.db'
773
 
774
- def update_or_insert_user(db_name, user_data, mapping_template):
775
- conn = sqlite3.connect(db_name)
776
- cursor = conn.cursor()
777
-
778
- email = user_data.get('email')
779
-
780
- cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
781
- user = cursor.fetchone()
782
-
783
- transformed_data = {}
784
- for json_key, db_column in mapping_template.items():
785
- value = user_data.get(json_key, "")
786
- if isinstance(value, list):
787
- transformed_data[db_column] = "-1" if value else "0"
 
 
 
788
  else:
789
- transformed_data[db_column] = str(value)
790
-
791
- required_fields = [
792
- "vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
793
- "b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
794
- "shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
795
- "key_pr", "n_con", "canal", "data_t"
796
- ]
797
-
798
- for field in required_fields:
799
- if field not in transformed_data:
800
- transformed_data[field] = ""
801
-
802
- if user:
803
- web_st_value = int(user[0]) + 1
804
-
805
- update_query = "UPDATE contacts SET "
806
- update_values = []
807
- for column, value in transformed_data.items():
808
- update_query += f"{column} = ?, "
809
- update_values.append(value)
810
- update_query += "web_st = ? WHERE email = ?"
811
- update_values.extend([web_st_value, email])
812
- cursor.execute(update_query, update_values)
813
- else:
814
- columns = ', '.join(transformed_data.keys()) + ", web_st"
815
- placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
816
- insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
817
- insert_values = list(transformed_data.values()) + [0]
818
- cursor.execute(insert_query, insert_values)
819
 
820
- conn.commit()
821
- conn.close()
822
 
823
  @app.route('/send_get_request', methods=['GET'])
824
  def send_get_request():
@@ -848,10 +848,10 @@ def send_get_request():
848
 
849
  users_meta = report_json.get('usersMeta', {})
850
 
851
- # Обновление или добавление каждого пользователя в базу данных data_gc.db
852
  for user_id, user_data in users_meta.items():
853
  user_data['messages'] = messages_json
854
- update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
855
 
856
  return jsonify({'status': 'User data saved successfully'})
857
  else:
 
771
 
772
  DATABASE_NAME = 'data_gc.db'
773
 
774
+ def update_or_insert_user(email, user_data):
775
+ for db in DATABASES:
776
+ conn = sqlite3.connect(db)
777
+ cursor = conn.cursor()
778
+ cursor.execute('SELECT * FROM contacts WHERE email = ?', (email,))
779
+ existing_user = cursor.fetchone()
780
+
781
+ if existing_user:
782
+ # Обновление данных пользователя
783
+ update_data = {}
784
+ for key, value in user_data.items():
785
+ if key in mapping_template:
786
+ update_data[mapping_template[key]] = value
787
+ update_data['b_baners'] = -1 if user_data.get('banners', []) else 0
788
+ update_data['b_butt'] = -1 if user_data.get('buttons', []) else 0
789
+ update_query = 'UPDATE contacts SET ' + ', '.join([f'{k} = ?' for k in update_data.keys()]) + ' WHERE email = ?'
790
+ cursor.execute(update_query, list(update_data.values()) + [email])
791
  else:
792
+ # Добавление нового пользователя
793
+ insert_data = {
794
+ "name": user_data.get('username', ''),
795
+ "phone": user_data.get('phone', ''),
796
+ "email": user_data.get('email', ''),
797
+ "b_city": user_data.get('city', ''),
798
+ "b_fin": user_data.get('finished', ''),
799
+ "b_ban": user_data.get('ban', ''),
800
+ "b_ign": user_data.get('ignore', ''),
801
+ "b_baners": -1 if user_data.get('banners', []) else 0,
802
+ "b_butt": -1 if user_data.get('buttons', []) else 0,
803
+ "b_mess": user_data.get('messages', ''),
804
+ "shop_st": '',
805
+ "curator": '',
806
+ "pr1": '',
807
+ "pr2": '',
808
+ "pr3": '',
809
+ "pr4": '',
810
+ "pr5": '',
811
+ "ad_url": '',
812
+ "key_pr": '',
813
+ "n_con": '',
814
+ "canal": '',
815
+ "data_t": ''
816
+ }
817
+ insert_query = 'INSERT INTO contacts (' + ', '.join(insert_data.keys()) + ') VALUES (' + ', '.join(['?'] * len(insert_data)) + ')'
818
+ cursor.execute(insert_query, list(insert_data.values()))
 
 
 
819
 
820
+ conn.commit()
821
+ conn.close()
822
 
823
  @app.route('/send_get_request', methods=['GET'])
824
  def send_get_request():
 
848
 
849
  users_meta = report_json.get('usersMeta', {})
850
 
851
+ # Обновление или добавление каждого пользователя в базу данных
852
  for user_id, user_data in users_meta.items():
853
  user_data['messages'] = messages_json
854
+ update_or_insert_user(user_data.get('email'), user_data)
855
 
856
  return jsonify({'status': 'User data saved successfully'})
857
  else: