Source code for memopolis.api.viewsets

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] def perform_create(self, serializer): response = super().perform_create(serializer) instance = serializer.instance instance.admins.add(self.request.user) return response
[docs] class FeedbackTicketViewSet(viewsets.ModelViewSet): serializer_class = serializers.FeedbackTicketSerializer queryset = models.FeedbackTicket.objects.filter() authentication_classes = [] permission_classes = []
[docs] def perform_create(self, serializer): ticket = serializer.save() if False: create_codocks_card( None, "e9ff39d0-b568-11ec-bf74-533552242bc7", "rotxblau", "8fFCCTbl4qCbOHt5zmzHSwH6", ticket, "%s/admin/memopolis/feedbackticket/%d" % (settings.URL, ticket.id) )
router = routers.DefaultRouter() router.register('user', UserViewSet) router.register('team-content', ContentViewSet) router.register('worldobject', WorldObjectViewSet) router.register('team', TeamViewSet) router.register('feedbackticket', FeedbackTicketViewSet)