openbackloggery/collection/games/views.py

87 lines
2.3 KiB
Python

from django.db.models import Q
from django.views.generic import ListView
from rest_framework import viewsets
from .serializers import (
GameSerializer,
GameTimelineSerializer,
PlatformSerializer
)
from .models import Game, Platform, Timeline
class PlatformViewSet(viewsets.ModelViewSet):
"""
API endpoints that allows platforms to be edited or viewed.
retrieve:
Return the given platform
list:
Return a list of all existing platforms ordered by name.
create:
Create a new platform instance.
"""
queryset = Platform.objects.all().order_by('name')
serializer_class = PlatformSerializer
class GameTimelineViewSet(viewsets.ModelViewSet):
"""
API endpoints that allows to keep games life.
BE CAREFUL. You shouldn't change these entries because they're generated
by Game status change. It could be only useful to change the acquisition
date. Not more.
retrieve:
Return the given timeline entry.
list:
Return a list of timeline entries sorted by date in descending order.
create:
Create a new timeline instance.
"""
queryset = Timeline.objects.all().order_by('-date')
serializer_class = GameTimelineSerializer
class GameViewSet(viewsets.ModelViewSet):
"""
API endpoints that allows games to be edited or viewed.
retrieve:
Return the given game.
list:
Return a list of all existing games ordered by name.
create:
Create a new game instance.
"""
queryset = Game.objects.all().order_by('name')
serializer_class = GameSerializer
class GameList(ListView):
model = Game
context_object_name = 'non_excluded_games'
template_name = 'games/index.html'
queryset = Game.objects.filter(
~Q(status=Game.EXCLUDED) &
Q(wish=False)).order_by('name')
def get_context_data(self, **kwargs):
"""
Add playing games list.
Add 5 last current activities from Timeline
"""
context = super(GameList, self).get_context_data(**kwargs)
context['playing_games'] = Game.objects.filter(
playing=True).order_by('name')
context['last_timelines'] = Timeline.objects.filter(
~Q(item__status=Game.EXCLUDED)
).order_by(
'-date')[:5]
return context