Skip to content

Commit

Permalink
feat: drop postgres specific IntegerRangeField
Browse files Browse the repository at this point in the history
it's not intuitive if the upper bound is excluded so use a start/end
field combination and include start and end in the query.
  • Loading branch information
toabctl committed Feb 22, 2024
1 parent b205c74 commit 131993a
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 27 deletions.
7 changes: 5 additions & 2 deletions django-kitamanager/kitamanager/admin/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class ChildPaymentTableAdmin(admin.ModelAdmin):


class ChildPaymentTableEntryAdmin(admin.ModelAdmin):
list_display = ["table", "age", "name", "pay", "requirement"]
list_filter = ["table", "name", "age"]
list_display = ["table", "age_start", "age_end", "name", "pay", "requirement"]
list_filter = [
"table",
"name",
]
search_fields = ["name", "pay", "requirement"]
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def handle(self, *args, **options):
if value.get("comment"):
defaults_entry["comment"] = value["comment"]
table_entry, created = ChildPaymentTableEntry.objects.update_or_create(
table=table, age=entry["age"], name=key, defaults=defaults_entry
table=table,
age_start=entry["age"][0],
age_end=entry["age"][1],
name=key,
defaults=defaults_entry,
)
self.stdout.write(
self.style.SUCCESS(f"{plan}: table {table}: {key}:{value} (newly created? {created})")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Generated by Django 5.0.2 on 2024-02-22 14:12

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("kitamanager", "0003_childpaymenttable_hours"),
]

operations = [
migrations.AlterModelOptions(
name="childpaymenttableentry",
options={"ordering": ("table", "age_start", "age_end", "name")},
),
migrations.RemoveConstraint(
model_name="childpaymenttableentry",
name="kitamanager_childpaymenttableentry_table_age_name",
),
migrations.RemoveIndex(
model_name="childpaymenttableentry",
name="kitamanager_age_c3e652_idx",
),
migrations.RemoveField(
model_name="childpaymenttableentry",
name="age",
),
migrations.AddField(
model_name="childpaymenttableentry",
name="age_end",
field=models.PositiveSmallIntegerField(
default=0,
help_text="age end (in years) range for this property",
validators=[
django.core.validators.MinValueValidator(0),
django.core.validators.MaxValueValidator(8),
],
),
preserve_default=False,
),
migrations.AddField(
model_name="childpaymenttableentry",
name="age_start",
field=models.PositiveSmallIntegerField(
default=0,
help_text="age start (in years) range for this property",
validators=[
django.core.validators.MinValueValidator(0),
django.core.validators.MaxValueValidator(8),
],
),
preserve_default=False,
),
migrations.AddIndex(
model_name="childpaymenttableentry",
index=models.Index(
fields=["age_start", "age_end", "name", "pay", "requirement"],
name="kitamanager_age_sta_7bde4f_idx",
),
),
migrations.AddConstraint(
model_name="childpaymenttableentry",
constraint=models.UniqueConstraint(
fields=("table", "age_start", "age_end", "name"),
name="kitamanager_childpaymenttableentry_table_age_start_age_end_name",
),
),
]
6 changes: 4 additions & 2 deletions django-kitamanager/kitamanager/models/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def payment(self, date: datetime.date):
table__plan=self.pay_plan,
table__start__lte=date,
table__end__gt=date,
age__contains=self.person.age(date),
age_start__lte=self.person.age(date),
age_end__gte=self.person.age(date),
name__in=pay_tags_with_base,
)
return qs.aggregate(models.Sum("pay"))["pay__sum"]
Expand All @@ -139,7 +140,8 @@ def requirement(self, date: datetime.date):

qs = ChildPaymentTableEntry.objects.filter(
table=table,
age__contains=self.person.age(date),
age_start__lte=self.person.age(date),
age_end__gte=self.person.age(date),
name__in=pay_tags_with_base,
)
sum = qs.aggregate(models.Sum("requirement"))["requirement__sum"]
Expand Down
23 changes: 15 additions & 8 deletions django-kitamanager/kitamanager/models/child_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.contrib.postgres.constraints import ExclusionConstraint
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
from django.contrib.postgres.fields import IntegerRangeField
from kitamanager.models.common import DateRange


