-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6e8dc1
commit 4301381
Showing
3 changed files
with
31 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django.forms import CharField, ValidationError | ||
|
||
|
||
class MeanValuesTupleField(CharField): | ||
def to_python(self, value): | ||
try: | ||
values = tuple(map(float, value.split(", "))) | ||
if len(values) != 3: | ||
raise ValueError("The tuple must have exactly three elements.") | ||
if not all(-255 <= v <= 255 for v in values): | ||
raise ValueError("Each value in the tuple must be between -255 and 255.") | ||
return values | ||
except Exception as e: | ||
raise ValidationError( | ||
""" | ||
Enter a valid tuple of three float values separated by commas and spaces, e.g. '0.0, 0.0, 0.0'. | ||
Each value must be between -255 and 255. | ||
""" | ||
) from e | ||
|
||
def prepare_value(self, value): | ||
if isinstance(value, tuple): | ||
return ", ".join(map(str, value)) | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters