Coverage for django_napse/api/keys/serializers/key.py: 72%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1from rest_framework import serializers 

2 

3from django_napse.api.permissions.serializers import PermissionSerializer 

4from django_napse.auth.models import NapseAPIKey 

5 

6 

7class NapseAPIKeySerializer(serializers.ModelSerializer): 

8 permissions = PermissionSerializer(many=True, read_only=True) 

9 

10 class Meta: 

11 model = NapseAPIKey 

12 fields = [ 

13 "name", 

14 "prefix", 

15 "permissions", 

16 "is_master_key", 

17 "revoked", 

18 "description", 

19 ] 

20 

21 

22class NapseAPIKeySpaceSerializer(serializers.ModelSerializer): 

23 class Meta: 

24 model = NapseAPIKey 

25 fields = [ 

26 "name", 

27 "prefix", 

28 "is_master_key", 

29 "revoked", 

30 "description", 

31 ] 

32 

33 def to_representation(self, instance): 

34 representation = super().to_representation(instance) 

35 representation["permissions"] = [] 

36 for permission in instance.permissions.filter(space__uuid=self.context["space"]): 

37 representation["permissions"].append(permission.permission_type) 

38 return representation