From e9d967eed694f36d7522c19e611e36533f6d0389 Mon Sep 17 00:00:00 2001 From: Drikus Roor Date: Tue, 28 May 2024 10:32:58 +0200 Subject: [PATCH] feat: Add playlist validation to ExperimentForm clean method --- backend/experiment/forms.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/experiment/forms.py b/backend/experiment/forms.py index 2ae4c118d..5d4d726b8 100644 --- a/backend/experiment/forms.py +++ b/backend/experiment/forms.py @@ -170,6 +170,26 @@ def __init__(self, *args, **kwargs): required=False ) + def clean(self): + + # Validat the rules' playlist + rule_id = self.cleaned_data['rules'] + cl = EXPERIMENT_RULES[rule_id] + rules = cl() + + playlists = self.cleaned_data['playlists'] + playlist_errors = [] + + # Validate playlists + for playlist in playlists: + errors = rules.validate_playlist(playlist) + + for error in errors: + playlist_errors.append(f"Playlist {playlist.name}: {error}") + + if playlist_errors: + self.add_error('playlists', playlist_errors) + class Meta: model = Experiment fields = ['name', 'slug', 'active', 'rules',