Skip to content
Snippets Groups Projects
Commit d7a8cd05 authored by borzechof99's avatar borzechof99 :whale2:
Browse files

Add Basic Question Serializer to Test out i18n in own API

parent ec845a29
No related branches found
No related tags found
No related merge requests found
...@@ -86,3 +86,31 @@ If you started the server as described above, you can access the django admin in ...@@ -86,3 +86,31 @@ If you started the server as described above, you can access the django admin in
[localhost:8000/admin](localhost:8000/admin). [localhost:8000/admin](localhost:8000/admin).
If you seeded the database you can login with username: "admin" and the password you specified. If you seeded the database you can login with username: "admin" and the password you specified.
## Internationalization and how to use it
At the current time, the backend uses [django-modeltranslation])(https://django-modeltranslation.readthedocs.io/en/latest/index.html) to handle translations of Strings in the models Question, CallToMove, KnowledgeSnack. The list of available languages is defined in `settings.py`, and the translatable fields are defined in `translation.py`. Right now, the two languages `de` and `en` are enabled, with `de` translations needing to be filled out.
Django internally keeps track of the active language, which decides on the language strings that `object.text` returns. If you want to force a certain language, `object.text_de` can be used.
`"django.middleware.locale.LocaleMiddleware"` has been added as a Middleware. This allows Django to recognize the locale of the requesting Browser and automatically sets the language to that locale. This means that both the Django Admin Panel and our own API choose the Strings depending on the GET request. It is yet to be determined whether React can change the locale in the requests, but if that were the case, this feature would potentially ease the implementation of Serializers for the User-Frontend.
Instead of relying on the Locale given by the Requests, one can also manually change the active language. This example demonstrates how:
```python
from django.utils.translation import get_language, activate
cur_language = get_language() # Returns current active language
activate("de") # Selects German as active language
print(object.text) # Prints German Translation
activate("en") # Selects English as active language
print(object.text) # Prints English Translation
activate(cur_language) # Resets active language to the one before manual activations
```
This might be particularly useful for entering data from the Admin Frontend into the database, or choosing the language for the User Frontend if the Browser Locale cannot be used.
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Serializers creating JSONs for every Model from .models Serializers creating JSONs for every Model from .models
""" """
from rest_framework import serializers from rest_framework import serializers
from .models import Sport, Criterion from .models import Sport, Criterion, Question
class SportListSerializer(serializers.ModelSerializer): class SportListSerializer(serializers.ModelSerializer):
...@@ -15,6 +15,17 @@ class SportListSerializer(serializers.ModelSerializer): ...@@ -15,6 +15,17 @@ class SportListSerializer(serializers.ModelSerializer):
fields = ("id", "name", "url", "criteria_ratings") fields = ("id", "name", "url", "criteria_ratings")
class QuestionListSerializer(serializers.ModelSerializer):
"""
Serializes all Questions.
"""
class Meta:
model = Question
fields = ("text", "criterion")
class CriterionListSerializer(serializers.ModelSerializer): class CriterionListSerializer(serializers.ModelSerializer):
""" """
Serializes Criterions Serializes Criterions
......
...@@ -4,8 +4,12 @@ Defines the views for the API ...@@ -4,8 +4,12 @@ Defines the views for the API
# from django.shortcuts import render # from django.shortcuts import render
from rest_framework import viewsets from rest_framework import viewsets
from .serializers import SportListSerializer, CriterionListSerializer from .serializers import (
from .models import Sport, Criterion SportListSerializer,
CriterionListSerializer,
QuestionListSerializer,
)
from .models import Sport, Criterion, Question
# Create your views here. # Create your views here.
...@@ -26,3 +30,12 @@ class CriterionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ance ...@@ -26,3 +30,12 @@ class CriterionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ance
serializer_class = CriterionListSerializer serializer_class = CriterionListSerializer
queryset = Criterion.objects.all() queryset = Criterion.objects.all()
class QuestionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ancestors
"""
A View returning every Question Object
"""
serializer_class = QuestionListSerializer
queryset = Question.objects.all()
...@@ -21,6 +21,7 @@ from quiz import views ...@@ -21,6 +21,7 @@ from quiz import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r"sport-list", views.SportListView, "sport-list") router.register(r"sport-list", views.SportListView, "sport-list")
router.register(r"criterion-list", views.CriterionListView, "criterion-list") router.register(r"criterion-list", views.CriterionListView, "criterion-list")
router.register(r"question-list", views.QuestionListView, "question-list")
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment