Skip to content

Commit

Permalink
show date ranges in letter admin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukavdplas committed Feb 1, 2024
1 parent 8b28a9b commit bc92f9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions backend/letter/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LetterAddresseesAdmin(admin.StackedInline):

@admin.register(models.Letter)
class LetterAdmin(admin.ModelAdmin):
readonly_fields = ["date_active", "date_written"]
inlines = [
LetterCategoryAdmin,
LetterMaterialAdmin,
Expand Down
17 changes: 11 additions & 6 deletions backend/letter/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.contrib import admin
from core.models import Field
from person.models import Person

Expand All @@ -14,15 +15,19 @@ class Letter(models.Model):
def __str__(self):
return self.name

def date_written(self):
"""Date range in which the letter was written"""
return self._aggregate_dates(self.events.filter(categories__value="write"))

@admin.display(
description="Date range of actions involving this letter",
)
def date_active(self):
"""Date range in which anything happened with the letter"""
return self._aggregate_dates(self.events.all())

def _aggregate_dates(actions):
@admin.display(
description="Date range in which this letter was written",
)
def date_written(self):
return self._aggregate_dates(self.events.filter(categories__value="write"))

def _aggregate_dates(self, actions):
"""Calculate a date range based on the dates of related actions"""
dates = [action.date for action in actions]
lower = min(date.year_lower for date in dates)
Expand Down

0 comments on commit bc92f9f

Please sign in to comment.