class DetailedFileSerializer(FileSha1Serializer): class Meta: model = models.File fields = "__all__" playbook = SimplePlaybookSerializer(read_only=True) content = ara_fields.FileContentField(read_only=True)
class FileSerializer(FileSha1Serializer): class Meta: model = models.File fields = "__all__" content = ara_fields.FileContentField() def get_unique_together_validators(self): """ Files have a "unique together" constraint for file.path and playbook.id. We want to have a "get_or_create" facility and in order to do that, we must manage the validation during the creation, not before. Overriding this method effectively disables this validator. """ return [] def create(self, validated_data): file_, created = models.File.objects.get_or_create( path=validated_data["path"], content=validated_data["content"], playbook=validated_data["playbook"], defaults=validated_data, ) return file_