API : ajout des jeux (nom et plateforme)
This commit is contained in:
parent
b0978034b5
commit
8dec41073f
@ -3,7 +3,7 @@ Current version (0.2) :
|
||||
- Renommage des consoles en « plateformes »
|
||||
- Activation des requêtes CORS pour permettre à une autre application d'accéder à l'API
|
||||
- MàJ vers Django 1.11.5
|
||||
- Activation d'une API (pour les consoles) accessible par l'administrateur (avec documentation)
|
||||
- Activation d'une API (pour les consoles et les jeux) accessible par l'administrateur (avec documentation)
|
||||
- Ajout d'une page d'accueil listant les jeux vidéos en cours, la liste complète et les 5 dernières activités sur ces derniers triées par date
|
||||
- Nouveau champ 'note' pour la progression dans le jeu
|
||||
- Omission de l'état "Nouveau" pour les jeux
|
||||
|
2
TODO
2
TODO
@ -1,5 +1,7 @@
|
||||
# À faire
|
||||
|
||||
* Ajouter le champ "status" des jeux dans l'API
|
||||
* API jeux : ajouter wish, playing et unplayed
|
||||
* Ajouter des help_text à tous les champs + classes enfants. Les traduire.
|
||||
* étudier la possibilité à l'utilisateur, via des variables d'environnement, de configurer un email, une autre BDD, etc.
|
||||
|
||||
|
@ -16,7 +16,7 @@ Including another URLconf
|
||||
from django.conf.urls import url, include
|
||||
from django.contrib import admin
|
||||
from collection import __version__ as app_version
|
||||
from games.views import GameList, PlatformViewSet
|
||||
from games.views import GameList, GameViewSet, PlatformViewSet
|
||||
from rest_framework import routers
|
||||
from rest_framework.documentation import include_docs_urls
|
||||
|
||||
@ -26,6 +26,7 @@ admin.site.site_header = '%s %s' % (admin.site.site_title, app_version)
|
||||
|
||||
# Django Rest Framework router
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'games', GameViewSet)
|
||||
router.register(r'platforms', PlatformViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
|
@ -1,4 +1,4 @@
|
||||
from games.models import Platform
|
||||
from games.models import Game, Platform
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
@ -6,3 +6,9 @@ class PlatformSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Platform
|
||||
fields = ('name',)
|
||||
|
||||
|
||||
class GameSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Game
|
||||
fields = ('name', 'collection')
|
||||
|
@ -1,6 +1,6 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
from games.models import Platform
|
||||
from games.models import Game, Platform
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase, force_authenticate
|
||||
import json
|
||||
@ -42,4 +42,59 @@ class PlatformTest(APITestCase):
|
||||
Platform.objects.all().order_by('name').values_list(
|
||||
'name', flat=True))
|
||||
platforms = [x.get('name') for x in json.loads(response.content)]
|
||||
self.assertEqual(platforms, sorted_platformss)
|
||||
self.assertEqual(platforms, sorted_platforms)
|
||||
|
||||
|
||||
class GameTest(APITestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.superuser = User.objects.create_superuser(
|
||||
'admin',
|
||||
'admin@localhost',
|
||||
'admin')
|
||||
|
||||
def test_create_game(self):
|
||||
"""
|
||||
Check we can create a game object.
|
||||
"""
|
||||
# Game works on specific platform
|
||||
console = Platform.objects.create(name='Game Boy')
|
||||
console_url = reverse('platform-detail', kwargs={'pk': console.id})
|
||||
|
||||
url = reverse('game-list')
|
||||
data = {
|
||||
'name': 'Tetris',
|
||||
'collection': console_url,
|
||||
}
|
||||
self.client.force_authenticate(user=self.superuser)
|
||||
response = self.client.post(url, data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(Game.objects.count(), 1)
|
||||
self.assertEqual(Game.objects.get().name, 'Tetris')
|
||||
self.assertEqual(Game.objects.get().collection_id, console.id)
|
||||
|
||||
|
||||
def test_sorted_games(self):
|
||||
"""
|
||||
Check that game list is sorted.
|
||||
"""
|
||||
console = Platform.objects.create(name='BestPlatform4Ever')
|
||||
Game.objects.create(
|
||||
name='Pomperman',
|
||||
collection=console)
|
||||
Game.objects.create(
|
||||
name='Vektoria',
|
||||
collection=console)
|
||||
Game.objects.create(
|
||||
name='Qrackovitchya',
|
||||
collection=console)
|
||||
url = reverse('game-list')
|
||||
self.client.force_authenticate(user=self.superuser)
|
||||
response = self.client.get(url, format='json')
|
||||
sorted_games = list(
|
||||
Game.objects.all().order_by('name').values_list(
|
||||
'name', flat=True))
|
||||
games = [x.get('name') for x in json.loads(response.content)]
|
||||
self.assertEqual(games, sorted_games)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
from django.db.models import Q
|
||||
from django.views.generic import ListView
|
||||
from rest_framework import viewsets
|
||||
from .serializers import PlatformSerializer
|
||||
from .serializers import GameSerializer, PlatformSerializer
|
||||
|
||||
from .models import Game, Platform, Timeline
|
||||
|
||||
@ -22,6 +22,23 @@ class PlatformViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = PlatformSerializer
|
||||
|
||||
|
||||
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'
|
||||
|
Loading…
Reference in New Issue
Block a user