Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame^] | 1 | from crashreports.models import Device |
| 2 | from crashreports.models import User |
| 3 | from crashreports.permissions import HasRightsOrIsDeviceOwnerDeviceCreation |
| 4 | from crashreports.serializers import DeviceSerializer |
| 5 | from django.contrib.auth.models import Permission |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 6 | from rest_framework import generics |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame^] | 7 | from rest_framework.authtoken.models import Token |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 8 | from rest_framework.decorators import api_view |
| 9 | from rest_framework.decorators import permission_classes |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 10 | from rest_framework.permissions import AllowAny |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 11 | from rest_framework.permissions import IsAuthenticated |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame^] | 12 | from rest_framework.response import Response |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 13 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 14 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 15 | class ListCreateDevices(generics.ListCreateAPIView): |
| 16 | queryset = Device.objects.all() |
| 17 | paginate_by = 20 |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame^] | 18 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 19 | serializer_class = DeviceSerializer |
| 20 | pass |
| 21 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 22 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 23 | class RetrieveUpdateDestroyDevice(generics.RetrieveUpdateDestroyAPIView): |
| 24 | queryset = Device.objects.all() |
Dirk Vogt | f3662f6 | 2016-12-12 16:43:06 +0100 | [diff] [blame^] | 25 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 26 | serializer_class = DeviceSerializer |
| 27 | lookup_field = 'uuid' |
| 28 | pass |
| 29 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 30 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 31 | @api_view(http_method_names=['POST'], ) |
| 32 | @permission_classes((AllowAny,)) |
| 33 | def register_device(request): |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 34 | """ Register a new device. This endpoint will generate a django user for |
| 35 | the new device. The device is identified by a uuid, and authenticated with |
| 36 | a token. |
| 37 | 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] | 38 | """ |
| 39 | device = Device() |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 40 | user = User.objects.create_user("device_" + str(device.uuid), '', None) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 41 | permission = Permission.objects.get(name='Can add crashreport') |
| 42 | user.user_permissions.add(permission) |
| 43 | user.save() |
Dirk Vogt | 7603a7a | 2016-11-23 17:19:58 +0100 | [diff] [blame] | 44 | device.board_date = request.data['board_date'] |
| 45 | device.chipset = request.data['chipset'] |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 46 | device.user = user |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 47 | device.token = Token.objects.create(user=user).key |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 48 | device.save() |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 49 | return Response({'uuid': device.uuid, 'token': device.token}) |