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
master
Olivier DOSSMANN 2017-09-04 23:42:57 +02:00
parent ff228ab845
commit 9b0def75b7
10 changed files with 125 additions and 23 deletions

View File

@ -0,0 +1,8 @@
# API - Django Rest Framework
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAdminUser',
],
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
'UNICODE_JSON': True,
}

View File

@ -26,6 +26,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework',
'core', 'core',
'games.apps.GamesConfig', 'games.apps.GamesConfig',
] ]

View File

@ -18,6 +18,7 @@ base_settings = [
'components/common.py', # standard django settings 'components/common.py', # standard django settings
'components/database.py', # SQLite 3 'components/database.py', # SQLite 3
'components/i18n.py', # Internationalisation and localization 'components/i18n.py', # Internationalisation and localization
'components/api.py', # API (django rest framework)
# Select the right env: # Select the right env:
'environments/%s.py' % ENV, 'environments/%s.py' % ENV,

View File

@ -16,14 +16,25 @@ Including another URLconf
from django.conf.urls import url, include from django.conf.urls import url, include
from django.contrib import admin from django.contrib import admin
from collection import __version__ as app_version from collection import __version__ as app_version
from games.views import GameList from games.views import GameList, ConsoleViewSet
from rest_framework import routers
from rest_framework.documentation import include_docs_urls
# Admin config # Admin config
admin.site.site_title = 'OpenBackloggery' admin.site.site_title = 'OpenBackloggery'
admin.site.site_header = '%s %s' % (admin.site.site_title, app_version) admin.site.site_header = '%s %s' % (admin.site.site_title, app_version)
# Django Rest Framework router
router = routers.DefaultRouter()
router.register(r'consoles', ConsoleViewSet)
urlpatterns = [ urlpatterns = [
url(r'^$', GameList.as_view(), name='homepage'), url(r'^$', GameList.as_view(), name='homepage'),
url(r'^games/', include('games.urls', namespace='games'), name='games'), url(r'^games/', include('games.urls', namespace='games'), name='games'),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls), name='api'),
url(r'^api/v1/docs/', include_docs_urls(title=' '.join(
[admin.site.site_title,
'API']))),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
] ]

View File

@ -7,8 +7,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 0.1\n" "Project-Id-Version: 0.1\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-08-31 14:49+0000\n" "POT-Creation-Date: 2017-09-04 21:24+0000\n"
"PO-Revision-Date: 2017-08-31 16:50+0200\n" "PO-Revision-Date: 2017-09-04 23:27+0200\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
"Language: fr\n" "Language: fr\n"
@ -45,7 +45,7 @@ msgstr "Information du jeu"
msgid "Progress" msgid "Progress"
msgstr "Progression" msgstr "Progression"
#: games/models.py:15 games/models.py:25 #: games/models.py:15 games/models.py:29
msgid "console" msgid "console"
msgstr "console" msgstr "console"
@ -53,71 +53,75 @@ msgstr "console"
msgid "consoles" msgid "consoles"
msgstr "consoles" msgstr "consoles"
#: games/models.py:36 #: games/models.py:20
msgid "Most used console name."
msgstr "Nom de console le plus utilisé."
#: games/models.py:40
msgid "Beaten" msgid "Beaten"
msgstr "Terminé (quête principale)" msgstr "Terminé (quête principale)"
#: games/models.py:37 #: games/models.py:41
msgid "Completed" msgid "Completed"
msgstr "Terminé complètement" msgstr "Terminé complètement"
#: games/models.py:38 #: games/models.py:42
msgid "Excluded" msgid "Excluded"
msgstr "Exclu" msgstr "Exclu"
#: games/models.py:39 #: games/models.py:43
msgid "Mastered" msgid "Mastered"
msgstr "Usé / Épuisé" msgstr "Usé / Épuisé"
#: games/models.py:40 #: games/models.py:44
msgid "Unfinished" msgid "Unfinished"
msgstr "Inachevé" msgstr "Inachevé"
#: games/models.py:49 #: games/models.py:53
msgid "Progress note" msgid "Progress note"
msgstr "Note de progression" msgstr "Note de progression"
#: games/models.py:50 #: games/models.py:54
msgid "Short note displayed to your followers." msgid "Short note displayed to your followers."
msgstr "Courte note affichée à ceux qui vous suive" msgstr "Courte note affichée à ceux qui vous suive"
#: games/models.py:55 #: games/models.py:59
msgid "playing?" msgid "playing?"
msgstr "en train d'y jouer ?" msgstr "en train d'y jouer ?"
#: games/models.py:56 #: games/models.py:60
msgid "You're currently playing this game." msgid "You're currently playing this game."
msgstr "Vous jouez actuellement à ce jeu." msgstr "Vous jouez actuellement à ce jeu."
#: games/models.py:59 #: games/models.py:63
msgid "unplayed?" msgid "unplayed?"
msgstr "jamais joué ?" msgstr "jamais joué ?"
#: games/models.py:60 #: games/models.py:64
msgid "You never played this game." msgid "You never played this game."
msgstr "Vous n'avez jamais joué à ce jeu." msgstr "Vous n'avez jamais joué à ce jeu."
#: games/models.py:63 #: games/models.py:67
msgid "wish?" msgid "wish?"
msgstr "envie de l'avoir ?" msgstr "envie de l'avoir ?"
#: games/models.py:64 #: games/models.py:68
msgid "You're waiting X-mas father offers you this game." msgid "You're waiting X-mas father offers you this game."
msgstr "Vous patientez que le père Noël vous offre ce jeu." msgstr "Vous patientez que le père Noël vous offre ce jeu."
#: games/models.py:68 #: games/models.py:72
msgid "game" msgid "game"
msgstr "jeu" msgstr "jeu"
#: games/models.py:69 #: games/models.py:73
msgid "games" msgid "games"
msgstr "jeux" msgstr "jeux"
#: games/models.py:80 #: games/models.py:84
msgid "Timeline" msgid "Timeline"
msgstr "Chronologie" msgstr "Chronologie"
#: games/models.py:81 #: games/models.py:85
msgid "Timelines" msgid "Timelines"
msgstr "Chronologies" msgstr "Chronologies"
@ -145,6 +149,6 @@ msgstr "Aucun jeu."
msgid "Memory Card" msgid "Memory Card"
msgstr "Carte mémoire" msgstr "Carte mémoire"
#: games/templates/games/index.html:38 #: games/templates/games/index.html:43
msgid "Empty memory." msgid "Empty memory."
msgstr "Mémoire vide." msgstr "Mémoire vide."

View File

@ -16,6 +16,10 @@ class Console(Collection):
verbose_name_plural = _('consoles') 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): class Game(Item):
""" """
A video game you will use on a specific Console. 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.db.models import Q
from django.views.generic import ListView 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): class GameList(ListView):

View File

@ -2,3 +2,5 @@ Django==1.11.4
django-split-settings==0.2.5 django-split-settings==0.2.5
PyYAML==3.12 PyYAML==3.12
uWSGI==2.0.15 uWSGI==2.0.15
djangorestframework==3.6.4
coreapi==2.3.1