Coverage for backend/core/auxiliary/methods/copy_flowsheet/copy_caching.py: 100%
24 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
1from django.db.models import Model
2from dataclasses import dataclass
3from typing import List, Dict, Type
4from core.validation import flowsheet_ctx
7class ModelLookup:
8 """
9 A class to help map model primary keys to their respective model classes.
10 """
12 def __init__(self, object_list: List[Model]):
13 self.model_map: Dict[int, Model] = {}
14 for model in object_list:
15 self.model_map[model.pk] = model
17 def get_model(self, pk: int) -> Model:
18 return self.model_map.get(pk, None)
20 # Enable iteration over the model_map values
21 def __iter__(self):
22 return iter(self.model_map.values())
25@dataclass
26class RelatedModels:
27 model: Model
28 related_model_ids: List[int]
31def get_prefetch_fields(model: Type[Model]) -> List[str]:
32 """
33 We want to prefetch all fields that are many to many relationships
35 """
36 prefetch_fields = []
37 for field in model._meta.get_fields():
38 if field.is_relation and field.many_to_many and not field.auto_created:
39 prefetch_fields.append(field.name)
40 return prefetch_fields
43ModelLookupDict = dict[Type[Model], ModelLookup]