from rest_framework import generics

from account.permissions import IsManager, ReadOnly
from core.models import GlobalSettings
from core.serializers import GlobalSettingsSerializer, AnonymousGlobalSettingsSerializer


class GlobalSettingsAPI(generics.RetrieveUpdateAPIView):
    serializer_class = GlobalSettingsSerializer
    permission_classes = [IsManager | ReadOnly]

    def get_serializer_class(self):
        if getattr(self.request.user, 'is_manager', False):
            return GlobalSettingsSerializer

        # Anonymous users can view only a certain set of fields
        return AnonymousGlobalSettingsSerializer

    def get_object(self):
        return GlobalSettings.load()
