Skip to content

Commit

Permalink
Add fields to the observations endpoint required by the frontend, add…
Browse files Browse the repository at this point in the history
… an observations/filters endpoint that returns the fields that observations can befiltered on, as well as the options for choice fields
  • Loading branch information
eheinrich committed Aug 31, 2020
1 parent 0519203 commit 1f7e831
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion observation_portal/observations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ class ObservationSerializer(serializers.ModelSerializer):

class Meta:
model = Observation
fields = ('site', 'enclosure', 'telescope', 'start', 'end', 'priority', 'configuration_statuses', 'request')
fields = ('site', 'enclosure', 'telescope', 'start', 'end', 'priority', 'configuration_statuses', 'request', 'state', 'modified', 'created')
read_only_fields = ('state', 'modified', 'created')

def validate(self, data):
user = self.context['request'].user
Expand Down
13 changes: 13 additions & 0 deletions observation_portal/observations/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ class ObservationViewSet(CreateListModelMixin, ListAsDictMixin, viewsets.ModelVi
def get_queryset(self):
return observations_queryset(self.request).prefetch_related('request__windows', 'request__location').distinct()

@action(detail=False, methods=['get'])
def filters(self, request):
obs_filter_options = {'fields': [], 'choice_fields': []}
for filter_name, filter_field in self.filter_class.get_filters().items():
if hasattr(filter_field.field, 'choices'):
obs_filter_options['choice_fields'].append({
'name': filter_name,
'options': list(filter_field.field.choices)
})
else:
obs_filter_options['fields'].append(filter_name)
return Response(obs_filter_options, status=200)

@action(detail=False, methods=['post'])
def cancel(self, request):
"""
Expand Down

0 comments on commit 1f7e831

Please sign in to comment.