Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 1 | """REST API for accessing log files.""" |
| 2 | from django.core.exceptions import ObjectDoesNotExist |
| 3 | from django.utils.decorators import method_decorator |
| 4 | from drf_yasg import openapi |
| 5 | from drf_yasg.utils import swagger_auto_schema |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 6 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 7 | from rest_framework import generics, status |
| 8 | from rest_framework.decorators import ( |
| 9 | api_view, |
| 10 | parser_classes, |
| 11 | permission_classes, |
| 12 | ) |
| 13 | from rest_framework.exceptions import ( |
| 14 | PermissionDenied, |
| 15 | NotFound, |
| 16 | ValidationError, |
| 17 | ) |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 18 | from rest_framework.parsers import FileUploadParser |
| 19 | from rest_framework.permissions import IsAuthenticated |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 20 | from rest_framework.response import Response |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 21 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 22 | from crashreports.response_descriptions import default_desc |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 23 | from crashreports.serializers import LogFileSerializer |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 24 | from crashreports.models import Crashreport, LogFile |
| 25 | from crashreports.permissions import ( |
| 26 | HasRightsOrIsDeviceOwnerDeviceCreation, |
| 27 | user_owns_uuid, |
| 28 | user_is_hiccup_staff, |
| 29 | ) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 30 | |
| 31 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 32 | @method_decorator( |
| 33 | name="get", |
| 34 | decorator=swagger_auto_schema(operation_description="List log files"), |
| 35 | ) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 36 | class ListCreateView(generics.ListAPIView): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 37 | """Endpoint for listing log files.""" |
| 38 | |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 39 | queryset = LogFile.objects.all() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 40 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 41 | serializer_class = LogFileSerializer |
| 42 | |
| 43 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 44 | @method_decorator( |
| 45 | name="get", |
| 46 | decorator=swagger_auto_schema( |
| 47 | operation_description="Get a log file", |
| 48 | responses=dict([default_desc(NotFound)]), |
| 49 | ), |
| 50 | ) |
| 51 | @method_decorator( |
| 52 | name="put", |
| 53 | decorator=swagger_auto_schema( |
| 54 | operation_description="Update a log file", |
| 55 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]), |
| 56 | ), |
| 57 | ) |
| 58 | @method_decorator( |
| 59 | name="patch", |
| 60 | decorator=swagger_auto_schema( |
| 61 | operation_description="Partially update a log file", |
| 62 | responses=dict([default_desc(NotFound), default_desc(ValidationError)]), |
| 63 | ), |
| 64 | ) |
| 65 | @method_decorator( |
| 66 | name="delete", |
| 67 | decorator=swagger_auto_schema( |
| 68 | operation_description="Delete a log file", |
| 69 | responses=dict([default_desc(NotFound)]), |
| 70 | ), |
| 71 | ) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 72 | class RetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 73 | """Endpoint for retrieving, updating and deleting log files.""" |
| 74 | |
| 75 | # pylint: disable=too-many-ancestors |
| 76 | |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 77 | queryset = LogFile.objects.all() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 78 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 79 | serializer_class = LogFileSerializer |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 80 | |
| 81 | |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 82 | @swagger_auto_schema( |
| 83 | method="post", |
| 84 | request_body=LogFileSerializer, |
| 85 | responses=dict( |
| 86 | [ |
| 87 | default_desc(ValidationError), |
| 88 | ( |
| 89 | status.HTTP_404_NOT_FOUND, |
| 90 | openapi.Response("Crashreport does not exist."), |
| 91 | ), |
| 92 | (status.HTTP_201_CREATED, openapi.Response("Created")), |
| 93 | ] |
| 94 | ), |
| 95 | ) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 96 | @api_view(http_method_names=["POST"]) |
| 97 | @parser_classes([FileUploadParser]) |
| 98 | @permission_classes([IsAuthenticated]) |
Dirk Vogt | 3663569 | 2016-10-17 12:19:10 +0200 | [diff] [blame] | 99 | def logfile_put(request, uuid, device_local_id, filename): |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 100 | """Upload a log file for a crash report.""" |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 101 | try: |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 102 | crashreport = Crashreport.objects.get( |
| 103 | device__uuid=uuid, device_local_id=device_local_id |
| 104 | ) |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 105 | except ObjectDoesNotExist: |
Dirk Vogt | 3663569 | 2016-10-17 12:19:10 +0200 | [diff] [blame] | 106 | raise NotFound(detail="Crashreport does not exist.") |
| 107 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 108 | if not ( |
| 109 | user_owns_uuid(request.user, crashreport.device.uuid) |
| 110 | or user_is_hiccup_staff(request.user) |
| 111 | ): |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 112 | raise PermissionDenied(detail="Not allowed.") |
Mitja Nikolaus | 1280ef4 | 2018-08-21 16:20:07 +0200 | [diff] [blame] | 113 | file = request.data["file"] |
| 114 | logfile = LogFile(crashreport=crashreport, logfile=file) |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 115 | logfile.save() |
Dirk Vogt | 3663569 | 2016-10-17 12:19:10 +0200 | [diff] [blame] | 116 | return Response(status=201) |