Skip to content

Commit

Permalink
Merge pull request #172 from Mosquito-Alert/agg_speciesdistribution
Browse files Browse the repository at this point in the history
Added aggregated stats in SpeciesDistribution
  • Loading branch information
epou authored Nov 30, 2023
2 parents 87ed2d7 + 0b3cbf2 commit a0f6721
Show file tree
Hide file tree
Showing 14 changed files with 1,530 additions and 145 deletions.
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"nested_admin",
"notifications",
"simple_history",
"django_jsonform",
]

LOCAL_APPS = [
Expand Down Expand Up @@ -170,6 +171,7 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.common.BrokenLinkEmailsMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
]

# STATIC
Expand Down
4 changes: 2 additions & 2 deletions mosquito_alert/epidemiology/managers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models import Manager
from mosquito_alert.taxa.managers import SpecieDistributionManager


class DiseaseVectorDistributionManager(Manager):
class DiseaseVectorDistributionManager(SpecieDistributionManager):
def get_queryset(self):
"""Sets the custom queryset as the default."""
return (
Expand Down
41 changes: 20 additions & 21 deletions mosquito_alert/taxa/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Any

from django.contrib import admin
from django.http.request import HttpRequest
from django.utils.translation import gettext_lazy as _
from modeltranslation.admin import TranslationAdmin
from simple_history.admin import SimpleHistoryAdmin
Expand Down Expand Up @@ -27,36 +30,32 @@ class TaxonAdmin(TreeAdmin, TranslationAdmin):
@admin.register(SpecieDistribution)
class SpecieDistributionAdmin(SimpleHistoryAdmin):
list_display = ("boundary", "taxon", "source", "status")
history_list_display = ["status"]
list_filter = (
("boundary", admin.RelatedOnlyFieldListFilter),
("taxon", admin.RelatedOnlyFieldListFilter),
"source",
"status",
)
search_fields = ("boundary__name",)
ordering = ["taxon", "boundary", "status"]

history_list_display = ["status", "stats_summary"]

fields = ("boundary", "taxon", "source", ("status", "status_since"), "pretty_stats_summary")

autocomplete_fields = ("boundary", "taxon")

def status(self, obj):
# Needed to show the displayable value.
# Needed to show the displayable value in history view.
# Not working otherwise...
return obj.get_status_display()

def get_readonly_fields(self, request: HttpRequest, obj: Any | None = ...) -> list[str] | tuple[Any, ...]:
readonly_fields = ["pretty_stats_summary", "status_since"]

class SpecieDistributionHistoryAdmin(admin.ModelAdmin):
list_display = (
"id",
"history_date",
"history_type",
"status",
)
search_fields = ("id",)
list_filter = (
"history_date",
"history_type",
"status",
)

def has_add_permission(self, request) -> bool:
return False

if obj:
readonly_fields += ["boundary", "taxon", "source"]
if obj.source == SpecieDistribution.DataSource.SELF:
# Not allowing editing status since it is auto computed.
readonly_fields += ["status"]

admin.site.register(SpecieDistribution.history.model, SpecieDistributionHistoryAdmin)
return tuple(readonly_fields)
5 changes: 5 additions & 0 deletions mosquito_alert/taxa/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.db.models import Manager

from .querysets import SpecieDistributionQuerySet

SpecieDistributionManager = Manager.from_queryset(SpecieDistributionQuerySet)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 4.2.3 on 2023-11-09 15:53

from django.db import migrations, models
import django.db.models.deletion
import django_jsonform.models.fields


class Migration(migrations.Migration):
dependencies = [
("geo", "0002_alter_boundary_created_at_alter_boundary_updated_at_and_more"),
("taxa", "0006_alter_taxon_gbif_id"),
]

operations = [
migrations.AddField(
model_name="historicalspeciedistribution",
name="stats_summary",
field=django_jsonform.models.fields.JSONField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name="speciedistribution",
name="stats_summary",
field=django_jsonform.models.fields.JSONField(blank=True, editable=False, null=True),
),
migrations.AlterField(
model_name="historicalspeciedistribution",
name="status",
field=models.PositiveSmallIntegerField(
blank=True,
choices=[(0, "Absent"), (10, "Reported"), (20, "Introduced"), (30, "Established")],
null=True,
),
),
migrations.AlterField(
model_name="speciedistribution",
name="boundary",
field=models.ForeignKey(
limit_choices_to={"numchild": 0},
on_delete=django.db.models.deletion.PROTECT,
related_name="+",
to="geo.boundary",
),
),
migrations.AlterField(
model_name="speciedistribution",
name="status",
field=models.PositiveSmallIntegerField(
blank=True,
choices=[(0, "Absent"), (10, "Reported"), (20, "Introduced"), (30, "Established")],
null=True,
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.3 on 2023-11-30 15:42

from django.db import migrations, models
import django.db.models.functions.datetime
import django.utils.timezone


class Migration(migrations.Migration):
dependencies = [
("taxa", "0007_historicalspeciedistribution_stats_summary_and_more"),
]

operations = [
migrations.AddField(
model_name="speciedistribution",
name="status_since",
field=models.DateTimeField(blank=True, default=django.utils.timezone.now, editable=False),
),
migrations.AddConstraint(
model_name="speciedistribution",
constraint=models.CheckConstraint(
check=models.Q(("status_since__lte", django.db.models.functions.datetime.Now())),
name="status_since_cannot_be_future_dated",
),
),
]
Loading

0 comments on commit a0f6721

Please sign in to comment.