Skip to content

Commit

Permalink
Merge pull request #953 from kartoza/download-action-on-sites
Browse files Browse the repository at this point in the history
feat: add download action to sites
  • Loading branch information
tinashechiraya authored Mar 5, 2024
2 parents d186d8d + a719fcc commit e2544a3
Showing 1 changed file with 162 additions and 6 deletions.
168 changes: 162 additions & 6 deletions django_project/monitor/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
import csv
from django.utils.encoding import smart_str
from django.http import HttpResponse
from minisass_authentication.models import UserProfile

from monitor.forms import ObservationPestImageForm
from .models import (
Expand Down Expand Up @@ -36,6 +38,7 @@ class ObservationPestImageInline(admin.TabularInline):

@admin.register(Observations)
class ObservationsAdmin(admin.ModelAdmin):
list_max_show_all = 1000
list_display = (
'gid',
'user',
Expand Down Expand Up @@ -76,14 +79,96 @@ def download_records(self, request, queryset):
response['Content-Disposition'] = 'attachment; filename="observations.csv"'

writer = csv.writer(response)
# testing TODO add all rows
writer.writerow(['obs_date'])
writer.writerow(
[
smart_str("User name"),
smart_str("User Organization"),
smart_str("User Country"),
smart_str("User Expert Status"),
smart_str("Obs Date"),
smart_str("Site name"),
smart_str("River name"),
smart_str("River category"),
smart_str("Latitude"),
smart_str("Longitude"),
smart_str("Flatworms"),
smart_str("Worms"),
smart_str("Leeches"),
smart_str("Crabs/shrimps"),
smart_str("Stoneflies"),
smart_str("Minnow mayflies"),
smart_str("Other mayflies"),
smart_str("Damselflies"),
smart_str("Dragonflies"),
smart_str("Bugs/beetles"),
smart_str("Caddisflies"),
smart_str("True flies"),
smart_str("Snails"),
smart_str("Score"),
smart_str("Status"),
smart_str("Water clarity"),
smart_str("Water temp"),
smart_str("pH"),
smart_str("Diss oxygen"),
smart_str("diss oxygen unit"),
smart_str("Elec cond"),
smart_str("Elec cond unit"),
smart_str("Comment")
])

for obj in queryset:
writer.writerow([obj.obs_date])
for obs in queryset:
if obs['flag'] == 'clean':
flag = 'Verified'
else:
flag = 'Unverified'
try:
user_profile = obs.user.userprofile
user_organization_name = user_profile.organisation_name
user_country = user_profile.country
user_is_expert = userprofile.is_expert
except (UserProfile.DoesNotExist, AttributeError):
user_organization_name = "N/A"
user_country = "N/A"
user_is_expert = False
writer.writerow(
[
smart_str(obs.user.username),
smart_str(user_organization_name),
smart_str(user_country),
smart_str(user_is_expert),
smart_str(obs.obs_date),
smart_str(obs.site.site_name),
smart_str(obs.site.river_name),
smart_str(obs.site.river_cat),
smart_str(obs.site.the_geom.y),
smart_str(obs.site.the_geom.x),
smart_str(obs.flatworms),
smart_str(obs.worms),
smart_str(obs.leeches),
smart_str(obs.crabs_shrimps),
smart_str(obs.stoneflies),
smart_str(obs.minnow_mayflies),
smart_str(obs.other_mayflies),
smart_str(obs.damselflies),
smart_str(obs.dragonflies),
smart_str(obs.bugs_beetles),
smart_str(obs.caddisflies),
smart_str(obs.true_flies),
smart_str(obs.snails),
smart_str(obs.score),
smart_str(flag),
smart_str(obs.water_clarity),
smart_str(obs.water_temp),
smart_str(obs.ph),
smart_str(obs.diss_oxygen),
smart_str(obs.diss_oxygen_unit),
smart_str(obs.elec_cond),
smart_str(obs.elec_cond_unit),
smart_str(obs.comment)
])

return response
download_records.short_description = "Download selected records"
download_records.short_description = "Download selected observations"


class SiteImageInline(admin.TabularInline):
Expand All @@ -95,13 +180,84 @@ class SitesAdmin(admin.ModelAdmin):
list_max_show_all = 1000
list_display = (
'site_name',
'user',
'river_name',
'user',
'user_organization_name',
'user_country',
'user_is_expert',
'time_stamp',
)

def user_organization_name(self, obj):
user_profile = obj.user.userprofile if hasattr(obj.user, 'userprofile') else None
return user_profile.organisation_name if user_profile else None

def user_country(self, obj):
user_profile = obj.user.userprofile if hasattr(obj.user, 'userprofile') else None
return user_profile.country if user_profile else None

def user_is_expert(self, obj):
user_profile = obj.user.userprofile if hasattr(obj.user, 'userprofile') else None
return user_profile.is_expert if user_profile else None

user_organization_name.short_description = 'User Organization Name'
user_country.short_description = 'User Country'
user_is_expert.short_description = 'Is Expert'
list_filter = ('river_cat', )
search_fields = ('site_name', 'river_name')
inlines = (SiteImageInline,)


def download_selected_sites(self, request, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="selected_sites.csv"'

writer = csv.writer(response)
writer.writerow(
[
'Site Name',
'River Name',
'Description',
'River Category',
'Site Location',
'Created By',
'User Organization Name',
'User Is Expert Status'
'User Country',
'Time Stamp'
])

for site in queryset:
try:
user_profile = site.user.userprofile
user_organization_name = user_profile.organisation_name
user_country = user_profile.country
user_is_expert = user_profile.is_expert
except (UserProfile.DoesNotExist, AttributeError):
user_organization_name = "N/A"
user_country = "N/A"
user_is_expert = False
writer.writerow(
[
smart_str(site.site_name),
smart_str(site.river_name),
smart_str(site.description),
smart_str(site.river_cat),
smart_str(f"Longitude: {site.the_geom.x}, Latitude: {site.the_geom.y}"),
smart_str(site.user.email),
smart_str(user_organization_name),
smart_str(user_country),
smart_str(user_is_expert),
smart_str(site.time_stamp)
]
)

return response

download_selected_sites.short_description = "Download Selected Sites"

actions = ['download_selected_sites']


admin.site.register(ObservationPestImage)
admin.site.register(Assessment, admin.ModelAdmin)

0 comments on commit e2544a3

Please sign in to comment.