Nouvelle API :

Avec

  * API sur les consoles en utilisant Django Rest Framework
  * Tri par nom de console
  * Nouveaux tests sur l'API
  * Documentation pour l'API
  * Ajout d'une description au champ "name" pour Console et sa traduction
  * Nouvelles dépendances à coreapi et djangorestframework
This commit is contained in:
2017-09-04 23:42:57 +02:00
parent ff228ab845
commit 9b0def75b7
10 changed files with 125 additions and 23 deletions

View File

@ -16,6 +16,10 @@ class Console(Collection):
verbose_name_plural = _('consoles')
# Redefine help_text (for documentation, API, etc.)
Console._meta.get_field('name').help_text = _('Most used console name.')
class Game(Item):
"""
A video game you will use on a specific Console.

View File

@ -0,0 +1,8 @@
from games.models import Console
from rest_framework import serializers
class ConsoleSerializer(serializers.ModelSerializer):
class Meta:
model = Console
fields = ('name',)

View File

@ -0,0 +1,45 @@
from django.contrib.auth.models import User
from django.urls import reverse
from games.models import Console
from rest_framework import status
from rest_framework.test import APITestCase, force_authenticate
import json
class ConsoleTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.superuser = User.objects.create_superuser(
'admin',
'admin@localhost',
'admin')
def test_create_console(self):
"""
Check we can create a console object.
"""
url = reverse('console-list')
data = {'name': 'GP2X'}
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(Console.objects.count(), 1)
self.assertEqual(Console.objects.get().name, 'GP2X')
def test_sorted_console(self):
"""
Check that console list is sorted.
"""
Console.objects.create(name='GP2X')
Console.objects.create(name='3DS')
Console.objects.create(name='Game Boy')
Console.objects.create(name='Amiga')
url = reverse('console-list')
self.client.force_authenticate(user=self.superuser)
response = self.client.get(url, format='json')
sorted_consoles = list(
Console.objects.all().order_by('name').values_list(
'name', flat=True))
consoles = [x.get('name') for x in json.loads(response.content)]
self.assertEqual(consoles, sorted_consoles)

View File

@ -1,7 +1,25 @@
from django.db.models import Q
from django.views.generic import ListView
from rest_framework import viewsets
from .serializers import ConsoleSerializer
from .models import Game, Timeline
from .models import Console, Game, Timeline
class ConsoleViewSet(viewsets.ModelViewSet):
"""
API endpoints that allows consoles to be edited or viewed.
retrieve:
Return the given console
list:
Return a list of all existing consoles ordered by name.
create:
Create a new console instance.
"""
queryset = Console.objects.all().order_by('name')
serializer_class = ConsoleSerializer
class GameList(ListView):