|
| 1 | +from django.db.models import Q |
1 | 2 | from django_filters.rest_framework import DjangoFilterBackend
|
2 | 3 | from rest_framework import viewsets
|
3 | 4 |
|
|
8 | 9 | class EnvironmentalJusticeRowViewSet(viewsets.ModelViewSet):
|
9 | 10 | """
|
10 | 11 | API endpoint that allows environmental justice rows to be read.
|
| 12 | + When combining spreadsheet and ml_production data, spreadsheet takes precedence |
| 13 | + for any matching dataset values. |
11 | 14 | """
|
12 | 15 |
|
13 | 16 | queryset = EnvironmentalJusticeRow.objects.all()
|
14 | 17 | serializer_class = EnvironmentalJusticeRowSerializer
|
15 | 18 | http_method_names = ["get"]
|
16 | 19 | filter_backends = [DjangoFilterBackend]
|
17 |
| - filterset_fields = ["destination_server"] |
| 20 | + filterset_fields = ["data_source"] |
| 21 | + |
| 22 | + def get_combined_queryset(self): |
| 23 | + """ |
| 24 | + Returns combined data where: |
| 25 | + 1. All spreadsheet data is included |
| 26 | + 2. ML production data is included only if there's no spreadsheet data with matching dataset |
| 27 | + """ |
| 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() |
| 33 | + ) |
| 34 | + |
| 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 | + ) |
| 41 | + |
| 42 | + return EnvironmentalJusticeRow.objects.filter(combined_query).order_by( |
| 43 | + "dataset" |
| 44 | + ) # Optional: orders results by dataset name |
18 | 45 |
|
19 | 46 | def get_queryset(self):
|
20 | 47 | """
|
21 |
| - if no destination_server is provided, default to PROD |
| 48 | + Handle different data_source filter scenarios: |
| 49 | + - No filter: Return combined data (spreadsheet takes precedence) |
| 50 | + - 'combined': Same as no filter |
| 51 | + - specific source: Return data for that source only |
22 | 52 | """
|
23 |
| - queryset = super().get_queryset() |
24 |
| - if not self.request.query_params.get("destination_server"): |
25 |
| - queryset = queryset.filter(destination_server=EnvironmentalJusticeRow.DestinationServerChoices.PROD) |
26 |
| - return queryset |
| 53 | + data_source = self.request.query_params.get("data_source", "combined") |
| 54 | + |
| 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) |
| 58 | + |
| 59 | + # Handle 'combined' or no filter case |
| 60 | + return self.get_combined_queryset() |
0 commit comments