Skip to content

Commit

Permalink
seperated backend filters for external and internal
Browse files Browse the repository at this point in the history
  • Loading branch information
Shruti Hindocha committed Feb 7, 2024
1 parent 47f6013 commit 7b2cf67
Showing 1 changed file with 60 additions and 34 deletions.
94 changes: 60 additions & 34 deletions boranga/components/occurrence/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,41 +111,67 @@

class OccurrenceReportFilterBackend(DatatablesFilterBackend):
def filter_queryset(self, request, queryset, view):
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 '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])

if filter_submitted_from_date and filter_submitted_to_date:
queryset = queryset.filter(reported_date__range=[filter_submitted_from_date, filter_submitted_to_date])
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

0 comments on commit 7b2cf67

Please sign in to comment.