|
18 | 18 | ScanReportTableEditSerializer, |
19 | 19 | ScanReportTableListSerializerV2, |
20 | 20 | ScanReportValueViewSerializerV2, |
| 21 | + ScanReportValueViewSerializerV3, |
21 | 22 | ScanReportViewSerializerV2, |
22 | 23 | UserSerializer, |
23 | 24 | ) |
@@ -866,11 +867,72 @@ def get(self, request, *args, **kwargs): |
866 | 867 |
|
867 | 868 | return self.list(request, *args, **kwargs) |
868 | 869 |
|
| 870 | + def get_queryset(self): |
| 871 | + return ScanReportValue.objects.filter(scan_report_field=self.field).order_by( |
| 872 | + "id" |
| 873 | + ) |
| 874 | + |
| 875 | + @method_decorator(cache_page(60 * 15)) |
| 876 | + @method_decorator(vary_on_cookie) |
| 877 | + def list(self, request, *args, **kwargs): |
| 878 | + return super().list(request, *args, **kwargs) |
| 879 | + |
| 880 | + |
| 881 | +class ScanReportValueListV3(ScanReportPermissionMixin, GenericAPIView, ListModelMixin): |
| 882 | + """ |
| 883 | + A view for listing ScanReportValue objects associated with a |
| 884 | + specific ScanReportField. This view provides filtering, |
| 885 | + pagination, and caching capabilities for the ScanReportValue |
| 886 | + objects. It uses DjangoFilterBackend for filtering and a custom |
| 887 | + pagination class for paginated responses. The view also caches the |
| 888 | + list response for 15 minutes. |
| 889 | +
|
| 890 | + Attributes: |
| 891 | + filterset_fields (dict): Specifies the fields and lookup types |
| 892 | + available for filtering. |
| 893 | + filter_backends (list): Specifies the filter backends to be |
| 894 | + used. |
| 895 | + pagination_class (class): Specifies the pagination class to be |
| 896 | + used. |
| 897 | + serializer_class (class): Specifies the serializer class to be |
| 898 | + used for the response. |
| 899 | +
|
| 900 | + Methods: |
| 901 | + get(request, *args, **kwargs): |
| 902 | + Handles GET requests and retrieves the ScanReportField |
| 903 | + object based on the provided field_pk. Returns the list of |
| 904 | + ScanReportValue objects associated with the field. |
| 905 | +
|
| 906 | + get_queryset(): |
| 907 | + Returns the queryset of ScanReportValue objects filtered by |
| 908 | + the associated ScanReportField. The queryset is ordered by |
| 909 | + ID and only includes specific fields. |
| 910 | +
|
| 911 | + list(request, *args, **kwargs): |
| 912 | + Overrides the default list method to add caching and |
| 913 | + vary-on-cookie functionality. Returns the paginated list of |
| 914 | + ScanReportValue objects. |
| 915 | + """ |
| 916 | + |
| 917 | + filterset_fields = { |
| 918 | + "value": ["in", "icontains"], |
| 919 | + } |
| 920 | + filter_backends = [DjangoFilterBackend] |
| 921 | + pagination_class = CustomPagination |
| 922 | + serializer_class = ScanReportValueViewSerializerV3 |
| 923 | + |
| 924 | + @extend_schema(responses=ScanReportValueViewSerializerV3) |
| 925 | + def get(self, request, *args, **kwargs): |
| 926 | + self.field = get_object_or_404(ScanReportField, pk=kwargs["field_pk"]) |
| 927 | + |
| 928 | + return self.list(request, *args, **kwargs) |
| 929 | + |
869 | 930 | def get_queryset(self): |
870 | 931 | return ( |
871 | 932 | ScanReportValue.objects.filter(scan_report_field=self.field) |
872 | 933 | .order_by("id") |
873 | | - .only("id", "value", "frequency", "value_description", "scan_report_field") |
| 934 | + .select_related("scan_report_field") |
| 935 | + .prefetch_related("concepts", "concepts__concept") |
874 | 936 | ) |
875 | 937 |
|
876 | 938 | @method_decorator(cache_page(60 * 15)) |
|
0 commit comments