|
| 1 | +from rest_framework import serializers |
| 2 | +from kirovy import typing as t |
| 3 | +from kirovy.models import cnc_game |
| 4 | +from kirovy.serializers import KirovySerializer |
| 5 | + |
| 6 | + |
| 7 | +class CncFileExtensionSerializer(KirovySerializer): |
| 8 | + extension = serializers.CharField( |
| 9 | + max_length=32, |
| 10 | + allow_blank=False, |
| 11 | + ) |
| 12 | + |
| 13 | + about = serializers.CharField( |
| 14 | + max_length=2048, |
| 15 | + allow_null=True, |
| 16 | + allow_blank=False, |
| 17 | + required=False, |
| 18 | + ) |
| 19 | + |
| 20 | + extension_type = serializers.ChoiceField( |
| 21 | + choices=cnc_game.CncFileExtension.ExtensionTypes.choices, |
| 22 | + ) |
| 23 | + |
| 24 | + def create(self, validated_data: dict[str, t.Any]) -> cnc_game.CncFileExtension: |
| 25 | + return cnc_game.CncFileExtension.objects.create(**validated_data) |
| 26 | + |
| 27 | + def update( |
| 28 | + self, instance: cnc_game.CncFileExtension, validated_data: dict[str, t.Any] |
| 29 | + ) -> cnc_game.CncFileExtension: |
| 30 | + # For now, don't allow editing the extension. These likely shouldn't ever need to be updated. |
| 31 | + # instance.extension = validated_data.get("extension", instance.extension) |
| 32 | + instance.about = validated_data.get("about", instance.about) |
| 33 | + instance.extension_type = validated_data.get("extension_type", instance.extension_type) |
| 34 | + instance.save(update_fields=["about", "extension_type"]) |
| 35 | + instance.refresh_from_db() |
| 36 | + return instance |
| 37 | + |
| 38 | + class Meta: |
| 39 | + model = cnc_game.CncFileExtension |
| 40 | + exclude = ["last_modified_by"] |
| 41 | + fields = "__all__" |
| 42 | + |
| 43 | + |
| 44 | +class CncGameSerializer(KirovySerializer): |
| 45 | + slug = serializers.CharField(read_only=True, allow_null=False, allow_blank=False) |
| 46 | + full_name = serializers.CharField(allow_null=False, allow_blank=False) |
| 47 | + is_visible = serializers.BooleanField(allow_null=False, default=True) |
| 48 | + allow_public_uploads = serializers.BooleanField(allow_null=False, default=False) |
| 49 | + compatible_with_parent_maps = serializers.BooleanField(allow_null=False, default=False) |
| 50 | + is_mod = serializers.BooleanField(read_only=True, allow_null=False, default=False) |
| 51 | + allowed_extension_ids = serializers.PrimaryKeyRelatedField( |
| 52 | + source="allowed_extensions", |
| 53 | + pk_field=serializers.UUIDField(), |
| 54 | + many=True, |
| 55 | + read_only=True, # Set these manually using the ORM. |
| 56 | + ) |
| 57 | + |
| 58 | + parent_game_id = serializers.PrimaryKeyRelatedField( |
| 59 | + source="parent_game", |
| 60 | + pk_field=serializers.UUIDField(), |
| 61 | + many=False, |
| 62 | + allow_null=True, |
| 63 | + allow_empty=False, |
| 64 | + default=None, |
| 65 | + read_only=True, # parent_id affects file path generation so we can't change it via the API. |
| 66 | + ) |
| 67 | + |
| 68 | + class Meta: |
| 69 | + model = cnc_game.CncGame |
| 70 | + # We return the ID instead of the whole object. |
| 71 | + exclude = ["parent_game", "allowed_extensions"] |
| 72 | + fields = "__all__" |
| 73 | + |
| 74 | + def create(self, validated_data: t.DictStrAny) -> cnc_game.CncGame: |
| 75 | + instance = cnc_game.CncGame(**validated_data) |
| 76 | + instance.save() |
| 77 | + return instance |
| 78 | + |
| 79 | + def update(self, instance: cnc_game.CncGame, validated_data: t.DictStrAny) -> cnc_game.CncGame: |
| 80 | + instance.full_name = validated_data.get("full_name", instance.full_name) |
| 81 | + instance.is_visible = validated_data.get("is_visible", instance.is_visible) |
| 82 | + instance.is_mod = validated_data.get("is_mod", instance.is_mod) |
| 83 | + instance.allow_public_uploads = validated_data.get("allow_public_uploads", instance.allow_public_uploads) |
| 84 | + instance.compatible_with_parent_maps = validated_data.get( |
| 85 | + "compatible_with_parent_maps", instance.compatible_with_parent_maps |
| 86 | + ) |
| 87 | + instance.save( |
| 88 | + update_fields=["full_name", "is_visible", "is_mod", "allow_public_uploads", "compatible_with_parent_maps"] |
| 89 | + ) |
| 90 | + instance.refresh_from_db() |
| 91 | + return instance |
0 commit comments