|
| 1 | +from django.core.exceptions import ValidationError |
| 2 | +from observation_portal.common.schema import ObservationPortalSchema |
1 | 3 | from django.core.cache import cache
|
2 | 4 | from django_filters.rest_framework.backends import DjangoFilterBackend
|
3 | 5 | from rest_framework.response import Response
|
|
17 | 19 | from observation_portal.common.telescope_states import ElasticSearchException, TelescopeStates
|
18 | 20 | from observation_portal.requestgroups.request_utils import get_airmasses_for_request_at_sites
|
19 | 21 | from observation_portal.requestgroups.contention import Contention, Pressure
|
20 |
| -from observation_portal.requestgroups.filters import TelescopeAvailabilityFilter, TelescopeStatesFilter |
| 22 | +from observation_portal.requestgroups.filters import LastChangedFilter, TelescopeAvailabilityFilter, TelescopeStatesFilter |
21 | 23 | from observation_portal.common.mixins import GetSerializerMixin
|
22 | 24 |
|
23 | 25 | logger = logging.getLogger(__name__)
|
@@ -176,17 +178,24 @@ def get(self, request):
|
176 | 178 | return Response(pressure.data())
|
177 | 179 |
|
178 | 180 |
|
179 |
| -class ObservationPortalLastChangedView(APIView): |
| 181 | +class ObservationPortalLastChangedView(APIView, GetSerializerMixin): |
180 | 182 | '''
|
181 | 183 | Returns the datetime of the last status of requests change or new requests addition
|
182 | 184 | '''
|
183 | 185 | permission_classes = (IsAdminUser,)
|
184 |
| - schema=AutoSchema(tags=['RequestGroups']) |
| 186 | + schema=ObservationPortalSchema(tags=['RequestGroups'], is_list_view=False) |
| 187 | + filter_class = LastChangedFilter |
| 188 | + filter_backends = (DjangoFilterBackend,) |
| 189 | + serializer_class = import_string(settings.SERIALIZERS['requestgroups']['LastChanged']) |
185 | 190 |
|
186 | 191 | def get(self, request):
|
187 | 192 | telescope_classes = request.GET.getlist('telescope_class', ['all'])
|
188 | 193 | most_recent_change_time = timezone.now() - timedelta(days=7)
|
189 | 194 | for telescope_class in telescope_classes:
|
190 | 195 | most_recent_change_time = max(most_recent_change_time, cache.get(f"observation_portal_last_change_time_{telescope_class}", timezone.now() - timedelta(days=7)))
|
191 | 196 |
|
192 |
| - return Response({'last_change_time': most_recent_change_time}) |
| 197 | + serializer = import_string(settings.SERIALIZERS['requestgroups']['LastChanged'])(data={'last_change_time': most_recent_change_time}) |
| 198 | + if serializer.is_valid(): |
| 199 | + return Response(serializer.validated_data) |
| 200 | + else: |
| 201 | + raise ValidationError(serializer.errors) |
0 commit comments