Skip to content

Commit

Permalink
feat: create fetch sites data api view (#1050)
Browse files Browse the repository at this point in the history
* feat: create fetch sites data api view

* feat: add url to expose sites view

* feat: add tests for sites api

* patch: handle errors gracefully

* patch: remove the try and catch
  • Loading branch information
tinashechiraya authored Jun 24, 2024
1 parent 90196d5 commit dbd2a62
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 214 deletions.
20 changes: 20 additions & 0 deletions django_project/monitor/site_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib.gis.measure import D
from rest_framework.parsers import MultiPartParser, FormParser, JSONParser
from minisass.models import GroupScores
from django.utils.dateparse import parse_date


from monitor.serializers import (
Expand Down Expand Up @@ -191,3 +192,22 @@ def get(self, request, latitude, longitude):
return Response([], status=status.HTTP_404_NOT_FOUND)
except Sites.DoesNotExist:
return Response([], status=status.HTTP_404_NOT_FOUND)


class SitesWithObservationsView(APIView):
def get(self, request):
start_date_str = request.query_params.get('start_date', None)
start_date = parse_date(start_date_str) if start_date_str else None

if start_date_str and not start_date:
return Response({"error": "Invalid date format. Please use YYYY-MM-DD."}, status=status.HTTP_400_BAD_REQUEST)

if start_date:
observations = Observations.objects.filter(obs_date__gte=start_date)
site_ids = observations.values_list('site_id', flat=True).distinct()
sites = Sites.objects.filter(gid__in=site_ids)
else:
sites = Sites.objects.all()

serializer = SitesWithObservationsSerializer(sites, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
Loading

0 comments on commit dbd2a62

Please sign in to comment.