Coverage for backend/django/flowsheetInternals/unitops/services/edit_operations/checks.py: 78%

24 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-07-22 05:22 +0000

1"""Django system checks for the flowsheet history model boundary.""" 

2 

3from django.core.exceptions import FieldDoesNotExist 

4from django.core.checks import Error, register 

5from django.db import models 

6 

7from .scope import PRIMARY, history_model_specs 

8 

9 

10EXCLUDED_PRIMARY_APP_LABELS = frozenset({"economics", "pinchanalysis"}) 

11 

12 

13@register() 

14def check_flowsheet_history_scope(app_configs, **kwargs): 

15 """Reject marker configurations that could broaden history accidentally.""" 

16 errors = [] 

17 for spec in history_model_specs().values(): 

18 if spec.is_automatic_through_model: 

19 continue 

20 flowsheet_state_field = next( 

21 (field for field in spec.fields if field.name == "flowsheet_state"), 

22 None, 

23 ) 

24 if ( 24 ↛ 28line 24 didn't jump to line 28 because the condition on line 24 was never true

25 not isinstance(flowsheet_state_field, models.ForeignKey) 

26 or flowsheet_state_field.null 

27 ): 

28 errors.append( 

29 Error( 

30 f"{spec.model._meta.label} must have a non-null direct " 

31 "flowsheet_state ForeignKey.", 

32 id="flowsheet_history.E001", 

33 ) 

34 ) 

35 if ( 35 ↛ 39line 35 didn't jump to line 39 because the condition on line 35 was never true

36 spec.scope == PRIMARY 

37 and spec.model._meta.app_label.lower() in EXCLUDED_PRIMARY_APP_LABELS 

38 ): 

39 errors.append( 

40 Error( 

41 f"{spec.model._meta.label} belongs to an excluded history domain.", 

42 hint="Use dependent scope only for an existing flowsheet compatibility row.", 

43 id="flowsheet_history.E002", 

44 ) 

45 ) 

46 if spec.soft_delete_field is not None: 

47 try: 

48 soft_delete_model_field = spec.model._meta.get_field( 

49 spec.soft_delete_field 

50 ) 

51 except FieldDoesNotExist: 

52 soft_delete_model_field = None 

53 if not isinstance(soft_delete_model_field, models.BooleanField): 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true

54 errors.append( 

55 Error( 

56 f"{spec.model._meta.label} history soft-delete field must be BooleanField.", 

57 id="flowsheet_history.E004", 

58 ) 

59 ) 

60 return errors