212 lines
6.3 KiB
Python
212 lines
6.3 KiB
Python
|
"""
|
||
|
Serializers for the APIs.
|
||
|
"""
|
||
|
|
||
|
from rest_framework import serializers
|
||
|
from rest_framework.validators import UniqueTogetherValidator
|
||
|
|
||
|
from centralisedFileSystem.models import File, ScreenPlay, Script
|
||
|
from scriptAudit.models import ScriptAuditModel
|
||
|
|
||
|
|
||
|
class ScreenPlaySerializer(serializers.ModelSerializer):
|
||
|
'''
|
||
|
ScreenPlay model serializer.
|
||
|
For creating one-single ScreenPlay(not for creating script and a screenplay togeather)
|
||
|
'''
|
||
|
class Meta:
|
||
|
model = ScreenPlay
|
||
|
fields = "__all__"
|
||
|
|
||
|
def validate(self, data):
|
||
|
"""
|
||
|
Check that the start is before the stop.
|
||
|
"""
|
||
|
data = super().validate(data)
|
||
|
try:
|
||
|
data["user"] = self.context['request'].user
|
||
|
if ScreenPlay.objects.filter(
|
||
|
name=data["name"],
|
||
|
user=data["user"],
|
||
|
).exists():
|
||
|
raise serializers.ValidationError({
|
||
|
"non_field_errors": [
|
||
|
"The fields user, name must make a unique set."
|
||
|
]
|
||
|
})
|
||
|
|
||
|
except KeyError:
|
||
|
print(
|
||
|
"user was not found in request. Maybe Serializer was not saved from a APIView")
|
||
|
|
||
|
return data
|
||
|
|
||
|
|
||
|
class ScriptSerializer(serializers.ModelSerializer):
|
||
|
'''
|
||
|
Script model serializer.
|
||
|
For creating one-single script(not for creating script and a screenplay togeather)
|
||
|
'''
|
||
|
class InnerFileSerializer(serializers.ModelSerializer):
|
||
|
class Meta:
|
||
|
model = File
|
||
|
exclude = ("script", )
|
||
|
|
||
|
file = InnerFileSerializer()
|
||
|
|
||
|
def validate(self, data):
|
||
|
"""
|
||
|
Check that the start is before the stop.
|
||
|
"""
|
||
|
try:
|
||
|
if self.context['request'].user != ScreenPlay.objects.get(name=data['screenplay']).user:
|
||
|
raise serializers.ValidationError(
|
||
|
f"User dosen't have any screenplay named {data['screenplay']}")
|
||
|
except KeyError:
|
||
|
pass
|
||
|
|
||
|
return data
|
||
|
|
||
|
class Meta:
|
||
|
model = Script
|
||
|
fields = ("id", "screenplay", "file", "language",
|
||
|
"version", "created_on", "modified_on")
|
||
|
|
||
|
def create(self, validated_data) -> Script:
|
||
|
|
||
|
file_data = validated_data.pop('file')
|
||
|
script = Script.objects.create(**validated_data)
|
||
|
script.file = File.objects.create(script=script, **file_data)
|
||
|
|
||
|
return script
|
||
|
|
||
|
|
||
|
class FileSerializer(serializers.ModelSerializer):
|
||
|
|
||
|
class Meta:
|
||
|
model = File
|
||
|
fields = "__all__"
|
||
|
|
||
|
|
||
|
class ScreenPlayScriptSerializer(serializers.ModelSerializer):
|
||
|
'''
|
||
|
ScreenPlay model serializer.
|
||
|
For creating a script and a screenplay togeather.
|
||
|
'''
|
||
|
class InnerScriptSerializer(serializers.ModelSerializer):
|
||
|
'''
|
||
|
Script model serializer.
|
||
|
For creating one-single script(not for creating script and a screenplay togeather)
|
||
|
'''
|
||
|
class InnerFileSerializer(serializers.ModelSerializer):
|
||
|
class Meta:
|
||
|
model = File
|
||
|
# fields = "__all__"
|
||
|
exclude = ("script", )
|
||
|
|
||
|
file = InnerFileSerializer()
|
||
|
|
||
|
class Meta:
|
||
|
model = Script
|
||
|
fields = ("id", "file", "language", "version",
|
||
|
"created_on", "modified_on")
|
||
|
|
||
|
script = InnerScriptSerializer()
|
||
|
|
||
|
class Meta:
|
||
|
model = ScreenPlay
|
||
|
fields = ("user", "author", "name", "script",
|
||
|
"created_on", "version_count")
|
||
|
|
||
|
def validate(self, data):
|
||
|
"""
|
||
|
Check that the start is before the stop.
|
||
|
"""
|
||
|
data = super().validate(data)
|
||
|
try:
|
||
|
data["user"] = self.context['request'].user
|
||
|
except KeyError:
|
||
|
print(
|
||
|
"user was not found in request. Maybe Serializer was not saved from a APIView", data, self.context.items())
|
||
|
data["user"] = self.user
|
||
|
|
||
|
|
||
|
if ScreenPlay.objects.filter(
|
||
|
name = data["name"],
|
||
|
user = data["user"],
|
||
|
).exists():
|
||
|
raise serializers.ValidationError({
|
||
|
"non_field_errors": [
|
||
|
"The fields user, name must make a unique set."
|
||
|
]
|
||
|
})
|
||
|
|
||
|
return data
|
||
|
|
||
|
def create(self, validated_data) -> ScreenPlay:
|
||
|
|
||
|
script_data = validated_data.pop('script')
|
||
|
file_data = script_data.pop('file')
|
||
|
|
||
|
screenplay = ScreenPlay.objects.create(**validated_data)
|
||
|
|
||
|
screenplay.script = Script.objects.create(
|
||
|
screenplay=screenplay, **script_data)
|
||
|
screenplay.script.file = File.objects.create(
|
||
|
script=screenplay.script, **file_data)
|
||
|
|
||
|
return screenplay
|
||
|
|
||
|
|
||
|
class UserScriptsSerializer(serializers.ModelSerializer):
|
||
|
'''
|
||
|
To serialize all the scripts that a given user has.
|
||
|
'''
|
||
|
class InnerScriptSerializer(serializers.ModelSerializer):
|
||
|
|
||
|
class InnerAuditSerializer(serializers.ModelSerializer):
|
||
|
class Meta:
|
||
|
model = ScriptAuditModel
|
||
|
exclude = ("script", "id")
|
||
|
|
||
|
script_audit = InnerAuditSerializer(read_only=True)
|
||
|
|
||
|
class Meta:
|
||
|
model = Script
|
||
|
fields = ("id", "created_on", "modified_on",
|
||
|
"version", "language", "script_audit")
|
||
|
|
||
|
scripts = InnerScriptSerializer(many=True, read_only=True)
|
||
|
|
||
|
class Meta:
|
||
|
model = ScreenPlay
|
||
|
fields = ("id", "name", "author", "version_count",
|
||
|
"created_on", "scripts")
|
||
|
|
||
|
|
||
|
# class BeatSheetSerializer(serializers.ModelSerializer):
|
||
|
# class Meta:
|
||
|
# model = BeatSheet
|
||
|
# fields = ("script","scene_number","beat_note","gpt_response","beat")
|
||
|
# extra_kwargs = {
|
||
|
# 'scene_number': {'required': False}
|
||
|
# }
|
||
|
|
||
|
# class BeatSheetSerializer(serializers.ModelSerializer):
|
||
|
# class Meta:
|
||
|
# model = BeatSheet
|
||
|
# fields = '__all__'
|
||
|
# # from centralisedFileSystem.models import TitlePage
|
||
|
|
||
|
# # class TitlePageSerializer(serializers.ModelSerializer):
|
||
|
# class Meta:
|
||
|
# model = TitlePage
|
||
|
# fields = ("id","script","file","type","created_on")
|
||
|
|
||
|
# class SerializerBeatSheet(serializers.ModelSerializer):
|
||
|
# class Meta:
|
||
|
# model = BeatSheet
|
||
|
# fields = ["id","script","scene_number","beat","beat_note","created_on","modified_on","had_used_gpt","gpt_response","has_notes","archeive"]
|
||
|
|
||
|
|