Skip to content

Commit

Permalink
New Upload v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraust committed Sep 2, 2024
1 parent 43b5376 commit 28e83e0
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 10 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.6.9.1
2024.9.2.1
2 changes: 1 addition & 1 deletion combatlog/models/combatlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def update_metadata_file(self, file, force=False):

if len(ladders) == 0:
raise APIException(
f"{combat.map} {combat.difficulty} at {combat.start_time} has no matching ladder"
f"{combat.map} ({combat.difficulty} Difficulty) at {combat.start_time} has no matching ladder"
)

for _, player in players:
Expand Down
20 changes: 19 additions & 1 deletion combatlog/serializers/combatlog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" CombatLog Serializers """
"""CombatLog Serializers"""

from rest_framework import serializers

Expand All @@ -23,10 +23,28 @@ class CombatLogUploadSerializer(serializers.Serializer):
file = serializers.FileField()


class CombatLogUploadV2Serializer(serializers.Serializer):
"""CombatLog Upload Serializer"""

file = serializers.FileField()


class CombatLogUploadResponseSerializer(serializers.Serializer):
"""CombatLog Upload Response Serializer"""

name = serializers.CharField()
updated = serializers.BooleanField()
detail = serializers.CharField()
value = serializers.FloatField()


class CombatLogUploadV2ResponseSerializer(serializers.Serializer):
"""CombatLog Upload Response Serializer"""

results = CombatLogUploadResponseSerializer(
many=True,
required=False,
allow_null=True,
)
combatlog = serializers.IntegerField(required=False, allow_null=True)
detail = serializers.CharField()
56 changes: 50 additions & 6 deletions combatlog/views/combatlog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" CombatLog Views """
"""CombatLog Views"""

import logging

Expand All @@ -14,11 +14,11 @@
from rest_framework.viewsets import GenericViewSet

from combatlog.models import CombatLog
from combatlog.serializers import (
CombatLogSerializer,
CombatLogUploadResponseSerializer,
CombatLogUploadSerializer,
)
from combatlog.serializers import (CombatLogSerializer,
CombatLogUploadResponseSerializer,
CombatLogUploadSerializer,
CombatLogUploadV2ResponseSerializer,
CombatLogUploadV2Serializer)
from core.pagination import PageNumberPagination

LOGGER = logging.getLogger("django")
Expand Down Expand Up @@ -65,6 +65,50 @@ def upload(self, request):

return Response(serializer.data)

@swagger_auto_schema(
responses={200: CombatLogUploadV2ResponseSerializer()},
)
@action(
detail=False,
methods=["POST"],
serializer_class=CombatLogUploadV2Serializer,
parser_classes=(MultiPartParser,),
permission_classes=(),
)
def uploadv2(self, request):
"""
Combat Log Upload
Uploads a Combat Log for analysis.
"""

serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)

data = serializer.validated_data["file"].read()

try:
with transaction.atomic():
instance = CombatLog.objects.create()
res = instance.update_metadata(data)
results = CombatLogUploadResponseSerializer(data=res, many=True)
results.is_valid(raise_exception=True)
except Exception as e:
serializer = CombatLogUploadV2ResponseSerializer(data={"detail": str(e)})
serializer.is_valid(raise_exception=True)
return Response(serializer.data)

serializer = CombatLogUploadV2ResponseSerializer(
data={
"results": results.data,
"combatlog": instance.pk,
"detail": "Combatlog uploaded successfully.",
}
)
serializer.is_valid(raise_exception=True)

return Response(serializer.data)

@swagger_auto_schema(
responses={
"200": openapi.Response(
Expand Down
28 changes: 28 additions & 0 deletions ladder/fixtures/ladders.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,33 @@
"internal_name": "Nukara Prime: Transdimensional Tactics",
"internal_difficulty": null
}
},
{
"model": "ladder.Ladder",
"pk": 10,
"fields": {
"name": "Infected: The Conduit",
"difficulty": "Any",
"metric": "DPS",
"variant": "Default",
"is_solo": false,
"is_space": true,
"internal_name": "Infected Space",
"internal_difficulty": null
}
},
{
"model": "ladder.Ladder",
"pk": 11,
"fields": {
"name": "Hive: Onslaught",
"difficulty": "Any",
"metric": "DPS",
"variant": "Default",
"is_solo": false,
"is_space": true,
"internal_name": "Hive Space",
"internal_difficulty": null
}
}
]
28 changes: 28 additions & 0 deletions ladder/fixtures/ladders_solo.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,33 @@
"internal_name": "Nukara Prime: Transdimensional Tactics",
"internal_difficulty": null
}
},
{
"model": "ladder.Ladder",
"pk": 1010,
"fields": {
"name": "Infected: The Conduit",
"difficulty": "Any",
"metric": "DPS",
"variant": "Default",
"is_solo": true,
"is_space": true,
"internal_name": "Infected Space",
"internal_difficulty": null
}
},
{
"model": "ladder.Ladder",
"pk": 1011,
"fields": {
"name": "Hive: Onslaught",
"difficulty": "Any",
"metric": "DPS",
"variant": "Default",
"is_solo": true,
"is_space": true,
"internal_name": "Hive Space",
"internal_difficulty": null
}
}
]
2 changes: 1 addition & 1 deletion ladder/models/ladder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ def create_variants(self):
self.create_variant(variant)

def __str__(self):
return f"{'[Solo]' if self.is_solo else ''} ({self.variant.name}) {self.name} {self.difficulty} {self.metric}"
return f"{'[Solo]' if self.is_solo else ''} ({self.variant.name}) {self.name} ({self.difficulty} Difficulty) {self.metric}"
2 changes: 2 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ in pkgs.mkShell rec {
name = "impurePythonEnv";
venvDir = "./.venv";
buildInputs = [
openapi-generator-cli

# A Python interpreter including the 'venv' module is required to bootstrap
# the environment.
pythonPackages.python
Expand Down

0 comments on commit 28e83e0

Please sign in to comment.