DmitrMakeev commited on
Commit
52128d4
1 Parent(s): e766c41

Update app.py

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