extensions-website/extensions/fields.py
Dalai Felinto 4d25a06ba9 Cleanup: black and flake8
I didn't have my pre-commit hooks enabled until now.

Redeeming myself by running `pre-commit run --all-files` and manually
cleaning some cases.
2024-01-29 15:06:36 +01:00

33 lines
923 B
Python

from semantic_version.django_fields import VersionField as SemanticVersionField
from semantic_version import Version
import json
class VersionStringField(SemanticVersionField):
description = "A field to store serializable semantic versions"
def to_python(self, value):
if isinstance(value, Version):
return value
if value is None:
return value
return str(Version(value))
def from_db_value(self, value, expression, connection):
return self.to_python(value)
def get_prep_value(self, value):
if value is None:
return value
return str(value)
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)
def from_json(self, json_str):
return self.to_python(json.loads(json_str))
def to_json(self, value):
return json.dumps(str(value))