Coverage for apis_core/generic/utils/models.py: 53%

17 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-28 06:34 +0000

1from typing import Optional, Union 

2 

3from django.utils.functional import Promise 

4from pydantic import BaseModel, ConfigDict, ValidationError 

5 

6 

7class ValidationModel(BaseModel): 

8 """ 

9 A pydantic `BaseModel` that provides a class method to 

10 turn pydantic validation errors into strings, usable by 

11 Django to pass on to an `Error` for example. 

12 """ 

13 

14 @classmethod 

15 def validation_errors_to_error_messages(cls, validate_cls): 

16 errors = [] 

17 try: 

18 cls.model_validate(validate_cls(), from_attributes=True) 

19 except ValidationError as e: 

20 for error in e.errors(): 

21 for loc in error["loc"]: 

22 errors.append(f"Config.{loc} {error['msg']}") 

23 return errors 

24 

25 

26class ConfigModel(ValidationModel): 

27 """ 

28 Pydantic based valdiation model for the config class used 

29 in the `GenericModel` 

30 """ 

31 

32 model_config = ConfigDict(arbitrary_types_allowed=True) 

33 overview_section: Optional[Union[str, Promise]] = None