Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.6 KiB
77 lines
2.6 KiB
from rest_framework import viewsets,filters
|
|
from . import models as m
|
|
from . import views
|
|
|
|
class ReadWriteSerializerMixin(object):
|
|
"""
|
|
Overrides get_serializer_class to choose the read serializer
|
|
for GET requests and the write serializer for POST requests.
|
|
|
|
Set read_serializer_class and write_serializer_class attributes on a
|
|
viewset.
|
|
"""
|
|
|
|
read_serializer_class = None
|
|
create_serializer_class = None
|
|
write_serializer_class = None
|
|
|
|
def get_serializer_class(self):
|
|
if self.action == "create":
|
|
return self.get_create_serializer_class()
|
|
if self.action in ["update", "partial_update", "destroy"]:
|
|
return self.get_write_serializer_class()
|
|
return self.get_read_serializer_class()
|
|
|
|
def get_read_serializer_class(self):
|
|
assert self.read_serializer_class is not None, (
|
|
"'%s' should either include a `read_serializer_class` attribute,"
|
|
"or override the `get_read_serializer_class()` method."
|
|
% self.__class__.__name__
|
|
)
|
|
return self.read_serializer_class
|
|
|
|
def get_write_serializer_class(self):
|
|
assert self.write_serializer_class is not None, (
|
|
"'%s' should either include a `write_serializer_class` attribute,"
|
|
"or override the `get_write_serializer_class()` method."
|
|
% self.__class__.__name__
|
|
)
|
|
return self.write_serializer_class
|
|
|
|
def get_create_serializer_class(self):
|
|
assert self.create_serializer_class is not None, (
|
|
"'%s' should either include a `create_serializer_class` attribute,"
|
|
"or override the `get_create_serializer_class()` method."
|
|
% self.__class__.__name__
|
|
)
|
|
return self.create_serializer_class
|
|
|
|
class UlohaVzorakNodeViewSet(viewsets.ModelViewSet):
|
|
queryset = m.UlohaVzorakNode.objects.all()
|
|
serializer_class = views.UlohaVzorakNodeSerializer
|
|
|
|
class TextViewSet(viewsets.ModelViewSet):
|
|
queryset = m.Text.objects.all()
|
|
serializer_class = views.TextSerializer
|
|
|
|
class TextNodeViewSet(ReadWriteSerializerMixin,viewsets.ModelViewSet):
|
|
queryset = m.TextNode.objects.all()
|
|
read_serializer_class = views.TextNodeSerializer
|
|
write_serializer_class = views.TextNodeWriteSerializer
|
|
create_serializer_class = views.TextNodeCreateSerializer
|
|
|
|
class CastNodeViewSet(ReadWriteSerializerMixin,viewsets.ModelViewSet):
|
|
queryset = m.CastNode.objects.all()
|
|
read_serializer_class = views.CastNodeSerializer
|
|
write_serializer_class = views.CastNodeSerializer
|
|
create_serializer_class = views.CastNodeCreateSerializer
|
|
|
|
class UlohaVzorakNodeViewSet(viewsets.ModelViewSet):
|
|
serializer_class = views.UlohaVzorakNodeSerializer
|
|
|
|
def get_queryset(self):
|
|
queryset = m.UlohaVzorakNode.objects.all()
|
|
nazev = self.request.query_params.get('nazev',None)
|
|
if nazev is not None:
|
|
queryset = queryset.filter(nazev__contains=nazev)
|
|
return queryset
|
|
|