Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
990 |
-
|
991 |
-
# 2.
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
'
|
1000 |
-
'
|
1001 |
-
|
1002 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1003 |
}
|
1004 |
-
|
|
|
1005 |
|
1006 |
-
#
|
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":
|
1022 |
-
"
|
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 |
|