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
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/crateweb/anonymise_api/views.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
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.
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.
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/>.
24===============================================================================
26Django REST Framework view. This is the anonymisation API end point.
28"""
30from django.views.generic import TemplateView
32from rest_framework.parsers import JSONParser
33from rest_framework.request import Request
34from rest_framework.response import Response
35from rest_framework.views import APIView
37from crate_anon.crateweb.anonymise_api.serializers import (
38 ScrubSerializer,
39)
42class HomeView(TemplateView):
43 """
44 Displays the API menu.
45 """
47 template_name = "anonymise_api/home.html"
50class ScrubView(APIView):
51 """
52 Main CRATE anonymisation end-point.
53 """
55 # Only needed by drf_spectacular to generate documentation
56 serializer_class = ScrubSerializer
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]
63 # noinspection PyMethodMayBeStatic
64 def post(self, request: Request) -> Response:
65 serializer = ScrubSerializer(data=request.data)
67 # If the input is valid, this will do the anonymisation
68 serializer.is_valid(raise_exception=True)
70 return Response(serializer.data)