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:
parent
a41468afee
commit
b061569bce
1 changed files with 41 additions and 0 deletions
41
coffeemachine/servers/forms.py
Normal file
41
coffeemachine/servers/forms.py
Normal 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
|
Loading…
Reference in a new issue