2017-08-31 13:20:08 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.views.generic import ListView
|
2017-09-04 21:42:57 +00:00
|
|
|
from rest_framework import viewsets
|
2018-02-03 19:11:14 +00:00
|
|
|
|
|
|
|
from .models import Game
|
|
|
|
from .models import Platform
|
|
|
|
from .models import Timeline
|
|
|
|
from .serializers import GameSerializer
|
|
|
|
from .serializers import GameTimelineSerializer
|
|
|
|
from .serializers import PlatformSerializer
|
2017-09-04 21:42:57 +00:00
|
|
|
|
2017-09-16 22:04:26 +00:00
|
|
|
|
2017-09-16 15:42:04 +00:00
|
|
|
class PlatformViewSet(viewsets.ModelViewSet):
|
2017-09-04 21:42:57 +00:00
|
|
|
"""
|
2017-09-16 15:42:04 +00:00
|
|
|
API endpoints that allows platforms to be edited or viewed.
|
2017-09-04 21:42:57 +00:00
|
|
|
|
|
|
|
retrieve:
|
2017-09-16 15:42:04 +00:00
|
|
|
Return the given platform
|
2017-09-04 21:42:57 +00:00
|
|
|
|
|
|
|
list:
|
2017-09-16 15:42:04 +00:00
|
|
|
Return a list of all existing platforms ordered by name.
|
2017-09-04 21:42:57 +00:00
|
|
|
|
|
|
|
create:
|
2017-09-16 15:42:04 +00:00
|
|
|
Create a new platform instance.
|
2017-09-04 21:42:57 +00:00
|
|
|
"""
|
2017-09-16 15:42:04 +00:00
|
|
|
queryset = Platform.objects.all().order_by('name')
|
|
|
|
serializer_class = PlatformSerializer
|
2017-08-31 13:20:08 +00:00
|
|
|
|
|
|
|
|
2017-09-16 22:04:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-09-16 19:37:36 +00:00
|
|
|
class GameViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoints that allows games to be edited or viewed.
|
|
|
|
|
|
|
|
retrieve:
|
2017-09-16 22:04:26 +00:00
|
|
|
Return the given game.
|
2017-09-16 19:37:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-08-31 13:20:08 +00:00
|
|
|
class GameList(ListView):
|
|
|
|
model = Game
|
|
|
|
context_object_name = 'non_excluded_games'
|
|
|
|
template_name = 'games/index.html'
|
2018-02-03 19:11:14 +00:00
|
|
|
queryset = Game.objects.filter(~Q(status=Game.EXCLUDED) & Q(
|
|
|
|
wish=False)).order_by('name').prefetch_related('collection')
|
2017-08-31 13:20:08 +00:00
|
|
|
|
2018-02-19 21:21:06 +00:00
|
|
|
|
|
|
|
class SummaryList(ListView):
|
|
|
|
model = Game
|
|
|
|
context_object_name = 'playing_games'
|
|
|
|
template_name = 'games/summary.html'
|
|
|
|
queryset = Game.objects.filter(
|
|
|
|
playing=True).order_by('name').prefetch_related('collection')
|
|
|
|
|
2017-08-31 13:20:08 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
2017-08-31 14:35:30 +00:00
|
|
|
Add playing games list.
|
|
|
|
Add 5 last current activities from Timeline
|
2017-08-31 13:20:08 +00:00
|
|
|
"""
|
2018-02-19 21:21:06 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2017-09-03 08:53:17 +00:00
|
|
|
context['last_timelines'] = Timeline.objects.filter(
|
2018-02-03 19:11:14 +00:00
|
|
|
~Q(item__status=Game.EXCLUDED)).order_by(
|
|
|
|
'-date')[:5].prefetch_related('item__collection')
|
2017-09-03 08:53:17 +00:00
|
|
|
return context
|