Skip to content

Commit

Permalink
Add API endpoint for the ProductDependency model #138
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jul 16, 2024
1 parent 872c144 commit db04c6d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dejacode/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from policy.api import UsagePolicyViewSet
from product_portfolio.api import CodebaseResourceViewSet
from product_portfolio.api import ProductComponentViewSet
from product_portfolio.api import ProductDependencyViewSet
from product_portfolio.api import ProductPackageViewSet
from product_portfolio.api import ProductViewSet
from reporting.api import ReportViewSet
Expand All @@ -69,6 +70,7 @@
api_router.register("packages", PackageViewSet)
api_router.register("products", ProductViewSet)
api_router.register("product_components", ProductComponentViewSet)
api_router.register("product_dependencies", ProductDependencyViewSet)
api_router.register("product_packages", ProductPackageViewSet)
api_router.register("codebase_resources", CodebaseResourceViewSet)
api_router.register("request_templates", RequestTemplateViewSet)
Expand Down
104 changes: 104 additions & 0 deletions product_portfolio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from product_portfolio.models import CodebaseResource
from product_portfolio.models import Product
from product_portfolio.models import ProductComponent
from product_portfolio.models import ProductDependency
from product_portfolio.models import ProductPackage

base_extra_kwargs = {
Expand Down Expand Up @@ -779,3 +780,106 @@ def get_queryset(self):
"product",
)
)


class ProductDependencyFilterSet(DataspacedAPIFilterSet):
uuid = MultipleUUIDFilter()
product = NameVersionFilter(
name_field_name="product__name",
version_field_name="product__version",
)
last_modified_date = LastModifiedDateFilter()

class Meta:
model = ProductDependency
fields = (
"uuid",
"product",
"dependency_uid",
"scope",
"datasource_id",
"is_runtime",
"is_optional",
"is_resolved",
"is_direct",
"last_modified_date",
)


class ProductDependencySerializer(DataspacedSerializer):
product = NameVersionHyperlinkedRelatedField(
view_name="api_v2:product-detail",
lookup_field="uuid",
allow_null=False,
)
for_package = DataspacedHyperlinkedRelatedField(
view_name="api_v2:package-detail",
lookup_field="uuid",
html_cutoff=10,
slug_field="filename",
)
resolved_to_package = DataspacedHyperlinkedRelatedField(
view_name="api_v2:package-detail",
lookup_field="uuid",
html_cutoff=10,
slug_field="filename",
)

class Meta:
model = ProductDependency
fields = (
"api_url",
"uuid",
"product",
"dependency_uid",
"for_package",
"resolved_to_package",
"extracted_requirement",
"scope",
"datasource_id",
"is_runtime",
"is_optional",
"is_resolved",
"is_direct",
"created_date",
"last_modified_date",
)
extra_kwargs = {
**base_extra_kwargs,
"api_url": {
"view_name": "api_v2:productdependency-detail",
"lookup_field": "uuid",
},
}


class ProductDependencyViewSet(ProductRelatedViewSet):
queryset = ProductDependency.objects.none()
serializer_class = ProductDependencySerializer
filterset_class = ProductDependencyFilterSet
search_fields = (
"for_package__filename",
"for_package__type",
"for_package__namespace",
"for_package__name",
"for_package__version",
"resolved_to_package__filename",
"resolved_to_package__type",
"resolved_to_package__namespace",
"resolved_to_package__name",
"resolved_to_package__version",
)
ordering_fields = (
"for_package",
"resolved_to_package",
)

def get_queryset(self):
return (
super()
.get_queryset()
.select_related(
"for_package__dataspace",
"resolved_to_package__dataspace",
)
)

0 comments on commit db04c6d

Please sign in to comment.