From d7a8cd053d966b7f5839ca40adde9dfe88a304da Mon Sep 17 00:00:00 2001
From: borzechof99 <borzechof99@mi.fu-berlin.de>
Date: Sat, 12 Jun 2021 17:33:14 +0200
Subject: [PATCH] Add Basic Question Serializer to Test out i18n in own API

---
 README.md                         | 28 ++++++++++++++++++++++++++++
 unisportomat/quiz/serializers.py  | 13 ++++++++++++-
 unisportomat/quiz/views.py        | 17 +++++++++++++++--
 unisportomat/unisportomat/urls.py |  1 +
 4 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index ae711e5..4a515e4 100644
--- a/README.md
+++ b/README.md
@@ -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).   
 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
diff --git a/unisportomat/quiz/serializers.py b/unisportomat/quiz/serializers.py
index 9ae6987..48c9443 100644
--- a/unisportomat/quiz/serializers.py
+++ b/unisportomat/quiz/serializers.py
@@ -2,7 +2,7 @@
 Serializers creating JSONs for every Model from .models
 """
 from rest_framework import serializers
-from .models import Sport, Criterion
+from .models import Sport, Criterion, Question
 
 
 class SportListSerializer(serializers.ModelSerializer):
@@ -15,6 +15,17 @@ class SportListSerializer(serializers.ModelSerializer):
         fields = ("id", "name", "url", "criteria_ratings")
 
 
+class QuestionListSerializer(serializers.ModelSerializer):
+    """
+    Serializes all Questions.
+    """
+
+    class Meta:
+        model = Question
+
+        fields = ("text", "criterion")
+
+
 class CriterionListSerializer(serializers.ModelSerializer):
     """
     Serializes Criterions
diff --git a/unisportomat/quiz/views.py b/unisportomat/quiz/views.py
index ce6602a..03cb6f9 100644
--- a/unisportomat/quiz/views.py
+++ b/unisportomat/quiz/views.py
@@ -4,8 +4,12 @@ Defines the views for the API
 
 # from django.shortcuts import render
 from rest_framework import viewsets
-from .serializers import SportListSerializer, CriterionListSerializer
-from .models import Sport, Criterion
+from .serializers import (
+    SportListSerializer,
+    CriterionListSerializer,
+    QuestionListSerializer,
+)
+from .models import Sport, Criterion, Question
 
 # Create your views here.
 
@@ -26,3 +30,12 @@ class CriterionListView(viewsets.ModelViewSet):  # pylint: disable=too-many-ance
 
     serializer_class = CriterionListSerializer
     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()
diff --git a/unisportomat/unisportomat/urls.py b/unisportomat/unisportomat/urls.py
index 9e25163..801097c 100644
--- a/unisportomat/unisportomat/urls.py
+++ b/unisportomat/unisportomat/urls.py
@@ -21,6 +21,7 @@ from quiz import views
 router = routers.DefaultRouter()
 router.register(r"sport-list", views.SportListView, "sport-list")
 router.register(r"criterion-list", views.CriterionListView, "criterion-list")
+router.register(r"question-list", views.QuestionListView, "question-list")
 
 urlpatterns = [
     path("admin/", admin.site.urls),
-- 
GitLab