Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 1 | """REST API for accessing heartbeats.""" |
| 2 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 3 | from django.utils.decorators import method_decorator |
| 4 | from drf_yasg import openapi |
| 5 | from drf_yasg.utils import swagger_auto_schema |
| 6 | from rest_framework import generics, status |
| 7 | from rest_framework.exceptions import NotFound, ValidationError |
| 8 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 9 | from crashreports.models import HeartBeat |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 10 | from crashreports.permissions import ( |
| 11 | HasRightsOrIsDeviceOwnerDeviceCreation, |
| 12 | SWAGGER_SECURITY_REQUIREMENTS_ALL, |
| 13 | ) |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 14 | from crashreports.response_descriptions import default_desc |
| 15 | from crashreports.serializers import HeartBeatSerializer |
Mitja Nikolaus | e2928eb | 2018-11-02 15:07:20 +0100 | [diff] [blame] | 16 | from crashreports.utils import get_object_by_lookup_fields |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 17 | |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 18 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 19 | @method_decorator( |
| 20 | name="get", |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 21 | decorator=swagger_auto_schema( |
| 22 | operation_description="List heartbeats", |
| 23 | security=SWAGGER_SECURITY_REQUIREMENTS_ALL, |
| 24 | ), |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 25 | ) |
| 26 | @method_decorator( |
| 27 | name="post", |
| 28 | decorator=swagger_auto_schema( |
| 29 | operation_description="Create a heartbeat", |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 30 | security=SWAGGER_SECURITY_REQUIREMENTS_ALL, |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 31 | request_body=HeartBeatSerializer, |
| 32 | responses=dict( |
| 33 | [ |
| 34 | default_desc(ValidationError), |
| 35 | ( |
| 36 | status.HTTP_404_NOT_FOUND, |
| 37 | openapi.Response( |
| 38 | "No device with the given uuid could be found." |
| 39 | ), |
| 40 | ), |
| 41 | ] |
| 42 | ), |
| 43 | ), |
| 44 | ) |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 45 | class ListCreateView(generics.ListCreateAPIView): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 46 | """Endpoint for listing heartbeats and creating new heartbeats.""" |
| 47 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 48 | queryset = HeartBeat.objects.all() |
| 49 | paginate_by = 20 |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 50 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 51 | serializer_class = HeartBeatSerializer |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 52 | filter_fields = ("device", "build_fingerprint", "radio_version") |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 53 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 54 | def get(self, request, *args, **kwargs): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 55 | """Override device__uuid parameter with uuid.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 56 | if "uuid" in kwargs: |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 57 | self.queryset = HeartBeat.objects.filter( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 58 | device__uuid=kwargs["uuid"] |
| 59 | ) |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 60 | return generics.ListCreateAPIView.get(self, request, *args, **kwargs) |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 61 | |
| 62 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 63 | @method_decorator( |
| 64 | name="get", |
| 65 | decorator=swagger_auto_schema( |
| 66 | operation_description="Get a heartbeat", |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 67 | security=SWAGGER_SECURITY_REQUIREMENTS_ALL, |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 68 | responses=dict([default_desc(NotFound)]), |
| 69 | ), |
| 70 | ) |
| 71 | @method_decorator( |
| 72 | name="put", |
| 73 | decorator=swagger_auto_schema( |
| 74 | operation_description="Update a heartbeat", |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 75 | security=SWAGGER_SECURITY_REQUIREMENTS_ALL, |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 76 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]), |
| 77 | ), |
| 78 | ) |
| 79 | @method_decorator( |
| 80 | name="patch", |
| 81 | decorator=swagger_auto_schema( |
| 82 | operation_description="Partially update a heartbeat", |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 83 | security=SWAGGER_SECURITY_REQUIREMENTS_ALL, |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 84 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]), |
| 85 | ), |
| 86 | ) |
| 87 | @method_decorator( |
| 88 | name="delete", |
| 89 | decorator=swagger_auto_schema( |
| 90 | operation_description="Delete a heartbeat", |
Mitja Nikolaus | 4d759da | 2018-08-28 15:31:29 +0200 | [diff] [blame] | 91 | security=SWAGGER_SECURITY_REQUIREMENTS_ALL, |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 92 | responses=dict([default_desc(NotFound)]), |
| 93 | ), |
| 94 | ) |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 95 | class RetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 96 | """Endpoint for retrieving, updating and deleting heartbeats.""" |
| 97 | |
| 98 | # pylint: disable=too-many-ancestors |
| 99 | |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 100 | queryset = HeartBeat.objects.all() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 101 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 102 | serializer_class = HeartBeatSerializer |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 103 | multiple_lookup_fields = {"id", "device__uuid", "device_local_id"} |
Dirk Vogt | e178488 | 2016-10-13 16:09:38 +0200 | [diff] [blame] | 104 | |
| 105 | def get_object(self): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 106 | """Retrieve a heartbeat.""" |
Mitja Nikolaus | e2928eb | 2018-11-02 15:07:20 +0100 | [diff] [blame] | 107 | return get_object_by_lookup_fields(self, self.multiple_lookup_fields) |