Advertisement
cristi123

aaa

Mar 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. def save_lithrometric_probe_data(request, agency_id):
  2.     if request.method == "POST":
  3.         try:
  4.             json_data = json.loads(request.body)
  5.             sn = json_data['sn']
  6.             value = int(json_data['value'])
  7.             device_id = int(json_data['device_id'])
  8.             device = Device.objects.get(id=device_id)
  9.             probe, created = LithrometricProbe.objects.update_or_create(serial_number=sn, defaults={
  10.                 "probe_value": value,
  11.                 "device": device
  12.             })
  13.             probe.volume_value = get_volume_for_probe_value(probe, value)
  14.             probe.save()
  15.  
  16.             LithrometricValue.objects.create(
  17.                 lithrometric_probe=probe,
  18.                 probe_value=value,
  19.                 volume_value=probe.volume_value,
  20.             )
  21.             return JSONResponse({"success": True})
  22.         except ValueError as e:
  23.             return JSONResponse({"success": False, "err_msg": str(e)}, status=400)
  24.         except KeyError as e:
  25.             return JSONResponse({"success": False, "err_msg": "Missing value: " + str(e)}, status=400)
  26.     else:
  27.         return HttpResponse("ERROR: Invalid request method " + request.method)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement