feat(api): enable writes to schedule table (#3109)

this fixes #3088

---------

Co-authored-by: Kyle Robbertze <paddatrapper@users.noreply.github.com>
This commit is contained in:
Thomas Göttgens 2024-11-18 17:10:54 +01:00 committed by GitHub
parent 824f6d2f1b
commit 2ac7e8a506
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 194 additions and 55 deletions

View File

@ -0,0 +1 @@
from .readwriteserializer import ReadWriteSerializerMixin

View File

@ -0,0 +1,33 @@
from rest_framework.serializers import Serializer
class ReadWriteSerializerMixin:
"""
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 = Serializer
write_serializer_class = Serializer
def get_serializer_class(self):
if self.action in ["create"]:
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, (
f"'{self.__class__.__name__}' should either include a `read_serializer_class`"
"attribute, or override the `get_read_serializer_class()` method."
)
return self.read_serializer_class
def get_write_serializer_class(self):
assert self.write_serializer_class is not None, (
f"'{self.__class__.__name__}' should either include a `write_serializer_class`"
"attribute, or override the `get_write_serializer_class()` method."
)
return self.write_serializer_class

View File

@ -1,5 +1,5 @@
from .playlist import PlaylistContentSerializer, PlaylistSerializer from .playlist import PlaylistContentSerializer, PlaylistSerializer
from .schedule import ScheduleSerializer from .schedule import ReadScheduleSerializer, WriteScheduleSerializer
from .show import ( from .show import (
ShowDaysSerializer, ShowDaysSerializer,
ShowHostSerializer, ShowHostSerializer,

View File

@ -3,10 +3,17 @@ from rest_framework import serializers
from ..models import Schedule from ..models import Schedule
class ScheduleSerializer(serializers.ModelSerializer): class ReadScheduleSerializer(serializers.ModelSerializer):
cue_out = serializers.DurationField(source="get_cue_out", read_only=True) cue_out = serializers.DurationField(source="get_cue_out", read_only=True)
ends_at = serializers.DateTimeField(source="get_ends_at", read_only=True) ends_at = serializers.DateTimeField(source="get_ends_at", read_only=True)
class Meta: class Meta:
model = Schedule model = Schedule
fields = "__all__" fields = "__all__"
class WriteScheduleSerializer(serializers.ModelSerializer):
class Meta:
model = Schedule
fields = "__all__"

View File

@ -2,8 +2,9 @@ from django.db import models
from django_filters import rest_framework as filters from django_filters import rest_framework as filters
from rest_framework import viewsets from rest_framework import viewsets
from ...mixins import ReadWriteSerializerMixin
from ..models import Schedule from ..models import Schedule
from ..serializers import ScheduleSerializer from ..serializers import ReadScheduleSerializer, WriteScheduleSerializer
class ScheduleFilter(filters.FilterSet): class ScheduleFilter(filters.FilterSet):
@ -26,8 +27,9 @@ class ScheduleFilter(filters.FilterSet):
fields = [] # type: ignore fields = [] # type: ignore
class ScheduleViewSet(viewsets.ModelViewSet): class ScheduleViewSet(ReadWriteSerializerMixin, viewsets.ModelViewSet):
queryset = Schedule.objects.all() queryset = Schedule.objects.all()
serializer_class = ScheduleSerializer read_serializer_class = ReadScheduleSerializer
write_serializer_class = WriteScheduleSerializer
filterset_class = ScheduleFilter filterset_class = ScheduleFilter
model_permission_name = "schedule" model_permission_name = "schedule"

View File

@ -2552,6 +2552,12 @@ paths:
/api/v2/schedule: /api/v2/schedule:
get: get:
operationId: schedule_list operationId: schedule_list
description: |-
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.
parameters: parameters:
- in: query - in: query
name: broadcasted name: broadcasted
@ -2597,23 +2603,29 @@ paths:
schema: schema:
type: array type: array
items: items:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
description: "" description: ""
post: post:
operationId: schedule_create operationId: schedule_create
description: |-
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.
tags: tags:
- schedule - schedule
requestBody: requestBody:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/WriteSchedule"
application/x-www-form-urlencoded: application/x-www-form-urlencoded:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/WriteSchedule"
multipart/form-data: multipart/form-data:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/WriteSchedule"
required: true required: true
security: security:
- cookieAuth: [] - cookieAuth: []
@ -2623,11 +2635,17 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/WriteSchedule"
description: "" description: ""
/api/v2/schedule/{id}: /api/v2/schedule/{id}:
get: get:
operationId: schedule_retrieve operationId: schedule_retrieve
description: |-
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.
parameters: parameters:
- in: path - in: path
name: id name: id
@ -2645,10 +2663,16 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
description: "" description: ""
put: put:
operationId: schedule_update operationId: schedule_update
description: |-
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.
parameters: parameters:
- in: path - in: path
name: id name: id
@ -2662,13 +2686,13 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
application/x-www-form-urlencoded: application/x-www-form-urlencoded:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
multipart/form-data: multipart/form-data:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
required: true required: true
security: security:
- cookieAuth: [] - cookieAuth: []
@ -2678,10 +2702,16 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
description: "" description: ""
patch: patch:
operationId: schedule_partial_update operationId: schedule_partial_update
description: |-
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.
parameters: parameters:
- in: path - in: path
name: id name: id
@ -2695,13 +2725,13 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/PatchedSchedule" $ref: "#/components/schemas/PatchedReadSchedule"
application/x-www-form-urlencoded: application/x-www-form-urlencoded:
schema: schema:
$ref: "#/components/schemas/PatchedSchedule" $ref: "#/components/schemas/PatchedReadSchedule"
multipart/form-data: multipart/form-data:
schema: schema:
$ref: "#/components/schemas/PatchedSchedule" $ref: "#/components/schemas/PatchedReadSchedule"
security: security:
- cookieAuth: [] - cookieAuth: []
- basicAuth: [] - basicAuth: []
@ -2710,10 +2740,16 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/Schedule" $ref: "#/components/schemas/ReadSchedule"
description: "" description: ""
delete: delete:
operationId: schedule_destroy operationId: schedule_destroy
description: |-
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.
parameters: parameters:
- in: path - in: path
name: id name: id
@ -6300,7 +6336,7 @@ components:
user: user:
type: integer type: integer
nullable: true nullable: true
PatchedSchedule: PatchedReadSchedule:
type: object type: object
properties: properties:
id: id:
@ -7096,41 +7132,7 @@ components:
- id - id
- key - key
- user - user
RecordEnabledEnum: ReadSchedule:
enum:
- 0
- 1
type: integer
description: |-
* `0` - No
* `1` - Yes
RepeatKindEnum:
enum:
- 0
- 1
- 4
- 5
- 2
type: integer
description: |-
* `0` - Every week
* `1` - Every 2 weeks
* `4` - Every 3 weeks
* `5` - Every 4 weeks
* `2` - Every month
RoleEnum:
enum:
- G
- H
- P
- A
type: string
description: |-
* `G` - Guest
* `H` - Host
* `P` - Manager
* `A` - Admin
Schedule:
type: object type: object
properties: properties:
id: id:
@ -7192,6 +7194,40 @@ components:
- instance - instance
- position - position
- starts_at - starts_at
RecordEnabledEnum:
enum:
- 0
- 1
type: integer
description: |-
* `0` - No
* `1` - Yes
RepeatKindEnum:
enum:
- 0
- 1
- 4
- 5
- 2
type: integer
description: |-
* `0` - Every week
* `1` - Every 2 weeks
* `4` - Every 3 weeks
* `5` - Every 4 weeks
* `2` - Every month
RoleEnum:
enum:
- G
- H
- P
- A
type: string
description: |-
* `G` - Guest
* `H` - Host
* `P` - Manager
* `A` - Admin
ServiceRegister: ServiceRegister:
type: object type: object
properties: properties:
@ -7795,6 +7831,66 @@ components:
* `4` - Friday * `4` - Friday
* `5` - Saturday * `5` - Saturday
* `6` - Sunday * `6` - Sunday
WriteSchedule:
type: object
properties:
id:
type: integer
readOnly: true
starts_at:
type: string
format: date-time
ends_at:
type: string
format: date-time
length:
type: string
nullable: true
fade_in:
type: string
format: time
nullable: true
fade_out:
type: string
format: time
nullable: true
cue_in:
type: string
cue_out:
type: string
position:
type: integer
maximum: 2147483647
minimum: -2147483648
position_status:
allOf:
- $ref: "#/components/schemas/PositionStatusEnum"
minimum: -32768
maximum: 32767
broadcasted:
type: integer
maximum: 32767
minimum: -32768
played:
type: boolean
nullable: true
instance:
type: integer
file:
type: integer
nullable: true
stream:
type: integer
nullable: true
required:
- broadcasted
- cue_in
- cue_out
- ends_at
- id
- instance
- position
- starts_at
securitySchemes: securitySchemes:
basicAuth: basicAuth:
type: http type: http