Skip to content

Commit

Permalink
Implement auto-trim char field
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic committed Jul 22, 2024
1 parent 64afbeb commit b8bfbde
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions bot/common/model_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ def deconstruct(self) -> typing.Tuple[str, str, typing.Any, typing.Any]:
name, path, args, kwargs = super().deconstruct()
kwargs['enum'] = self.enum
return name, path, args, kwargs


class CustomCharField(models.CharField):
def __init__(self, *args, **kwargs) -> None:
self.auto_trim = kwargs.pop('auto_trim', False)
super().__init__(*args, **kwargs)

def get_prep_value(self, value: str) -> str:
value = super().get_prep_value(value)

if self.auto_trim is True and len(value) > self.max_length:
return value[: self.max_length]

return value
3 changes: 2 additions & 1 deletion bot/models/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ class Post(models.Model):
null=True,
default=None,
)
description = models.CharField(
description = model_fields.CustomCharField(
max_length=2000,
null=True,
default=None,
auto_trim=True,
)
views = models.BigIntegerField(
null=True,
Expand Down

0 comments on commit b8bfbde

Please sign in to comment.