Bug fixes

- report Export progress correctly
- move Questionnaire ecological district display to admin.py
This commit is contained in:
Matthew Northcott 2023-03-02 12:04:01 +13:00
parent 9ac545a6d4
commit 1b800ff8ef
2 changed files with 12 additions and 11 deletions

View file

@ -13,7 +13,7 @@ class ZoneAdmin(admin.ModelAdmin):
class QuestionnaireAdmin(admin.ModelAdmin): class QuestionnaireAdmin(admin.ModelAdmin):
list_display = ['address_display', 'location_display', 'soil_variant', 'ecological_district', 'habitat', 'zone'] list_display = ['address_display', 'location_display', 'soil_variant', 'ecological_district_display', 'habitat', 'zone']
actions = ['export'] actions = ['export']
@admin.display(description="Address") @admin.display(description="Address")
@ -24,6 +24,14 @@ class QuestionnaireAdmin(admin.ModelAdmin):
def location_display(self, obj): def location_display(self, obj):
return f"({obj.location.x}, {obj.location.y})" return f"({obj.location.x}, {obj.location.y})"
@admin.display(description="Ecological District")
def ecological_district_display(self, obj):
return getattr(
models.EcologicalDistrictLayer.objects.filter(geom__intersects=obj.location).first(),
"ecologic_2",
None,
)
@admin.action(description="Export planting guides for selected questionnaires") @admin.action(description="Export planting guides for selected questionnaires")
def export(self, request, queryset): def export(self, request, queryset):
export = models.Export.objects.create(creation_date=timezone.now()) export = models.Export.objects.create(creation_date=timezone.now())

View file

@ -208,14 +208,6 @@ class Questionnaire(models.Model):
def habitat(self): def habitat(self):
return self.zone.habitat return self.zone.habitat
@property
def ecological_district_layer(self):
return EcologicalDistrictLayer.objects.filter(geom__intersects=self.location).first()
@property
def ecological_district(self):
return self.ecological_district_layer.ecologic_2
@cached_property @cached_property
def address(self): def address(self):
return \ return \
@ -264,8 +256,9 @@ class Export(models.Model):
_, files = storage.listdir(f"export_{self.pk}") _, files = storage.listdir(f"export_{self.pk}")
# halved as there are two files (csv, pdf) per questionnaire # 0.25 multiplier as there are four files per questionnaire:
return 0.5 * len(files) / self.questionnaires.count() # csv, filters PDF, plants PDF, merged PDF
return 0.25 * len(files) / self.questionnaires.count()
def export(self): def export(self):
from .tasks import generate_pdf from .tasks import generate_pdf