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.
This commit is contained in:
Kumi 2024-06-05 16:59:29 +02:00
parent a41468afee
commit b061569bce
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -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