blob: 3934db6007f86ef396aa75c777ca3e27925ec637 [file] [log] [blame]
Dirk Vogt83107df2017-05-02 12:04:19 +02001from crashreports.models import *
Dirk Vogtf3662f62016-12-12 16:43:06 +01002from crashreports.permissions import HasRightsOrIsDeviceOwnerDeviceCreation
3from crashreports.serializers import DeviceSerializer
4from django.contrib.auth.models import Permission
Dirk Vogtf2a33422016-10-11 17:17:26 +02005from rest_framework import generics
Dirk Vogtf3662f62016-12-12 16:43:06 +01006from rest_framework.authtoken.models import Token
Dirk Vogtf2a33422016-10-11 17:17:26 +02007from rest_framework.decorators import api_view
8from rest_framework.decorators import permission_classes
Dirk Vogtf2a33422016-10-11 17:17:26 +02009from rest_framework.permissions import AllowAny
Dirk Vogtf2a33422016-10-11 17:17:26 +020010from rest_framework.permissions import IsAuthenticated
Dirk Vogtf3662f62016-12-12 16:43:06 +010011from rest_framework.response import Response
Dirk Vogt83107df2017-05-02 12:04:19 +020012from rest_framework.views import APIView
Dirk Vogt7160b5e2016-10-12 17:04:40 +020013
Dirk Vogtf2a33422016-10-11 17:17:26 +020014class ListCreateDevices(generics.ListCreateAPIView):
15 queryset = Device.objects.all()
16 paginate_by = 20
Dirk Vogtf3662f62016-12-12 16:43:06 +010017 permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation, )
Dirk Vogtf2a33422016-10-11 17:17:26 +020018 serializer_class = DeviceSerializer
Dirk Vogt83107df2017-05-02 12:04:19 +020019 filter_fields = ('uuid', 'board_date', 'chipset')
Dirk Vogtf2a33422016-10-11 17:17:26 +020020 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})