11import textwrap
2- from rest_framework import viewsets , filters , mixins
3- from rest_framework .response import Response
2+ from rest_framework import viewsets , mixins
43from django_filters .rest_framework import (
54 DjangoFilterBackend ,
65 FilterSet ,
76 CharFilter ,
8- MultipleChoiceFilter ,
97 BooleanFilter ,
108 BaseCSVFilter ,
119)
1210from django_filters .fields import ChoiceField
1311from obstracts .server import autoschema as api_schema
1412
15- from drf_spectacular .utils import extend_schema , extend_schema_view , OpenApiParameter
16- from drf_spectacular .types import OpenApiTypes
17- from django .db .models import F , Value , JSONField as DjangoJSONField , Min , Max
18- from django .db .models .functions import JSONObject
13+ from drf_spectacular .utils import extend_schema , extend_schema_view
14+ from django .db .models import F , Min , Max
1915from django .contrib .postgres .aggregates import ArrayAgg
2016
2117
2218from obstracts .server .models import ObjectValue
2319from obstracts .server .utils import Pagination
24- from obstracts .server .values .values import sco_value_map , sdo_value_map
20+ from obstracts .server .values .values import sco_value_map , sdo_value_map , KB_TYPES
2521from .serializers import ObjectValueSerializer
2622from dogesec_commons .utils .ordering import Ordering
23+ from .filters import NormalizeDict
2724
2825TTP_TYPES = [
2926 "cve" ,
@@ -111,7 +108,7 @@ class BaseObjectValueView(mixins.ListModelMixin, viewsets.GenericViewSet):
111108 pagination_class = Pagination ("values" )
112109 filter_backends = [DjangoFilterBackend , Ordering ]
113110 filterset_class = ObjectValueFilterSet
114- ordering_fields = ["stix_id" , "type" , "ttp_type " ]
111+ ordering_fields = ["stix_id" , "type" , "knowledgebase" , "value " ]
115112 ordering = "stix_id_descending"
116113 openapi_tags = ["Object Values" ]
117114
@@ -128,11 +125,12 @@ def get_queryset(self):
128125 # Aggregate all post_ids for each unique stix_id
129126 queryset = queryset .values ("stix_id" ).annotate (
130127 type = F ("type" ),
131- ttp_type = F ("ttp_type " ),
128+ knowledgebase = F ("knowledgebase " ),
132129 values = F ("values" ),
133130 matched_posts = ArrayAgg ("file__post_id" , distinct = True ),
134131 created = Min ("created" ),
135132 modified = Max ("modified" ),
133+ value = NormalizeDict (F ("values" )),
136134 )
137135
138136 return queryset
@@ -175,8 +173,8 @@ class SCOValueView(BaseObjectValueView):
175173 """View for STIX Cyber Observable Objects (SCOs) only."""
176174
177175 allowed_types = list (sco_value_map .keys ())
178- ordering_fields = ["values " , "stix_id" , "type" ]
179- ordering = "values_ascending "
176+ ordering_fields = ["value " , "stix_id" , "type" ]
177+ ordering = "value_ascending "
180178
181179 class filterset_class (ObjectValueFilterSet ):
182180 types = ChoiceCSVFilter (
@@ -225,11 +223,11 @@ class SDOValueView(BaseObjectValueView):
225223 """View for STIX Domain Objects (SDOs) only."""
226224
227225 allowed_types = list (sdo_value_map .keys ())
228- ordering_fields = ["stix_id" , "type" , "ttp_type " , "values " , "created" , "modified" ]
226+ ordering_fields = ["stix_id" , "type" , "knowledgebase " , "value " , "created" , "modified" ]
229227
230228 class filterset_class (ObjectValueFilterSet ):
231- ttp_types = ChoiceCSVFilter (
232- field_name = "ttp_type " ,
229+ knowledgebases = ChoiceCSVFilter (
230+ field_name = "knowledgebase " ,
233231 help_text = "Filter results by source of TTP object (cve, cwe, enterprise-attack, mobile-attack, ics-attack, capec, location, disarm, atlas, sector)" ,
234232 choices = [(c , c ) for c in TTP_TYPES ],
235233 )
@@ -238,3 +236,9 @@ class filterset_class(ObjectValueFilterSet):
238236 help_text = "Filter the results by one or more STIX Domain Object types" ,
239237 choices = [(c , c ) for c in sdo_value_map .keys ()],
240238 )
239+
240+ kb_type = ChoiceCSVFilter (
241+ field_name = "values__kb_type" ,
242+ help_text = "Filter results by knowledge base type." ,
243+ choices = [(c , c ) for c in KB_TYPES .keys ()],
244+ )
0 commit comments