Dalai Felinto
4d25a06ba9
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.
33 lines
923 B
Python
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))
|