Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 1 | """REST API for accessing devices.""" |
| 2 | |
| 3 | from django.contrib.auth.models import Permission |
| 4 | from django.utils.decorators import method_decorator |
| 5 | from drf_yasg import openapi |
| 6 | from drf_yasg.utils import swagger_auto_schema |
| 7 | from rest_framework import generics, status |
| 8 | from rest_framework.exceptions import NotFound, ValidationError |
| 9 | from rest_framework.authtoken.models import Token |
| 10 | from rest_framework.decorators import api_view, permission_classes |
| 11 | from rest_framework.permissions import AllowAny |
| 12 | from rest_framework.response import Response |
| 13 | |
| 14 | from crashreports.models import Device, User |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame] | 15 | from crashreports.permissions import HasRightsOrIsDeviceOwnerDeviceCreation |
Franz-Xaver Geiger | d994335 | 2018-02-27 14:26:41 +0100 | [diff] [blame] | 16 | from crashreports.serializers import DeviceSerializer, DeviceCreateSerializer |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 17 | from crashreports.response_descriptions import default_desc |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 18 | |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 19 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 20 | @method_decorator( |
| 21 | name="get", |
| 22 | decorator=swagger_auto_schema(operation_description="List devices"), |
| 23 | ) |
| 24 | @method_decorator( |
| 25 | name="post", |
| 26 | decorator=swagger_auto_schema( |
| 27 | operation_description="Create a device", |
| 28 | responses=dict([default_desc(ValidationError)]), |
| 29 | ), |
| 30 | ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 31 | class ListCreateDevices(generics.ListCreateAPIView): |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 32 | """Endpoint for listing devices and creating new devices.""" |
| 33 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 34 | queryset = Device.objects.all() |
| 35 | paginate_by = 20 |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 36 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 37 | serializer_class = DeviceSerializer |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 38 | filter_fields = ("uuid", "board_date", "chipset") |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 39 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 40 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 41 | @method_decorator( |
| 42 | name="get", |
| 43 | decorator=swagger_auto_schema( |
| 44 | operation_description="Get a device", |
| 45 | responses=dict([default_desc(NotFound)]), |
| 46 | ), |
| 47 | ) |
| 48 | @method_decorator( |
| 49 | name="put", |
| 50 | decorator=swagger_auto_schema( |
| 51 | operation_description="Update a device", |
| 52 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]), |
| 53 | ), |
| 54 | ) |
| 55 | @method_decorator( |
| 56 | name="patch", |
| 57 | decorator=swagger_auto_schema( |
| 58 | operation_description="Make a partial update for a device", |
| 59 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]), |
| 60 | ), |
| 61 | ) |
| 62 | @method_decorator( |
| 63 | name="delete", |
| 64 | decorator=swagger_auto_schema( |
| 65 | operation_description="Delete a device", |
| 66 | responses=dict([default_desc(NotFound)]), |
| 67 | ), |
| 68 | ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 69 | class RetrieveUpdateDestroyDevice(generics.RetrieveUpdateDestroyAPIView): |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 70 | """Endpoint for retrieving, updating, patching and deleting devices.""" |
| 71 | |
| 72 | # pylint: disable=too-many-ancestors |
| 73 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 74 | queryset = Device.objects.all() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 75 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 76 | serializer_class = DeviceSerializer |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 77 | lookup_field = "uuid" |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 78 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 79 | |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 80 | class DeviceRegisterResponseSchema(DeviceSerializer): |
| 81 | """Response schema for successful device registration.""" |
| 82 | |
| 83 | class Meta: # noqa: D106 |
| 84 | model = Device |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 85 | fields = ["uuid", "token"] |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 86 | |
| 87 | |
| 88 | @swagger_auto_schema( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 89 | method="post", |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 90 | request_body=DeviceCreateSerializer, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 91 | responses=dict( |
| 92 | [ |
| 93 | default_desc(ValidationError), |
| 94 | ( |
| 95 | status.HTTP_200_OK, |
| 96 | openapi.Response( |
| 97 | "The device has been successfully registered.", |
| 98 | DeviceRegisterResponseSchema, |
| 99 | ), |
| 100 | ), |
| 101 | ] |
| 102 | ), |
| 103 | ) |
| 104 | @api_view(http_method_names=["POST"]) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 105 | @permission_classes((AllowAny,)) |
| 106 | def register_device(request): |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 107 | """Register a new device. |
| 108 | |
| 109 | This endpoint will generate a django user for the new device. The device is |
| 110 | identified by a uuid, and authenticated with a token. |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 111 | We generate the uuid here as this makes it easier to deal with collisions. |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 112 | """ |
Franz-Xaver Geiger | d994335 | 2018-02-27 14:26:41 +0100 | [diff] [blame] | 113 | serializer = DeviceCreateSerializer(data=request.data) |
| 114 | serializer.is_valid(raise_exception=True) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 115 | device = Device() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 116 | user = User.objects.create_user("device_" + str(device.uuid), "", None) |
| 117 | permission = Permission.objects.get(name="Can add crashreport") |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 118 | user.user_permissions.add(permission) |
| 119 | user.save() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 120 | device.board_date = serializer.validated_data["board_date"] |
| 121 | device.chipset = serializer.validated_data["chipset"] |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 122 | device.user = user |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 123 | device.token = Token.objects.create(user=user).key |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 124 | device.save() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 125 | return Response({"uuid": device.uuid, "token": device.token}) |