File tree Expand file tree Collapse file tree 4 files changed +237
-572
lines changed Expand file tree Collapse file tree 4 files changed +237
-572
lines changed Original file line number Diff line number Diff line change 22
33from django .core .exceptions import ValidationError
44from django .utils import timezone
5+ from django .utils .text import Truncator
56from huey .api import Task
67
78from huey_monitor .constants import TASK_MODEL_DESC_MAX_LENGTH
@@ -50,10 +51,12 @@ def __init__(self,
5051 if len (self .desc ) > TASK_MODEL_DESC_MAX_LENGTH :
5152 # We call .update() that will not validate the data, so a overlong
5253 # description will raise a database error and maybe a user doesn't know
53- # what's happen ;)
54- raise ValidationError (
55- 'Process info description overlong: %(desc)r' ,
56- params = {'desc' : self .desc },
54+ # what's happen -> Just crop it.
55+ self .desc = Truncator (self .desc ).chars (TASK_MODEL_DESC_MAX_LENGTH )
56+ logger .warning (
57+ 'Process info description %r has been cropped maximum allowed %i characters' ,
58+ self .desc ,
59+ TASK_MODEL_DESC_MAX_LENGTH ,
5760 )
5861
5962 TaskModel .objects .filter (task_id = task .id ).update (
Original file line number Diff line number Diff line change 11import datetime
2+ import logging
23import time
34from pathlib import Path
45from unittest import mock
@@ -162,8 +163,7 @@ def test_process_description_overlong(self):
162163
163164 # Test with current max length:
164165 max_length = TaskModel ._meta .get_field ('desc' ).max_length
165- assert max_length == TASK_MODEL_DESC_MAX_LENGTH
166- assert max_length == 128
166+ assert max_length == TASK_MODEL_DESC_MAX_LENGTH == 128
167167
168168 max_length_txt = 'X' * max_length
169169 overlong_txt = 'Y' * (max_length + 1 )
@@ -183,7 +183,12 @@ def test_process_description_overlong(self):
183183 )
184184 ]
185185
186- # Overlong description should be cut:
187- msg = f'["Process info description overlong: \' { overlong_txt } \' "]'
188- with self .assertRaisesMessage (ValidationError , msg ):
186+ # Overlong description will be cut:
187+ with self .assertLogs ('huey_monitor.tqdm' , level = logging .WARNING ) as logs :
189188 ProcessInfo (task , desc = overlong_txt , total = 999 )
189+ instance = TaskModel .objects .get ()
190+ assert len (instance .desc ) == TASK_MODEL_DESC_MAX_LENGTH == 128
191+
192+ assert len (logs .output ) == 1
193+ log_line = logs .output [0 ]
194+ assert "YYYYY…' has been cropped maximum allowed 128 characters" in log_line
You can’t perform that action at this time.
0 commit comments