· 5 years ago · Aug 24, 2020, 02:50 PM
1 if pk < 0:
2 return HttpResponse(status=400)
3
4 path = request.path
5 # check if the name already exists
6 try:
7 # Take the body in json and give me dictionary
8 json_body = json.loads(request.body)
9 except JSONDecodeError:
10 # Bad request
11 return HttpResponse(status=400)
12 if "devices" in path:
13 # Check if advisory exists.
14 try:
15 bb_found_advisory = BbAdvisory.objects.get(pk=pk)
16 print(bb_found_advisory.id)
17 except BbAdvisory.DoesNotExist:
18 return JsonResponse({"Result: ": "Questa segnalazione non esiste"}, status=404)
19 # Check if device exists.
20 try:
21 bb_found_device = BbDevice.objects.get(pk=json_body['device_id'])
22 except BbDevice.DoesNotExist:
23 return JsonResponse({"Result: ": "Questo Device non esiste"}, status=404)
24 # Check if advisory_id already exists.
25
26 bb_advisory_found_hook = R_BbHookAdvisory.objects.filter(advisory_id__exact=pk)
27 print(bb_found_advisory)
28 # If not exists create the relation
29 if not bb_advisory_found_hook:
30
31 # Create new items in table
32 hook_factory = BbHookFactory()
33 hook_code_factory = uuid.uuid1()
34 print(f'Questo è il codice hook_factory: {hook_code_factory}')
35 hook_factory.code = hook_code_factory
36 hook_factory.save()
37
38 # Generate the association between hook code and mission
39 hook_advisory = R_BbHookAdvisory()
40 hook_advisory.hook_id = hook_factory
41 hook_advisory.advisory_id = bb_found_advisory
42 hook_advisory.device_id = bb_found_device
43 hook_advisory.save()
44 # Return JSON with id of new record in database created
45 return JsonResponse({'id': hook_advisory.device_id}, status=201)