added character control endpoint

This commit is contained in:
Alexander Karpov 2022-06-04 19:30:54 +03:00
parent 92d42888bb
commit 9a0e84ede9
9 changed files with 105 additions and 18 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
.idea
static/
media/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

View File

@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@ -37,6 +37,11 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Packages
"rest_framework",
# Apps
"game"
]
@ -113,11 +118,15 @@ USE_I18N = True
USE_TZ = True
MEDIA_URL = "/media/"
STATIC_URL = "/static/"
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/'
if DEBUG:
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
else:
MEDIA_ROOT = "/var/www/media/"
STATIC_ROOT = "/var/www/static/"
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

View File

@ -13,9 +13,13 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns = (
[path("admin/", admin.site.urls), path("api/", include("game.urls"))]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
)

View File

@ -28,15 +28,15 @@ class Hero(models.Model):
"""Model to store heroes and their stats, connected to player"""
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
player = models.ForeignKey(
Player,
on_delete=models.CASCADE,
related_name="heroes",
related_query_name="hero",
)
# player = models.ForeignKey(
# Player,
# on_delete=models.CASCADE,
# related_name="heroes",
# related_query_name="hero",
# )
added = models.DateTimeField(auto_now_add=True)
type = models.CharField(blank=False, choices=HER0_TYPES, max_length=1)
type = models.CharField(blank=False, choices=HER0_TYPES, max_length=7)
idle_img = models.ImageField(upload_to="uploads/idle", blank=False)
attack_img = models.ImageField(upload_to="uploads/attack", blank=False)
die_img = models.ImageField(upload_to="uploads/die", blank=False)

0
game/permissions.py Normal file
View File

23
game/serializers.py Normal file
View File

@ -0,0 +1,23 @@
from rest_framework import serializers
from game.models import Hero
class CreateHeroSerializer(serializers.ModelSerializer):
class Meta:
model = Hero
fields = ("type", "idle_img", "attack_img", "die_img", "health", "speed")
class GetHeroSerializer(serializers.ModelSerializer):
class Meta:
model = Hero
fields = (
"added",
"type",
"idle_img",
"attack_img",
"die_img",
"health",
"speed",
)

8
game/urls.py Normal file
View File

@ -0,0 +1,8 @@
from django.urls import path
from game.views import CreateHeroView, RetrieveHeroView
urlpatterns = [
path("hero/", CreateHeroView.as_view(), name="hero_api_create"),
path("hero/<uuid:uuid>", RetrieveHeroView.as_view(), name="hero_api_retrieve"),
]

View File

@ -1,3 +1,36 @@
from django.shortcuts import render
from rest_framework import status
# Create your views here.
from rest_framework.generics import GenericAPIView, UpdateAPIView
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin
from rest_framework.response import Response
from game.models import Hero
from game.serializers import CreateHeroSerializer, GetHeroSerializer
class CreateHeroView(GenericAPIView, CreateModelMixin):
serializer_class = CreateHeroSerializer
def perform_create(self, serializer):
return serializer.save()
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = self.perform_create(serializer)
return Response({"uuid": instance.uuid}, status=status.HTTP_201_CREATED)
class RetrieveHeroView(RetrieveModelMixin, UpdateAPIView, GenericAPIView):
serializer_class = GetHeroSerializer
lookup_field = "uuid"
queryset = Hero.objects.all()
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)

6
requirements.txt Normal file
View File

@ -0,0 +1,6 @@
asgiref==3.5.2
Django==4.0.5
djangorestframework==3.13.1
Pillow==9.1.1
pytz==2022.1
sqlparse==0.4.2