MCPcopy Create free account
hub / github.com/intelowlproject/IntelOwl / annotate_configured

Method annotate_configured

api_app/queryset.py:788–842  ·  view source on GitHub ↗

Annotates configurations indicating if they are fully configured. Args: user (User, optional): The user to check. Defaults to None. Returns: PythonConfigQuerySet: The annotated queryset.

(self, user: User = None)

Source from the content-addressed store, hash-verified

786 """
787
788 def annotate_configured(self, user: User = None) -> "PythonConfigQuerySet":
789 """
790 Annotates configurations indicating if they are fully configured.
791
792 Args:
793 user (User, optional): The user to check. Defaults to None.
794
795 Returns:
796 PythonConfigQuerySet: The annotated queryset.
797 """
798 # a Python plugin is configured only if every required parameter is configured
799 from api_app.models import Parameter, PluginConfig
800
801 return (
802 # we retrieve the number or required parameters
803 self.alias(
804 required_params=Coalesce(
805 Subquery(
806 Parameter.objects.filter(python_module=OuterRef("python_module"), required=True)
807 # count them
808 .annotate(count=Func(F("pk"), function="Count"))
809 .values("count"),
810 output_field=IntegerField(),
811 ),
812 0,
813 )
814 )
815 # how many of them are configured
816 .alias(
817 # just to be sure that if the query fails, we return an integered
818 required_configured_params=Coalesce(
819 Subquery(
820 # we count how many parameters have a valid value
821 # considering the values that the user has access to
822 Parameter.objects.filter(
823 pk__in=Subquery(
824 # we get all values that the user can see
825 PluginConfig.objects.filter(
826 **{self.model.snake_case_name: OuterRef(OuterRef("pk"))},
827 parameter__required=True,
828 )
829 .visible_for_user(user)
830 .values("parameter__pk")
831 )
832 )
833 .annotate(count=Func(F("pk"), function="Count"))
834 .values("count"),
835 output_field=IntegerField(),
836 ),
837 0,
838 )
839 )
840 # and we save the difference
841 .annotate(configured=Exact(F("required_params") - F("required_configured_params"), 0))
842 )
843
844 def annotate_runnable(self, user: User = None) -> "PythonConfigQuerySet":
845 """

Callers

nothing calls this directly

Calls 2

valuesMethod · 0.80
visible_for_userMethod · 0.45

Tested by

no test coverage detected