Source code for oioioi.contests.fields

from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _

from oioioi.contests.scores import ScoreValue


[docs]class ScoreField(models.CharField): """Model field for storing :class:`~oioioi.contests.scores.ScoreValue`s"""
[docs] description = _("Score")
def __init__(self, *args, **kwargs): kwargs.setdefault('max_length', 255) super(ScoreField, self).__init__(*args, **kwargs)
[docs] def get_prep_value(self, value): if value is None: return None # The field might have been filled in code with some strange data # we deserialize it to make sure it's in proper format if isinstance(value, str): value = ScoreValue.deserialize(value) if isinstance(value, ScoreValue): return value.serialize() else: raise ValidationError('Invalid score value object type')
[docs] def from_db_value(self, value, expression, connection, context=None): if value is None or value == '': return None return ScoreValue.deserialize(value)
[docs] def value_to_string(self, obj): return self.get_prep_value(self.value_from_object(obj))
[docs] def to_python(self, value): if isinstance(value, ScoreValue): return value if value is None or value == '': return None return ScoreValue.deserialize(value)