1
- from django .db .models import Q
2
1
from django_filters .rest_framework import DjangoFilterBackend
3
2
from rest_framework import viewsets
3
+ from rest_framework .exceptions import ValidationError
4
4
5
5
from .models import EnvironmentalJusticeRow
6
6
from .serializers import EnvironmentalJusticeRowSerializer
@@ -17,31 +17,27 @@ class EnvironmentalJusticeRowViewSet(viewsets.ModelViewSet):
17
17
serializer_class = EnvironmentalJusticeRowSerializer
18
18
http_method_names = ["get" ]
19
19
filter_backends = [DjangoFilterBackend ]
20
- filterset_fields = ["data_source" ]
20
+ filterset_fields = []
21
21
22
22
def get_combined_queryset (self ):
23
23
"""
24
24
Returns combined data where:
25
25
1. All spreadsheet data is included
26
26
2. ML production data is included only if there's no spreadsheet data with matching dataset
27
+ Records are sorted by dataset name and then data_source (ensuring spreadsheet comes before ml_production)
27
28
"""
28
- # First, get all unique datasets that exist in spreadsheet
29
- spreadsheet_datasets = (
30
- EnvironmentalJusticeRow .objects .filter (data_source = EnvironmentalJusticeRow .DataSourceChoices .SPREADSHEET )
31
- .values_list ("dataset" , flat = True )
32
- .distinct ()
29
+ # Get spreadsheet data
30
+ spreadsheet_data = EnvironmentalJusticeRow .objects .filter (
31
+ data_source = EnvironmentalJusticeRow .DataSourceChoices .SPREADSHEET
33
32
)
34
33
35
- # Build query to get:
36
- # 1. ALL spreadsheet records
37
- # 2. ML production records where dataset isn't in spreadsheet
38
- combined_query = Q (data_source = EnvironmentalJusticeRow .DataSourceChoices .SPREADSHEET ) | Q (
39
- data_source = EnvironmentalJusticeRow .DataSourceChoices .ML_PRODUCTION , dataset__not_in = spreadsheet_datasets
40
- )
34
+ # Get ML production data excluding datasets that exist in spreadsheet
35
+ ml_production_data = EnvironmentalJusticeRow .objects .filter (
36
+ data_source = EnvironmentalJusticeRow .DataSourceChoices .ML_PRODUCTION
37
+ ).exclude (dataset__in = spreadsheet_data .values_list ("dataset" , flat = True ))
41
38
42
- return EnvironmentalJusticeRow .objects .filter (combined_query ).order_by (
43
- "dataset"
44
- ) # Optional: orders results by dataset name
39
+ # Combine the querysets and sort
40
+ return spreadsheet_data .union (ml_production_data ).order_by ("dataset" , "data_source" )
45
41
46
42
def get_queryset (self ):
47
43
"""
@@ -52,9 +48,13 @@ def get_queryset(self):
52
48
"""
53
49
data_source = self .request .query_params .get ("data_source" , "combined" )
54
50
55
- # straightfoward case: return data for specific source
56
- if data_source in EnvironmentalJusticeRow .DataSourceChoices .values :
57
- return super ().get_queryset ().filter (data_source = data_source )
51
+ # Handle the 'combined' case or no parameter case
52
+ if not data_source or data_source == "combined" :
53
+ return self .get_combined_queryset ()
54
+
55
+ # Validate specific data source
56
+ if data_source not in EnvironmentalJusticeRow .DataSourceChoices .values :
57
+ valid_choices = list (EnvironmentalJusticeRow .DataSourceChoices .values ) + ["combined" ]
58
+ raise ValidationError (f"Invalid data_source. Valid choices are: { ', ' .join (valid_choices )} " )
58
59
59
- # Handle 'combined' or no filter case
60
- return self .get_combined_queryset ()
60
+ return super ().get_queryset ().filter (data_source = data_source ).order_by ("dataset" )
0 commit comments