viewsets.py 4.64 KB
Newer Older
1 2
import logging

3 4 5 6 7
from django.conf import settings
from rest_framework.response import Response
from rest_framework.views import APIView

from backend_app.checks import check_viewsets
8
from backend_app.models.abstract.essentialModule import EssentialModuleViewSet
9 10 11 12 13 14 15 16
from backend_app.models.campus import CampusViewSet, MainCampusViewSet
from backend_app.models.campusTaggedItem import CampusTaggedItemViewSet
from backend_app.models.city import CityViewSet
from backend_app.models.cityTaggedItem import CityTaggedItemViewSet
from backend_app.models.country import CountryViewSet
from backend_app.models.countryDri import CountryDriViewSet
from backend_app.models.countryScholarship import CountryScholarshipViewSet
from backend_app.models.countryTaggedItem import CountryTaggedItemViewSet
17
from backend_app.models.courseFeedback import CourseFeedback
18 19
from backend_app.models.currency import CurrencyViewSet
from backend_app.models.department import DepartmentViewSet
20
from backend_app.models.exchangeFeedback import ExchangeFeedbackViewSet
21
from backend_app.models.file_picture import FileViewSet, PictureViewSet
22 23
from backend_app.models.for_testing.moderation import ForTestingModerationViewSet
from backend_app.models.for_testing.versioning import ForTestingVersioningViewSet
24
from backend_app.models.language import LanguageViewSet
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
from backend_app.models.offer import OfferViewSet
from backend_app.models.pendingModeration import (
    PendingModerationViewSet,
    PendingModerationObjViewSet,
)
from backend_app.models.specialty import SpecialtyViewSet
from backend_app.models.tag import TagViewSet
from backend_app.models.university import UniversityViewSet
from backend_app.models.universityDri import UniversityDriViewSet
from backend_app.models.universityInfo import UniversityInfoViewSet
from backend_app.models.universityScholarship import UniversityScholarshipViewSet
from backend_app.models.universitySemestersDates import UniversitySemestersDatesViewSet
from backend_app.models.universityTaggedItem import UniversityTaggedItemViewSet
from backend_app.models.userData import UserDataViewSet
from backend_app.models.version import VersionViewSet
from backend_app.permissions.app_permissions import ReadOnly
41
from backend_app.serializers import CourseFeedbackSerializer
42 43
from backend_app.settings.defaults import OBJ_MODERATION_PERMISSIONS
from base_app.models import UserViewset
44

45 46 47 48 49 50 51

class CourseFeedbackViewSet(EssentialModuleViewSet):
    queryset = CourseFeedback.objects.all().prefetch_related()  # pylint: disable=E1101
    serializer_class = CourseFeedbackSerializer
    end_point_route = "courseFeedbacks"


52
ALL_API_VIEWSETS = [
53
    UserViewset,
54 55 56 57 58 59 60 61 62
    CampusViewSet,
    MainCampusViewSet,
    CampusTaggedItemViewSet,
    CityViewSet,
    CityTaggedItemViewSet,
    CountryViewSet,
    CountryDriViewSet,
    CountryScholarshipViewSet,
    CountryTaggedItemViewSet,
63
    CourseFeedbackViewSet,
64 65 66
    CurrencyViewSet,
    DepartmentViewSet,
    OfferViewSet,
67
    LanguageViewSet,
68 69
    PendingModerationViewSet,
    PendingModerationObjViewSet,
70 71
    FileViewSet,
    PictureViewSet,
72
    ExchangeFeedbackViewSet,
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    SpecialtyViewSet,
    TagViewSet,
    UniversityViewSet,
    UniversityDriViewSet,
    UniversityInfoViewSet,
    UniversityScholarshipViewSet,
    UniversitySemestersDatesViewSet,
    UniversityTaggedItemViewSet,
    UserDataViewSet,
    VersionViewSet,
]

if settings.TESTING:
    # We only register viewsets in a testing environment
    ALL_API_VIEWSETS += [ForTestingModerationViewSet, ForTestingVersioningViewSet]


class AppModerationStatusViewSet(APIView):
    """
    Viewset to know what is the app moderation status
    """

    # Since AppModerationStatusViewSet doesn't inherit from BaseModelViewSet
    # We need to link here the correct permissions
    permission_classes = (ReadOnly,)
    end_point_route = "serverModerationStatus"

    def get(self, request):
        return Response(
            {
                "activated": settings.MODERATION_ACTIVATED,
                "moderator_level": OBJ_MODERATION_PERMISSIONS["moderator"],
            }
        )


109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
class LogFrontendErrorsViewSet(APIView):
    """
    Viewset to handle the logging of errors coming from the frontend.
    """

    permission_classes = tuple()
    end_point_route = "frontendErrors"

    def post(self, request):
        logger = logging.getLogger("frontend")
        data = request.data
        if "componentStack" in data.keys():
            logger.error(request.data["componentStack"])
        else:
            logger.error(request.data)
        return Response(status=201)


ALL_API_VIEW_VIEWSETS = [AppModerationStatusViewSet, LogFrontendErrorsViewSet]
128 129 130 131

ALL_VIEWSETS = ALL_API_VIEWSETS + ALL_API_VIEW_VIEWSETS

check_viewsets(ALL_VIEWSETS)