From b061569bce4bf8b04c8147d33286e30e4b8b98a0 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 5 Jun 2024 16:59:29 +0200 Subject: [PATCH] feat(servers): Add GameServerForm for server management Introduced a new form `GameServerForm` in the `coffeemachine` project to streamline the management of game servers. This form supports adding either new or existing servers, with options to upload or directly input custom scripts. Key validations include ensuring the installation path is absolute and preventing simultaneous provision of both script file and content to avoid conflicts. This enhancement simplifies the setup process for administrators and reinforces data integrity checks. --- coffeemachine/servers/forms.py | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 coffeemachine/servers/forms.py diff --git a/coffeemachine/servers/forms.py b/coffeemachine/servers/forms.py new file mode 100644 index 0000000..780a363 --- /dev/null +++ b/coffeemachine/servers/forms.py @@ -0,0 +1,41 @@ +from django import forms +from .models import GameServer +import os + + +class GameServerForm(forms.ModelForm): + existing_server = forms.BooleanField(required=False, label="Add Existing Server") + script_file = forms.FileField(required=False, label="Upload Custom Script") + script_content = forms.CharField( + widget=forms.Textarea, required=False, label="Or Enter Custom Script" + ) + + class Meta: + model = GameServer + fields = [ + "name", + "game", + "installation_path", + "existing_server", + "script_file", + "script_content", + ] + + def clean_installation_path(self): + path = self.cleaned_data["installation_path"] + if not os.path.isabs(path): + raise forms.ValidationError( + "The installation path must be an absolute path." + ) + return path + + def clean(self): + cleaned_data = super().clean() + script_file = cleaned_data.get("script_file") + script_content = cleaned_data.get("script_content") + + if script_file and script_content: + raise forms.ValidationError( + "Please provide either a script file or script content, not both." + ) + return cleaned_data