第二周第二次
优化了之前代码的逻辑,增加了判断错误以及错误返回信息。
代码改进:
class CropaView(APIView):
def post(self, request):
try:
cropinput = request.data
crops = models.Crop.objects.filter(city=cropinput["city"], crop_type=cropinput["type"])
if not crops:
return JsonResponse({'message': '查询城市作物不存在'}, status=404)
crop_list = []
for crop in crops:
crop_dict = {
'city': crop.city,
'crop_type': crop.crop_type,
'weight_all': crop.weight_all
}
crop_list.append(crop_dict)
return JsonResponse(crop_list, safe=False)
except Exception as e:
return JsonResponse({'message': '请求异常'})
之后将相应的后端功能进行同样的判断错误情况优化
实现了相应的接口里的功能:
实现效果如下:
代码实现:
class CropCompareView(APIView):
def post(self, request):
try:
cropinput = request.data
crops1 = models.Crop.objects.filter(city=cropinput["city1"], crop_type=cropinput["type"],
year=cropinput["year"])
crops2 = models.Crop.objects.filter(city=cropinput["city2"], crop_type=cropinput["type"],
year=cropinput["year"])
if not crops1:
return JsonResponse({'message': '城市1该年不存在作物'})
if not crops2:
return JsonResponse({'message': '城市2该年不存在作物'})
for crop1 in crops1:
for crop2 in crops2:
crop_dict = {
'city1': crop1.city,
'city2': crop2.city,
'crop_type': crop1.crop_type,
'year': crop1.year,
'area1': crop1.area,
'weight_all1': crop1.weight_all,
'weight_avg1': crop1.weight_avg,
'area2': crop2.area,
'weight_all2': crop2.weight_all,
'weight_avg2': crop2.weight_avg,
}
return JsonResponse(crop_dict, safe=False)
except Exception as e:
return JsonResponse({'message': '请求异常'})
class TemperatureCompareView(APIView):
def post(self, request):
try:
climateinput = request.data
climates1 = models.Climate.objects.filter(city=climateinput["city1"], year=climateinput["year"])
climates2 = models.Climate.objects.filter(city=climateinput["city2"], year=climateinput["year"])
if not climates1:
return JsonResponse({'message': '城市1该年数据不存在'})
if not climates2:
return JsonResponse({'message': '城市2该年数据不存在'})
cliamte_list = []
for climate1 in climates1:
for climate2 in climates2:
if climate1.month==climate2.month:
cliamte_dict = {
'city1': climate1.city,
'city2': climate2.city,
'year': climate1.year,
'month': climate1.month,
'temperature1': climate1.temperature,
'temperature2': climate2.temperature,
}
cliamte_list.append(cliamte_dict)
return JsonResponse(cliamte_list, safe=False)
except Exception as e:
return JsonResponse({'message': '请求异常'})
class RainCompareView(APIView):
def post(self, request):
try:
climateinput = request.data
climates1 = models.Climate.objects.filter(city=climateinput["city1"], year=climateinput["year"])
climates2 = models.Climate.objects.filter(city=climateinput["city2"], year=climateinput["year"])
if not climates1:
return JsonResponse({'message': '城市1该年数据不存在'})
if not climates2:
return JsonResponse({'message': '城市2该年数据不存在'})
cliamte_list = []
for climate1 in climates1:
for climate2 in climates2:
if climate1.month==climate2.month:
cliamte_dict = {
'city1': climate1.city,
'city2': climate2.city,
'year': climate1.year,
'month': climate1.month,
'rain1': climate1.rain,
'rain2': climate2.rain,
}
cliamte_list.append(cliamte_dict)
return JsonResponse(cliamte_list, safe=False)
except Exception as e:
return JsonResponse({'message': '请求异常'})