Skip to content

Commit

Permalink
Merge branch 'working' of https://github.com/thakurpriya1990/boranga
Browse files Browse the repository at this point in the history
…into working
  • Loading branch information
thakurpriya1990 committed Feb 8, 2024
2 parents d3b1cac + 9576180 commit 24ea07f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 383 deletions.
100 changes: 69 additions & 31 deletions boranga/components/occurrence/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from rest_framework.decorators import action as list_route
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from datetime import datetime
from datetime import datetime, time
from ledger_api_client.settings_base import TIME_ZONE
from boranga import settings
from boranga import exceptions
Expand Down Expand Up @@ -118,29 +118,67 @@

class OccurrenceReportFilterBackend(DatatablesFilterBackend):
def filter_queryset(self, request, queryset, view):
total_count = queryset.count()

flora = GroupType.GROUP_TYPE_FLORA
fauna = GroupType.GROUP_TYPE_FAUNA
community = GroupType.GROUP_TYPE_COMMUNITY

filter_group_type = request.GET.get('filter_group_type')
if filter_group_type and not filter_group_type.lower() == 'all':
queryset = queryset.filter(group_type__name=filter_group_type)
if 'internal' in view.name:
total_count = queryset.count()

filter_group_type = request.GET.get('filter_group_type')
if filter_group_type and not filter_group_type.lower() == 'all':
queryset = queryset.filter(group_type__name=filter_group_type)

# To do - change group_type__name based on the relevant model
# filter_occurrence = request.GET.get('filter_occurrence')
# if filter_occurrence and not filter_occurrence.lower() == 'all':
# queryset = queryset.filter(group_type__name=filter_occurrence)

filter_scientific_name = request.GET.get('filter_scientific_name')
if filter_scientific_name and not filter_scientific_name.lower() == 'all':
queryset = queryset.filter(species__taxonomy__scientific_name=filter_scientific_name)

filter_status = request.GET.get('filter_status')
if filter_status and not filter_status.lower() == 'all':
queryset = queryset.filter(processing_status=filter_status)

def get_date(filter_date):
date = request.GET.get(filter_date)
if date:
date = datetime.strptime(date,'%Y-%m-%d')
return date

filter_submitted_from_date = get_date('filter_submitted_from_date')
filter_submitted_to_date = get_date('filter_submitted_to_date')
if filter_submitted_to_date:
filter_submitted_to_date = datetime.combine(filter_submitted_to_date , time.max)

if filter_submitted_from_date and not filter_submitted_to_date:
queryset = queryset.filter(reported_date__gte=filter_submitted_from_date)

if filter_submitted_from_date and filter_submitted_to_date:
queryset = queryset.filter(reported_date__range=[filter_submitted_from_date, filter_submitted_to_date])

# filter_scientific_name is the species_id
filter_scientific_name = request.GET.get('filter_scientific_name')
if filter_scientific_name and not filter_scientific_name.lower() == 'all':
queryset = queryset.filter(species=filter_scientific_name)

# filter_community_name is the community_id
filter_community_name = request.GET.get('filter_community_name')
if filter_community_name and not filter_community_name.lower() == 'all':
queryset = queryset.filter(community=filter_community_name)

filter_application_status = request.GET.get('filter_application_status')
if filter_application_status and not filter_application_status.lower() == 'all':
queryset = queryset.filter(customer_status=filter_application_status)
if 'external' in view.name:
total_count = queryset.count()

flora = GroupType.GROUP_TYPE_FLORA
fauna = GroupType.GROUP_TYPE_FAUNA
community = GroupType.GROUP_TYPE_COMMUNITY

filter_group_type = request.GET.get('filter_group_type')
if filter_group_type and not filter_group_type.lower() == 'all':
queryset = queryset.filter(group_type__name=filter_group_type)

# filter_scientific_name is the species_id
filter_scientific_name = request.GET.get('filter_scientific_name')
if filter_scientific_name and not filter_scientific_name.lower() == 'all':
queryset = queryset.filter(species=filter_scientific_name)

# filter_community_name is the community_id
filter_community_name = request.GET.get('filter_community_name')
if filter_community_name and not filter_community_name.lower() == 'all':
queryset = queryset.filter(community=filter_community_name)

filter_application_status = request.GET.get('filter_application_status')
if filter_application_status and not filter_application_status.lower() == 'all':
queryset = queryset.filter(customer_status=filter_application_status)

getter = request.query_params.get
fields = self.get_fields(getter)
Expand Down Expand Up @@ -200,14 +238,14 @@ def occurrence_report_external(self, request, *args, **kwargs):

@list_route(methods=['GET',], detail=False)
def occurrence_report_internal(self, request, *args, **kwargs):
qs = self.get_queryset()
qs = qs.filter(Q(internal_application=False))
qs = self.filter_queryset(qs)

self.paginator.page_size = qs.count()
result_page = self.paginator.paginate_queryset(qs, request)
serializer = ListInternalOccurrenceReportSerializer(result_page, context={'request': request}, many=True)
return self.paginator.get_paginated_response(serializer.data)
qs = self.get_queryset()
qs = qs.filter(Q(internal_application=False))
qs = self.filter_queryset(qs)

self.paginator.page_size = qs.count()
result_page = self.paginator.paginate_queryset(qs, request)
serializer = ListInternalOccurrenceReportSerializer(result_page, context={'request': request}, many=True)
return self.paginator.get_paginated_response(serializer.data)

class OccurrenceReportViewSet(viewsets.ModelViewSet):
queryset = OccurrenceReport.objects.none()
Expand Down
Loading

0 comments on commit 24ea07f

Please sign in to comment.