Coverage for django_napse/api/fleets/serializers/cluster_serialisers.py: 62%

24 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.bots.serializers import BotSerializer 

4from django_napse.core.models import Bot, Cluster 

5 

6 

7class ClusterSerializerV1(serializers.ModelSerializer): 

8 template_bot = BotSerializer() 

9 

10 class Meta: 

11 model = Cluster 

12 fields = [ 

13 "template_bot", 

14 "share", 

15 "breakpoint", 

16 "autoscale", 

17 ] 

18 

19 

20class ClusterFormatterSerializer(serializers.Serializer): 

21 """Format cluster dictionnary for fleet creation.""" 

22 

23 template_bot = serializers.UUIDField(required=True) 

24 share = serializers.FloatField(required=True) 

25 breakpoint = serializers.IntegerField(required=True) 

26 autoscale = serializers.BooleanField(required=True) 

27 

28 def validate(self, data): 

29 data = super().validate(data) 

30 try: 

31 bot = Bot.objects.get(uuid=data.pop("template_bot")) 

32 except Bot.DoesNotExist: 

33 error_msg: str = "Template bot does not exist." 

34 raise serializers.ValidationError(error_msg) from None 

35 data["template_bot"] = bot 

36 return data 

37 

38 def create(self, validated_data): 

39 return super().create(validated_data)