from django.db.models import Q
from rest_framework import routers, permissions
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework import viewsets, views
from ..tools import create_codocks_card
from . import serializers
from .. import models
[docs]
class CsrfExemptSessionAuthentication(SessionAuthentication):
[docs]
def enforce_csrf(self, request):
return
[docs]
class CustomAuthToken(ObtainAuthToken):
authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication)
[docs]
class UserViewSet(viewsets.ModelViewSet):
serializer_class = serializers.UserSerializer
queryset = models.User.objects.all()
filterset_fields = ['organization', 'username']
[docs]
class ContentViewSet(viewsets.ModelViewSet):
serializer_class = serializers.ContentSerializer
queryset = models.Content.objects.all()
authentication_classes = []
permission_classes = []
[docs]
def get_queryset(self):
content = self.request.query_params.get('content')
if content:
try:
content = int(content)
return self.queryset.filter(id=content)
except ValueError:
return self.queryset.filter(special=content)
return self.queryset
[docs]
class WorldObjectViewSet(viewsets.ModelViewSet):
serializer_class = serializers.WorldObjectSerializer
queryset = models.WorldObject.objects.all()
authentication_classes = []
permission_classes = []
[docs]
class TeamViewSet(viewsets.ModelViewSet):
serializer_class = serializers.TeamSerializer
queryset = models.Team.objects.all()
permission_classes = [permissions.IsAuthenticated, ]
filterset_fields = ['id']
[docs]
def get_queryset(self):
return self.queryset.filter(Q(admins__in=[self.request.user]) | Q(organization__admins__in=[self.request.user])).distinct()
[docs]
class FeedbackTicketViewSet(viewsets.ModelViewSet):
serializer_class = serializers.FeedbackTicketSerializer
queryset = models.FeedbackTicket.objects.filter()
authentication_classes = []
permission_classes = []
router = routers.DefaultRouter()
router.register('user', UserViewSet)
router.register('team-content', ContentViewSet)
router.register('worldobject', WorldObjectViewSet)
router.register('team', TeamViewSet)
router.register('feedbackticket', FeedbackTicketViewSet)