-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Game endpoints #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
kirovy/migrations/0020_alter_cncmapfile_file_alter_cncmapimagefile_file_and_more.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Generated by Django 4.2.26 on 2025-11-23 00:19 | ||
|
|
||
| from django.db import migrations, models | ||
| import kirovy.models.file_base | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("kirovy", "0019_alter_cncmapimagefile_file"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="cncmapfile", | ||
| name="file", | ||
| field=models.FileField(max_length=2048, upload_to=kirovy.models.file_base.default_generate_upload_to), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="cncmapimagefile", | ||
| name="file", | ||
| field=models.ImageField(max_length=2048, upload_to=kirovy.models.file_base.default_generate_upload_to), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="mappreview", | ||
| name="file", | ||
| field=models.FileField(max_length=2048, upload_to=kirovy.models.file_base.default_generate_upload_to), | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| from rest_framework import serializers | ||
| from kirovy import typing as t | ||
| from kirovy.models import cnc_game | ||
| from kirovy.serializers import KirovySerializer | ||
|
|
||
|
|
||
| class CncFileExtensionSerializer(KirovySerializer): | ||
| extension = serializers.CharField( | ||
| max_length=32, | ||
| allow_blank=False, | ||
| ) | ||
|
|
||
| about = serializers.CharField( | ||
| max_length=2048, | ||
| allow_null=True, | ||
| allow_blank=False, | ||
| required=False, | ||
| ) | ||
|
|
||
| extension_type = serializers.ChoiceField( | ||
| choices=cnc_game.CncFileExtension.ExtensionTypes.choices, | ||
| ) | ||
|
|
||
| def create(self, validated_data: dict[str, t.Any]) -> cnc_game.CncFileExtension: | ||
| return cnc_game.CncFileExtension.objects.create(**validated_data) | ||
|
|
||
| def update( | ||
| self, instance: cnc_game.CncFileExtension, validated_data: dict[str, t.Any] | ||
| ) -> cnc_game.CncFileExtension: | ||
| # For now, don't allow editing the extension. These likely shouldn't ever need to be updated. | ||
| # instance.extension = validated_data.get("extension", instance.extension) | ||
| instance.about = validated_data.get("about", instance.about) | ||
| instance.extension_type = validated_data.get("extension_type", instance.extension_type) | ||
| instance.save(update_fields=["about", "extension_type"]) | ||
| instance.refresh_from_db() | ||
| return instance | ||
|
|
||
| class Meta: | ||
| model = cnc_game.CncFileExtension | ||
| exclude = ["last_modified_by"] | ||
| fields = "__all__" | ||
|
|
||
|
|
||
| class CncGameSerializer(KirovySerializer): | ||
| slug = serializers.CharField(read_only=True, allow_null=False, allow_blank=False) | ||
| full_name = serializers.CharField(allow_null=False, allow_blank=False) | ||
| is_visible = serializers.BooleanField(allow_null=False, default=True) | ||
| allow_public_uploads = serializers.BooleanField(allow_null=False, default=False) | ||
| compatible_with_parent_maps = serializers.BooleanField(allow_null=False, default=False) | ||
| is_mod = serializers.BooleanField(read_only=True, allow_null=False, default=False) | ||
| allowed_extension_ids = serializers.PrimaryKeyRelatedField( | ||
| source="allowed_extensions", | ||
| pk_field=serializers.UUIDField(), | ||
| many=True, | ||
| read_only=True, # Set these manually using the ORM. | ||
| ) | ||
|
|
||
| parent_game_id = serializers.PrimaryKeyRelatedField( | ||
| source="parent_game", | ||
| pk_field=serializers.UUIDField(), | ||
| many=False, | ||
| allow_null=True, | ||
| allow_empty=False, | ||
| default=None, | ||
| read_only=True, # parent_id affects file path generation so we can't change it via the API. | ||
| ) | ||
|
|
||
| class Meta: | ||
| model = cnc_game.CncGame | ||
| # We return the ID instead of the whole object. | ||
| exclude = ["parent_game", "allowed_extensions"] | ||
| fields = "__all__" | ||
|
|
||
| def create(self, validated_data: t.DictStrAny) -> cnc_game.CncGame: | ||
| instance = cnc_game.CncGame(**validated_data) | ||
| instance.save() | ||
| return instance | ||
|
|
||
| def update(self, instance: cnc_game.CncGame, validated_data: t.DictStrAny) -> cnc_game.CncGame: | ||
| instance.full_name = validated_data.get("full_name", instance.full_name) | ||
| instance.is_visible = validated_data.get("is_visible", instance.is_visible) | ||
| instance.is_mod = validated_data.get("is_mod", instance.is_mod) | ||
| instance.allow_public_uploads = validated_data.get("allow_public_uploads", instance.allow_public_uploads) | ||
| instance.compatible_with_parent_maps = validated_data.get( | ||
| "compatible_with_parent_maps", instance.compatible_with_parent_maps | ||
| ) | ||
| instance.save( | ||
| update_fields=["full_name", "is_visible", "is_mod", "allow_public_uploads", "compatible_with_parent_maps"] | ||
| ) | ||
| instance.refresh_from_db() | ||
| return instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from django.db.models import QuerySet | ||
|
|
||
| from kirovy import permissions, typing as t | ||
| from kirovy.models import CncGame | ||
| from kirovy.serializers import cnc_game_serializers | ||
| from kirovy.views.base_views import KirovyListCreateView, KirovyDefaultPagination, KirovyRetrieveUpdateView | ||
|
|
||
|
|
||
| class GamesListView(KirovyListCreateView): | ||
|
|
||
| permission_classes = [permissions.IsAdmin | permissions.ReadOnly] | ||
| pagination_class: t.Type[KirovyDefaultPagination] | None = KirovyDefaultPagination | ||
| serializer_class = cnc_game_serializers.CncGameSerializer | ||
|
|
||
| def get_queryset(self) -> QuerySet[CncGame]: | ||
| if self.request.user.is_staff: | ||
| return CncGame.objects.all() | ||
|
|
||
| return CncGame.objects.filter(is_visible=True) | ||
|
|
||
|
|
||
| class GameDetailView(KirovyRetrieveUpdateView): | ||
|
|
||
| permission_classes = [permissions.IsAdmin | permissions.ReadOnly] | ||
| pagination_class: t.Type[KirovyDefaultPagination] | None = KirovyDefaultPagination | ||
| serializer_class = cnc_game_serializers.CncGameSerializer | ||
|
|
||
| def get_queryset(self) -> QuerySet[CncGame]: | ||
| if self.request.user.is_staff: | ||
| return CncGame.objects.all() | ||
|
|
||
| return CncGame.objects.filter(is_visible=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URLs were getting truncated by the very short default length