blueprints: Fix resolve model_name in `!Find` tag (#4371)

Resolve model_name in !Find tag
This commit is contained in:
sdimovv 2023-01-06 08:49:28 +00:00 committed by GitHub
parent 0fb2b5550a
commit c63ba3f378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -25,7 +25,7 @@ entries:
[slug, default-source-authentication], [slug, default-source-authentication],
] ]
enrollment_flow: enrollment_flow:
!Find [authentik_flows.Flow, [slug, default-source-enrollment]] !Find [!Format ["%s", authentik_flows.Flow], [slug, default-source-enrollment]]
- attrs: - attrs:
expression: return True expression: return True
identifiers: identifiers:

View File

@ -287,15 +287,12 @@ class Format(YAMLTag):
class Find(YAMLTag): class Find(YAMLTag):
"""Find any object""" """Find any object"""
model_name: str model_name: str | YAMLTag
conditions: list[list] conditions: list[list]
model_class: type[Model]
def __init__(self, loader: "BlueprintLoader", node: SequenceNode) -> None: def __init__(self, loader: "BlueprintLoader", node: SequenceNode) -> None:
super().__init__() super().__init__()
self.model_name = node.value[0].value self.model_name = loader.construct_object(node.value[0])
self.model_class = apps.get_model(*self.model_name.split("."))
self.conditions = [] self.conditions = []
for raw_node in node.value[1:]: for raw_node in node.value[1:]:
values = [] values = []
@ -304,6 +301,13 @@ class Find(YAMLTag):
self.conditions.append(values) self.conditions.append(values)
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any: def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
if isinstance(self.model_name, YAMLTag):
model_name = self.model_name.resolve(entry, blueprint)
else:
model_name = self.model_name
model_class = apps.get_model(*model_name.split("."))
query = Q() query = Q()
for cond in self.conditions: for cond in self.conditions:
if isinstance(cond[0], YAMLTag): if isinstance(cond[0], YAMLTag):
@ -315,7 +319,7 @@ class Find(YAMLTag):
else: else:
query_value = cond[1] query_value = cond[1]
query &= Q(**{query_key: query_value}) query &= Q(**{query_key: query_value})
instance = self.model_class.objects.filter(query).first() instance = model_class.objects.filter(query).first()
if instance: if instance:
return instance.pk return instance.pk
return None return None