2022-11-11 11:20:17 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import ReadableHiddenField
|
2022-11-11 11:20:17 +00:00
|
|
|
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]
|
|
|
|
|
|
|
|
|
2023-12-19 10:42:44 +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
|
|
|
|
2023-02-16 07:36:54 +00:00
|
|
|
def to_internal_value(self, data):
|
|
|
|
name = data.get('name', False)
|
2023-02-20 07:50:43 +00:00
|
|
|
if not name and data.get('path'):
|
2023-02-16 07:36:54 +00:00
|
|
|
data['name'] = parse_playbook_name(data['path'].name)
|
|
|
|
return super().to_internal_value(data)
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Playbook
|
2022-11-30 08:24:17 +00:00
|
|
|
read_only_fields = ["id", "date_created", "date_updated"]
|
|
|
|
fields = read_only_fields + [
|
2023-12-05 03:16:34 +00:00
|
|
|
"id", 'path', "name", "comment", "creator",
|
2023-12-19 10:42:44 +00:00
|
|
|
'create_method', 'vcs_url',
|
2022-11-11 11:20:17 +00:00
|
|
|
]
|