Coverage for crateweb/anonymise_api/views.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-08-27 10:34 -0500

1""" 

2crate_anon/crateweb/anonymise_api/views.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

11 CRATE is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CRATE is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CRATE. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26Django REST Framework view. This is the anonymisation API end point. 

27 

28""" 

29 

30from django.views.generic import TemplateView 

31 

32from rest_framework.parsers import JSONParser 

33from rest_framework.request import Request 

34from rest_framework.response import Response 

35from rest_framework.views import APIView 

36 

37from crate_anon.crateweb.anonymise_api.serializers import ( 

38 ScrubSerializer, 

39) 

40 

41 

42class HomeView(TemplateView): 

43 """ 

44 Displays the API menu. 

45 """ 

46 

47 template_name = "anonymise_api/home.html" 

48 

49 

50class ScrubView(APIView): 

51 """ 

52 Main CRATE anonymisation end-point. 

53 """ 

54 

55 # Only needed by drf_spectacular to generate documentation 

56 serializer_class = ScrubSerializer 

57 

58 # Not currently supporting MultiPartParser for multipart/form-data. 

59 # Requires some work to get the nested serializers handling the data 

60 # correctly, particularly in the Browseable API form. See git history. 

61 parser_classes = [JSONParser] 

62 

63 # noinspection PyMethodMayBeStatic 

64 def post(self, request: Request) -> Response: 

65 serializer = ScrubSerializer(data=request.data) 

66 

67 # If the input is valid, this will do the anonymisation 

68 serializer.is_valid(raise_exception=True) 

69 

70 return Response(serializer.data)