vbv/server/vbv_lernwelt/api/directory.py

41 lines
1.3 KiB
Python

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from vbv_lernwelt.core.models import Country, Organisation
from vbv_lernwelt.core.serializers import CountrySerializer, OrganisationSerializer
from vbv_lernwelt.learnpath.models import CourseProfile
from vbv_lernwelt.learnpath.serializers import CourseProfileSerializer
@api_view(["GET"])
@permission_classes([IsAuthenticated])
def list_entities(request):
language_code = request.LANGUAGE_CODE if request.LANGUAGE_CODE else "de"
field_mapping = {
"de": "name_de",
"fr": "name_fr",
"it": "name_it",
}
field_name = field_mapping.get(language_code, field_mapping["de"])
context = {"langauge": request.user.language}
organisations = OrganisationSerializer(
Organisation.objects.all().order_by(field_name), many=True, context=context
).data
countries = CountrySerializer(
Country.objects.all(), many=True, context=context
).data
course_profiles = CourseProfileSerializer(
CourseProfile.objects.all(), many=True, context=context
).data
return Response(
{
"organisations": organisations,
"countries": countries,
"courseProfiles": course_profiles,
}
)