Expand Down Expand Up @@ -92,8 +91,13 @@ class ChildPaymentTableEntry(models.Model):
"""

table = models.ForeignKey("ChildPaymentTable", on_delete=models.CASCADE, related_name="entries")
age = IntegerRangeField(
help_text=_("age (in years) range for this property"), validators=[MinValueValidator(0), MaxValueValidator(8)]
age_start = models.PositiveSmallIntegerField(
help_text=_("age start (in years) range for this property"),
validators=[MinValueValidator(0), MaxValueValidator(8)],
)
age_end = models.PositiveSmallIntegerField(
help_text=_("age end (in years) range for this property"),
validators=[MinValueValidator(0), MaxValueValidator(8)],
)
name = models.CharField(max_length=255, help_text=_("property name"))
pay = models.DecimalField(
Expand All @@ -105,16 +109,19 @@ class ChildPaymentTableEntry(models.Model):
comment = models.TextField(blank=True)

class Meta:
ordering = ("table", "age", "name")
ordering = ("table", "age_start", "age_end", "name")
constraints = [
models.UniqueConstraint(
fields=["table", "age", "name"],
name="%(app_label)s_%(class)s_table_age_name",
fields=["table", "age_start", "age_end", "name"],
name="%(app_label)s_%(class)s_table_age_start_age_end_name",
)
]
indexes = [
models.Index(fields=["age", "name", "pay", "requirement"]),
models.Index(fields=["age_start", "age_end", "name", "pay", "requirement"]),
]

def __str__(self):
return f"{self.table}: age:{self.age}, name:{self.name}, pay:{self.pay}, req:{self.requirement}"
return (
f"{self.table}: age:{self.age_start}-{self.age_end}, "
f"name:{self.name}, pay:{self.pay}, req:{self.requirement}"
)
14 changes: 10 additions & 4 deletions django-kitamanager/kitamanager/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ def _childpaymentplan_create():
"""
plan1, _ = ChildPaymentPlan.objects.get_or_create(name="plan1")
table1, _ = ChildPaymentTable.objects.get_or_create(plan=plan1, start="2020-01-01", end="2022-01-01")
ChildPaymentTableEntry.objects.get_or_create(table=table1, age=[0, 2], name="ganztag", pay=200, requirement=0.1)
ChildPaymentTableEntry.objects.get_or_create(
table=table1, age=[0, 2], name="ganztag erweitert", pay=300, requirement=0.1
table=table1, age_start=0, age_end=2, name="ganztag", pay=200, requirement=0.1
)
ChildPaymentTableEntry.objects.get_or_create(
table=table1, age_start=0, age_end=2, name="ganztag erweitert", pay=300, requirement=0.1
)
# a second table (different date) with a "base" tag
table2, _ = ChildPaymentTable.objects.get_or_create(plan=plan1, start="2024-01-01", end="2024-02-01")
ChildPaymentTableEntry.objects.get_or_create(table=table2, age=[0, 8], name="ganztag", pay=300, requirement=0.2)
ChildPaymentTableEntry.objects.get_or_create(table=table2, age=[0, 8], name="base", pay=22, requirement=0.33)
ChildPaymentTableEntry.objects.get_or_create(
table=table2, age_start=0, age_end=8, name="ganztag", pay=300, requirement=0.2
)
ChildPaymentTableEntry.objects.get_or_create(
table=table2, age_start=0, age_end=8, name="base", pay=22, requirement=0.33
)
return plan1


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ def test_childcontract_sum_requirements_multiple_paymentplans():
"""
pp1 = ChildPaymentPlan.objects.create(name="plan1")
ppt1 = ChildPaymentTable.objects.create(plan=pp1, start="2020-01-01", end="2022-01-01", hours=10)
ChildPaymentTableEntry.objects.create(table=ppt1, age=[0, 2], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=ppt1, age_start=0, age_end=2, name="ganztag", pay=100, requirement=0.1)

pp2 = ChildPaymentPlan.objects.create(name="plan2")
ppt2 = ChildPaymentTable.objects.create(plan=pp2, start="2020-01-01", end="2022-01-01", hours=40)
ChildPaymentTableEntry.objects.create(table=ppt2, age=[0, 2], name="ganztag", pay=100, requirement=0.2)
ChildPaymentTableEntry.objects.create(table=ppt2, age_start=0, age_end=2, name="ganztag", pay=100, requirement=0.2)

c1 = Child.objects.create(first_name="1", last_name="11", birth_date="2020-06-29")
c1.refresh_from_db()
Expand Down
18 changes: 11 additions & 7 deletions django-kitamanager/kitamanager/tests/models/test_childpayment.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,19 @@ def test_childpaymenttableentry_create():
t22 = ChildPaymentTable.objects.create(plan=p2, start="2021-01-01", end="2021-12-31")

# same table/age/name should work in different tables
ChildPaymentTableEntry.objects.create(table=t11, age=[0, 1], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t12, age=[0, 1], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t21, age=[0, 1], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t22, age=[0, 1], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t11, age_start=0, age_end=1, name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t12, age_start=0, age_end=1, name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t21, age_start=0, age_end=1, name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t22, age_start=0, age_end=1, name="ganztag", pay=100, requirement=0.1)

# different age/name should work in the same table
ChildPaymentTableEntry.objects.create(table=t11, age=[0, 1], name="ganztag_erweitert", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t11, age=[0, 2], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(
table=t11, age_start=0, age_end=1, name="ganztag_erweitert", pay=100, requirement=0.1
)
ChildPaymentTableEntry.objects.create(table=t11, age_start=0, age_end=2, name="ganztag", pay=100, requirement=0.1)

# same table/age/name with different pay/requirement in the same table shouldn't work
with pytest.raises(IntegrityError):
ChildPaymentTableEntry.objects.create(table=t11, age=[0, 1], name="ganztag_erweitert", pay=200, requirement=0.2)
ChildPaymentTableEntry.objects.create(
table=t11, age_start=0, age_end=1, name="ganztag_erweitert", pay=200, requirement=0.2
)
2 changes: 1 addition & 1 deletion django-kitamanager/kitamanager/tests/test_child_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_childpayment_detail(admin_client):
# response with a plan
p = ChildPaymentPlan.objects.create(name="plan1")
t = ChildPaymentTable.objects.create(plan=p, start="2020-01-01", end="2020-12-31")
ChildPaymentTableEntry.objects.create(table=t, age=[0, 1], name="ganztag", pay=100, requirement=0.1)
ChildPaymentTableEntry.objects.create(table=t, age_start=0, age_end=1, name="ganztag", pay=100, requirement=0.1)

response = admin_client.get(reverse("kitamanager:childpayment-detail", args=[p.pk]))
assert response.status_code == 200
Expand Down

0 comments on commit 131993a

Please sign in to comment.