2022-11-11 11:20:17 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from common.drf.fields import ReadableHiddenField
|
|
|
|
from ops.models import Playbook
|
2022-11-29 11:44:12 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_playbook_name(path):
|
|
|
|
file_name = os.path.split(path)[-1]
|
|
|
|
return file_name.split(".")[-2]
|
|
|
|
|
|
|
|
|
2022-12-15 09:25:21 +00:00
|
|
|
class PlaybookSerializer(BulkOrgResourceModelSerializer):
|
2022-11-29 11:44:12 +00:00
|
|
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
2022-12-02 04:21:56 +00:00
|
|
|
path = serializers.FileField(required=False)
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
name = validated_data.get('name')
|
|
|
|
if not name:
|
|
|
|
path = validated_data.get('path').name
|
|
|
|
validated_data['name'] = parse_playbook_name(path)
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Playbook
|
2022-11-30 08:24:17 +00:00
|
|
|
read_only_fields = ["id", "date_created", "date_updated"]
|
|
|
|
fields = read_only_fields + [
|
2022-12-02 04:21:56 +00:00
|
|
|
"id", 'path', "name", "comment", "creator",
|
2022-11-11 11:20:17 +00:00
|
|
|
]
|