Skip to content

Instantly share code, notes, and snippets.

@michaelhelmick
Last active April 2, 2021 20:59
Show Gist options
  • Save michaelhelmick/19a18123a64866e9b7812d8d15e2cd01 to your computer and use it in GitHub Desktop.
Save michaelhelmick/19a18123a64866e9b7812d8d15e2cd01 to your computer and use it in GitHub Desktop.
game/views.py
from django.utils.translation import gettext_lazy as _
class Game(models.Model):
TEEBALL_INTRODUCED_VERSION = "1.1.0"
REVAMPED_PRODUCT_INTRODUCED_VERSION = "2.0.0"
class GameType(models.TextChoices):
BASEBALL = "BB", _("Baseball")
SOFTBALL = "SB", _("Softball")
TEEBALL = "TB", _("Tee-ball")
type = models.CharField(
max_length=2,
choices=GameType.choices,
default=GameType.BASEBALL,
)
from rest_framework import viewsets
from semantic_version import SimpleSpec, Version
class GameViewSet(viewsets.ReadOnlyModelViewSet):
"""Game viewset."""
queryset = Game.objects.all()
serializer_class = GameSerializer
def get_serializer_class(self):
"""Determine serializer class."""
teeball_introduced = SimpleSpec(f">={Game.SOFTBALL_INTRODUCED_VERSION},<{Game.REVAMPED_PRODUCT_INTRODUCED_VERSION}")
revamped_product_introduced = SimpleSpec(f">={Game.REVAMPED_PRODUCT_INTRODUCED_VERSION}")
if teeball_introduced.match(Version(self.request.version)):
return TeeBallSupportedGameSerializer
elif revamped_product_introduced.match(Version(self.request.version)):
return RevampedGameSerializer
return GameSerializer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment