blob: 542f4e07725a4effb5740a6e1d88a64b30de5b04 [file] [log] [blame]
Dirk Vogtf3662f62016-12-12 16:43:06 +01001from crashreports.models import Device
2from crashreports.models import User
3from crashreports.permissions import HasRightsOrIsDeviceOwnerDeviceCreation
4from crashreports.serializers import DeviceSerializer
5from django.contrib.auth.models import Permission
Dirk Vogtf2a33422016-10-11 17:17:26 +02006from rest_framework import generics
Dirk Vogtf3662f62016-12-12 16:43:06 +01007from rest_framework.authtoken.models import Token
Dirk Vogtf2a33422016-10-11 17:17:26 +02008from rest_framework.decorators import api_view
9from rest_framework.decorators import permission_classes
Dirk Vogtf2a33422016-10-11 17:17:26 +020010from rest_framework.permissions import AllowAny
Dirk Vogtf2a33422016-10-11 17:17:26 +020011from rest_framework.permissions import IsAuthenticated
Dirk Vogtf3662f62016-12-12 16:43:06 +010012from rest_framework.response import Response
Dirk Vogtf2a33422016-10-11 17:17:26 +020013
Dirk Vogt7160b5e2016-10-12 17:04:40 +020014
Dirk Vogtf2a33422016-10-11 17:17:26 +020015class ListCreateDevices(generics.ListCreateAPIView):
16 queryset = Device.objects.all()
17 paginate_by = 20
Dirk Vogtf3662f62016-12-12 16:43:06 +010018 permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, )
Dirk Vogtf2a33422016-10-11 17:17:26 +020019 serializer_class = DeviceSerializer
20 pass
21
Dirk Vogt7160b5e2016-10-12 17:04:40 +020022
Dirk Vogtf2a33422016-10-11 17:17:26 +020023class RetrieveUpdateDestroyDevice(generics.RetrieveUpdateDestroyAPIView):
24 queryset = Device.objects.all()
Dirk Vogtf3662f62016-12-12 16:43:06 +010025 permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, )
Dirk Vogtf2a33422016-10-11 17:17:26 +020026 serializer_class = DeviceSerializer
27 lookup_field = 'uuid'
28 pass
29
Dirk Vogt7160b5e2016-10-12 17:04:40 +020030
Dirk Vogtf2a33422016-10-11 17:17:26 +020031@api_view(http_method_names=['POST'], )
32@permission_classes((AllowAny,))
33def register_device(request):
Dirk Vogt7160b5e2016-10-12 17:04:40 +020034 """ 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 Vogtf2a33422016-10-11 17:17:26 +020038 """
39 device = Device()
Dirk Vogt7160b5e2016-10-12 17:04:40 +020040 user = User.objects.create_user("device_" + str(device.uuid), '', None)
Dirk Vogtf2a33422016-10-11 17:17:26 +020041 permission = Permission.objects.get(name='Can add crashreport')
42 user.user_permissions.add(permission)
43 user.save()
Dirk Vogt7603a7a2016-11-23 17:19:58 +010044 device.board_date = request.data['board_date']
45 device.chipset = request.data['chipset']
Dirk Vogtf2a33422016-10-11 17:17:26 +020046 device.user = user
Dirk Vogt7160b5e2016-10-12 17:04:40 +020047 device.token = Token.objects.create(user=user).key
Dirk Vogtf2a33422016-10-11 17:17:26 +020048 device.save()
Dirk Vogt7160b5e2016-10-12 17:04:40 +020049 return Response({'uuid': device.uuid, 'token': device.token})