DmitrMakeev commited on
Commit
2ea87f5
·
verified ·
1 Parent(s): 84eac94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -983,43 +983,48 @@ if __name__ == "__main__":
983
  @app.route('/calculation', methods=['POST'])
984
  def handle_calculation():
985
  try:
986
- # 1. Получаем и проверяем данные
987
  data = request.get_json()
988
  if not data:
989
- raise ValueError("Не получены данные")
990
-
991
- # 2. Преобразуем данные клиента в серверный формат
992
- server_data = {
993
- 'fertilizerConstants': convert_client_data(data['fertilizerConstants']),
994
- 'profileSettings': {
995
- 'P': float(data['profileSettings']['P']),
996
- 'K': float(data['profileSettings']['K']),
997
- 'Mg': float(data['profileSettings']['Mg']),
998
- 'Ca': float(data['profileSettings']['Ca']),
999
- 'S': float(data['profileSettings']['S']),
1000
- 'N (NO3-)': float(data['profileSettings']['N (NO3-)']), # Берем из фронтенда
1001
- 'N (NH4+)': float(data['profileSettings']['N (NH4+)']), # Берем из фронтенда
1002
- 'liters': int(data['profileSettings']['liters'])
 
 
 
 
 
 
 
1003
  }
1004
- }
 
1005
 
1006
- # 3. Используем ТОЛЬКО оригинальный калькулятор
1007
  calculator = NutrientCalculator(volume_liters=server_data['profileSettings']['liters'])
1008
-
1009
- # 4. Вручную переопределяем target_profile перед расчетом
1010
  calculator.target_profile.update(server_data['profileSettings'])
1011
  calculator.fertilizers = server_data['fertilizerConstants']
1012
-
1013
- # 5. Выполняем расчет
1014
  calculator.calculate()
1015
 
1016
- # 6. Возвращаем результаты
1017
  return jsonify(calculator.get_web_results())
1018
-
1019
  except Exception as e:
 
 
1020
  return jsonify({
1021
- "error": str(e),
1022
- "message": "Ошибка расчета"
1023
  }), 500
1024
 
1025
 
 
983
  @app.route('/calculation', methods=['POST'])
984
  def handle_calculation():
985
  try:
986
+ # 1. Жесткая проверка данных
987
  data = request.get_json()
988
  if not data:
989
+ return jsonify({"error": "No JSON data"}), 400
990
+
991
+ # 2. Валидация структуры
992
+ required = {'fertilizerConstants', 'profileSettings'}
993
+ if not required.issubset(data.keys()):
994
+ return jsonify({"error": f"Required fields: {required}"}), 400
995
+
996
+ # 3. Конвертация данных
997
+ try:
998
+ server_data = {
999
+ 'fertilizerConstants': convert_client_data(data['fertilizerConstants']),
1000
+ 'profileSettings': {
1001
+ 'P': float(data['profileSettings']['P']),
1002
+ 'K': float(data['profileSettings']['K']),
1003
+ 'Mg': float(data['profileSettings']['Mg']),
1004
+ 'Ca': float(data['profileSettings']['Ca']),
1005
+ 'S': float(data['profileSettings']['S']),
1006
+ 'N (NO3-)': float(data['profileSettings']['N (NO3-)']),
1007
+ 'N (NH4+)': float(data['profileSettings']['N (NH4+)']),
1008
+ 'liters': int(data['profileSettings']['liters'])
1009
+ }
1010
  }
1011
+ except (ValueError, KeyError) as e:
1012
+ return jsonify({"error": f"Invalid data format: {str(e)}"}), 400
1013
 
1014
+ # 4. Расчет
1015
  calculator = NutrientCalculator(volume_liters=server_data['profileSettings']['liters'])
 
 
1016
  calculator.target_profile.update(server_data['profileSettings'])
1017
  calculator.fertilizers = server_data['fertilizerConstants']
 
 
1018
  calculator.calculate()
1019
 
 
1020
  return jsonify(calculator.get_web_results())
1021
+
1022
  except Exception as e:
1023
+ # Логируем ошибку для диагностики
1024
+ print(f"Calculation error: {str(e)}")
1025
  return jsonify({
1026
+ "error": "Internal server error",
1027
+ "details": str(e)
1028
  }), 500
1029
 
1030