Coverage for apis_core/core/views.py: 69%

26 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-09-03 06:15 +0000

1import json 

2 

3from django.contrib import messages 

4from django.contrib.auth import views as auth_views 

5from django.urls import reverse_lazy 

6from django.utils.translation import gettext_lazy as _ 

7from drf_spectacular.utils import OpenApiParameter, extend_schema, inline_serializer 

8from rest_framework.permissions import IsAuthenticated 

9from rest_framework.response import Response 

10from rest_framework.views import APIView 

11 

12from apis_core.utils.helpers import datadump_serializer 

13 

14 

15class Dumpdata(APIView): 

16 """ 

17 provide an API endpoint that outputs the datadump of an APIS installation 

18 

19 this is a bit of a hack, becaus we first use the Django JSON serializer to 

20 serialize the data using natural keys, then we use json.loads to so we can 

21 output it as an API reponse. 

22 so basically: serialize -> deserialize -> serialize 

23 """ 

24 

25 permission_classes = [IsAuthenticated] 

26 

27 @extend_schema( 

28 parameters=[OpenApiParameter(name="app_labels", type=str, many=True)], 

29 responses={200: inline_serializer(name="DumpDataResponse", fields={})}, 

30 ) 

31 def get(self, request, *args, **kwargs): 

32 params = request.query_params.dict() 

33 app_labels = params.pop("app_labels", []) 

34 if app_labels: 

35 app_labels = app_labels.split(",") 

36 return Response(json.loads(datadump_serializer(app_labels, "json"))) 

37 

38 

39class PasswordChangeView(auth_views.PasswordChangeView): 

40 template_name = "profile/password-change.html" 

41 success_url = reverse_lazy("apis_core:password-change") 

42 

43 def form_valid(self, form): 

44 ret = super().form_valid(form) 

45 messages.success( 

46 self.request, 

47 _("Password for %(user)s changed.").format(user=self.request.user), 

48 ) 

49 return ret