def validate_template(self, value: str) -> str: """Check validity of template""" choices = get_template_choices() for path, _ in choices: if path == value: return value raise ValidationError(f"Invalid template '{value}' specified.")
def templates(self, request: Request) -> Response: """Get all available templates, including custom templates""" choices = [] for value, label in get_template_choices(): choices.append({ "name": value, "description": label, "component": "", "model_name": "", }) return Response(TypeCreateSerializer(choices, many=True).data)
def test_custom_template(self): """Test with custom template""" with self.settings(TEMPLATES=get_templates_setting(gettempdir())): _, file = mkstemp(suffix=".html") _, file2 = mkstemp(suffix=".html") chmod(file2, 0o000) # Remove all permissions so we can't read the file choices = get_template_choices() self.assertEqual(choices[-1][0], Path(file).name) self.assertEqual(len(choices), 3) unlink(file) unlink(file2)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["template"].choices = get_template_choices()