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 | |
| 20 | @method_decorator(name='get', decorator=swagger_auto_schema( |
| 21 | operation_description='List devices')) |
| 22 | @method_decorator(name='post', decorator=swagger_auto_schema( |
| 23 | operation_description='Create a device', |
| 24 | responses=dict([default_desc(ValidationError)]))) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 25 | class ListCreateDevices(generics.ListCreateAPIView): |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 26 | """Endpoint for listing devices and creating new devices.""" |
| 27 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 28 | queryset = Device.objects.all() |
| 29 | paginate_by = 20 |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame] | 30 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 31 | serializer_class = DeviceSerializer |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 32 | filter_fields = ('uuid', 'board_date', 'chipset') |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 33 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 34 | |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 35 | @method_decorator(name='get', decorator=swagger_auto_schema( |
| 36 | operation_description='Get a device', |
| 37 | responses=dict([default_desc(NotFound)]))) |
| 38 | @method_decorator(name='put', decorator=swagger_auto_schema( |
| 39 | operation_description='Update a device', |
| 40 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]))) |
| 41 | @method_decorator(name='patch', decorator=swagger_auto_schema( |
| 42 | operation_description='Make a partial update for a device', |
| 43 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]))) |
| 44 | @method_decorator(name='delete', decorator=swagger_auto_schema( |
| 45 | operation_description='Delete a device', |
| 46 | responses=dict([default_desc(NotFound)]))) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 47 | class RetrieveUpdateDestroyDevice(generics.RetrieveUpdateDestroyAPIView): |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 48 | """Endpoint for retrieving, updating, patching and deleting devices.""" |
| 49 | |
| 50 | # pylint: disable=too-many-ancestors |
| 51 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 52 | queryset = Device.objects.all() |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame] | 53 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 54 | serializer_class = DeviceSerializer |
| 55 | lookup_field = 'uuid' |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 56 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 57 | |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 58 | class DeviceRegisterResponseSchema(DeviceSerializer): |
| 59 | """Response schema for successful device registration.""" |
| 60 | |
| 61 | class Meta: # noqa: D106 |
| 62 | model = Device |
| 63 | fields = ['uuid', 'token'] |
| 64 | |
| 65 | |
| 66 | @swagger_auto_schema( |
| 67 | method='post', |
| 68 | request_body=DeviceCreateSerializer, |
| 69 | responses=dict([ |
| 70 | default_desc(ValidationError), |
| 71 | (status.HTTP_200_OK, |
| 72 | openapi.Response('The device has been successfully registered.', |
| 73 | DeviceRegisterResponseSchema)) |
| 74 | ])) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 75 | @api_view(http_method_names=['POST'], ) |
| 76 | @permission_classes((AllowAny,)) |
| 77 | def register_device(request): |
Mitja Nikolaus | e920808 | 2018-07-30 14:22:09 +0200 | [diff] [blame] | 78 | """Register a new device. |
| 79 | |
| 80 | This endpoint will generate a django user for the new device. The device is |
| 81 | identified by a uuid, and authenticated with a token. |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 82 | 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] | 83 | """ |
Franz-Xaver Geiger | d994335 | 2018-02-27 14:26:41 +0100 | [diff] [blame] | 84 | serializer = DeviceCreateSerializer(data=request.data) |
| 85 | serializer.is_valid(raise_exception=True) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 86 | device = Device() |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 87 | user = User.objects.create_user("device_" + str(device.uuid), '', None) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 88 | permission = Permission.objects.get(name='Can add crashreport') |
| 89 | user.user_permissions.add(permission) |
| 90 | user.save() |
Franz-Xaver Geiger | d994335 | 2018-02-27 14:26:41 +0100 | [diff] [blame] | 91 | device.board_date = serializer.validated_data['board_date'] |
| 92 | device.chipset = serializer.validated_data['chipset'] |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 93 | device.user = user |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 94 | device.token = Token.objects.create(user=user).key |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 95 | device.save() |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 96 | return Response({'uuid': device.uuid, 'token': device.token}) |