Skip to content

Commit

Permalink
Merge pull request #597 from Gary-Community-Ventures/dev
Browse files Browse the repository at this point in the history
Release 10/18/2024
  • Loading branch information
CalebPena authored Oct 18, 2024
2 parents e3ea200 + af4db59 commit f86ea62
Show file tree
Hide file tree
Showing 27 changed files with 692 additions and 29 deletions.
31 changes: 31 additions & 0 deletions programs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .models import (
LegalStatus,
Program,
ProgramCategory,
UrgentNeed,
Navigator,
UrgentNeedFunction,
Expand Down Expand Up @@ -301,6 +302,35 @@ def action_buttons(self, obj):
action_buttons.allow_tags = True


class ProgramCategoryAdmin(ModelAdmin):
search_fields = ("external_name",)
list_display = ["get_str", "external_name", "action_buttons"]

def get_str(self, obj):
return str(obj)

get_str.admin_order_field = "external_name"
get_str.short_description = "Name"

def action_buttons(self, obj):
return format_html(
"""
<div class="dropdown">
<span class="dropdown-btn material-symbols-outlined"> menu </span>
<div class="dropdown-content">
<a href="{}">Name</a>
<a href="{}">Description</a>
</div>
</div>
""",
reverse("translation_admin_url", args=[obj.name.id]),
reverse("translation_admin_url", args=[obj.description.id]),
)

action_buttons.short_description = "Translate:"
action_buttons.allow_tags = True


admin.site.register(LegalStatus, LegalStatusAdmin)
admin.site.register(Program, ProgramAdmin)
admin.site.register(County, CountiesAdmin)
Expand All @@ -315,3 +345,4 @@ def action_buttons(self, obj):
admin.site.register(Referrer, ReferrerAdmin)
admin.site.register(WebHookFunction, WebHookFunctionsAdmin)
admin.site.register(TranslationOverride, TranslationOverrideAdmin)
admin.site.register(ProgramCategory, ProgramCategoryAdmin)
50 changes: 50 additions & 0 deletions programs/migrations/0090_programcategory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 4.2.15 on 2024-10-09 18:49

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


class Migration(migrations.Migration):

dependencies = [
("translations", "0004_translation_no_auto"),
("programs", "0089_alter_translationoverride_counties_and_more"),
]

operations = [
migrations.CreateModel(
name="ProgramCategory",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"external_name",
models.CharField(blank=True, max_length=120, null=True, unique=True),
),
("calculator", models.CharField(blank=True, max_length=120, null=True)),
(
"description",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="program_category_description",
to="translations.translation",
),
),
(
"name",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="program_category_name",
to="translations.translation",
),
),
],
),
]
25 changes: 25 additions & 0 deletions programs/migrations/0091_program_category_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.15 on 2024-10-09 18:53

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


class Migration(migrations.Migration):

dependencies = [
("programs", "0090_programcategory"),
]

operations = [
migrations.AddField(
model_name="program",
name="category_v2",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="programs",
to="programs.programcategory",
),
),
]
19 changes: 19 additions & 0 deletions programs/migrations/0092_programcategory_icon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.15 on 2024-10-09 20:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("programs", "0091_program_category_v2"),
]

operations = [
migrations.AddField(
model_name="programcategory",
name="icon",
field=models.CharField(default="housing", max_length=120),
preserve_default=False,
),
]
79 changes: 76 additions & 3 deletions programs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from programs.util import Dependencies
import requests
from integrations.util.cache import Cache
from typing import Optional, Type, TypedDict
from typing import Optional, TypedDict
from programs.programs.translation_overrides import warning_calculators


Expand Down Expand Up @@ -97,6 +97,70 @@ def __str__(self):
return self.status


class ProgramCategoryManager(models.Manager):
translated_fields = ("name", "description")

def new_program_category(self, external_name: str, icon: str):
translations = {}
for field in self.translated_fields:
translations[field] = Translation.objects.add_translation(
f"program_category.{external_name}_temporary_key-{field}"
)

program_category = self.create(external_name=external_name, icon=icon, **translations)

for [field, translation] in translations.items():
translation.label = f"program_category.{external_name}_{program_category.id}-{field}"
translation.save()

