Skip to content

Commit

Permalink
Update and improve csv generator (#4143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput authored Aug 13, 2024
1 parent 81e31b2 commit 0a47b4a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
10 changes: 8 additions & 2 deletions bims/api_views/checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
from django.conf import settings
from django.http import Http404
from django.template.loader import render_to_string
from preferences import preferences
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4, landscape
Expand All @@ -14,6 +13,7 @@
from rest_framework.views import APIView

from bims.api_views.search import CollectionSearch
from bims.enums import TaxonomicRank
from bims.models.taxonomy import Taxonomy
from bims.utils.domain import get_current_domain
from bims.models.download_request import DownloadRequest
Expand Down Expand Up @@ -140,7 +140,13 @@ def generate_pdf_checklist(download_request, module_name, collection_records, ba
unique_taxonomy_ids = set(record_taxonomy_ids) - written_taxa_ids

if unique_taxonomy_ids:
taxa = Taxonomy.objects.filter(id__in=unique_taxonomy_ids)
taxa = Taxonomy.objects.filter(
id__in=unique_taxonomy_ids
).filter(
rank__in=[
TaxonomicRank.SPECIES.name,
TaxonomicRank.SUBSPECIES.name]
)
taxon_serializer = ChecklistPDFSerializer(taxa, many=True)
for taxon in taxon_serializer.data:
written_taxa_ids.add(taxon['id'])
Expand Down
4 changes: 2 additions & 2 deletions bims/api_views/location_site_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def biodiversity_data(self):
collection_results = collection_results.select_related(
'taxonomy', 'taxonomy__endemism', 'taxonomy__iucn_status', 'site_visit'
)
location_site_ids = set()
taxonomy_ids = set()
for group in groups:
location_site_ids = set()
taxonomy_ids = set()
group_data = {}
try:
group_data[self.GROUP_ICON] = get_thumbnail(
Expand Down
15 changes: 11 additions & 4 deletions bims/serializers/checklist_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ def get_common_name(self, obj: Taxonomy):
if len(vernacular_names) == 0:
return ''
else:
return ', '.join(
list(
set([name.capitalize() for name in vernacular_names]))
)
return vernacular_names[0]

def get_threat_status(self, obj: Taxonomy):
if obj.iucn_status:
Expand Down Expand Up @@ -232,6 +229,15 @@ def get_sources(self, obj: Taxonomy):

# TODO
def get_confidence(self, obj: Taxonomy):
bio = self.get_bio_data(obj)
if not bio.exists():
return ''
if bio.exclude(certainty_of_identification='').exists():
return (
bio.exclude(
certainty_of_identification=''
).first().certainty_of_identification
)
return ''

def get_park_or_mpa_name(self, obj: Taxonomy):
Expand Down Expand Up @@ -303,6 +309,7 @@ class Meta:
'family',
'scientific_name',
'synonyms',
'rank',
'common_name',
'most_recent_record',
'origin',
Expand Down
2 changes: 1 addition & 1 deletion bims/tasks/email_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def send_csv_via_email(
zip_folder = os.path.join(
settings.MEDIA_ROOT, settings.PROCESSED_CSV_PATH, user.username)
if not os.path.exists(zip_folder):
os.mkdir(zip_folder)
os.makedirs(zip_folder)
zip_file = os.path.join(zip_folder, '{}.zip'.format(file_name))
with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.write(download_file_path, f'{file_name}.{extension}')
Expand Down

0 comments on commit 0a47b4a

Please sign in to comment.