return program_category


class ProgramCategoryDataController(ModelDataController["ProgramCategory"]):
_model_name = "ProgramCategory"

DataType = TypedDict(
"DataType",
{
"calculator": str,
"icon": str,
},
)

def to_model_data(self) -> DataType:
program_category = self.instance
return {"calculator": program_category.calculator, "icon": program_category.icon}

def from_model_data(self, data: DataType):
program_category = self.instance

program_category.calculator = data["calculator"]
program_category.icon = data["icon"]

@classmethod
def create_instance(cls, external_name: str, Model: type["ProgramCategory"]) -> "ProgramCategory":
return Model.objects.new_program_category(external_name, "housing")


class ProgramCategory(models.Model):
external_name = models.CharField(max_length=120, blank=True, null=True, unique=True)
calculator = models.CharField(max_length=120, blank=True, null=True)
icon = models.CharField(max_length=120, blank=False, null=False)
name = models.ForeignKey(
Translation, related_name="program_category_name", blank=False, null=False, on_delete=models.PROTECT
)
description = models.ForeignKey(
Translation, related_name="program_category_description", blank=False, null=False, on_delete=models.PROTECT
)

objects = ProgramCategoryManager()

TranslationExportBuilder = ProgramCategoryDataController

def __str__(self):
return self.name.text


class DocumentManager(models.Manager):
translated_fields = ("text",)

Expand Down Expand Up @@ -128,7 +192,7 @@ class Document(models.Model):
TranslationExportBuilder = DocumentDataController

def __str__(self) -> str:
return self.external_name
return self.text.text


class ProgramManager(models.Manager):
Expand Down Expand Up @@ -174,7 +238,7 @@ def new_program(self, name_abbreviated):

class ProgramDataController(ModelDataController["Program"]):
_model_name = "Program"
dependencies = ["Document"]
dependencies = ["Document", "ProgramCategory"]

FplDataType = TypedDict("FplDataType", {"year": str, "period": str})
LegalStatusesDataType = list[TypedDict("LegalStatusDataType", {"status": str})]
Expand All @@ -187,6 +251,7 @@ class ProgramDataController(ModelDataController["Program"]):
"active": bool,
"low_confidence": bool,
"documents": list[str],
"category": str,
},
)

Expand All @@ -207,6 +272,7 @@ def to_model_data(self) -> DataType:
"low_confidence": program.low_confidence,
"name_abbreviated": program.name_abbreviated,
"documents": [d.external_name for d in program.documents.all()],
"category": program.category_v2.external_name,
}

def from_model_data(self, data: DataType):
Expand Down Expand Up @@ -248,6 +314,10 @@ def from_model_data(self, data: DataType):
documents.append(doc)
program.documents.set(documents)

# get program category
program_category = ProgramCategory.objects.get(external_name=data["category"])
program.category_v2 = program_category

program.save()

@classmethod
Expand All @@ -266,6 +336,9 @@ class Program(models.Model):
active = models.BooleanField(blank=True, default=True)
low_confidence = models.BooleanField(blank=True, null=False, default=False)
fpl = models.ForeignKey(FederalPoveryLimit, related_name="fpl", blank=True, null=True, on_delete=models.SET_NULL)
category_v2 = models.ForeignKey(
ProgramCategory, related_name="programs", blank=True, null=True, on_delete=models.SET_NULL
)

description_short = models.ForeignKey(
Translation, related_name="program_description_short", blank=False, null=False, on_delete=models.PROTECT
Expand Down
2 changes: 1 addition & 1 deletion programs/programs/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MemberEligibility:
def __init__(self, member: HouseholdMember) -> None:
self.member = member
self.eligible = True
self.value = 0
self.value: int = 0

def condition(self, passed: bool):
"""
Expand Down
7 changes: 7 additions & 0 deletions programs/programs/categories/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .base import ProgramCategoryCapCalculator
from .co import co_category_cap_calculators

category_cap_calculators: dict[str, type[ProgramCategoryCapCalculator]] = {
"no_cap": ProgramCategoryCapCalculator,
**co_category_cap_calculators,
}
Loading

0 comments on commit f86ea62

Please sign in to comment.