Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new ExtendedChoiceField and map ai_is_trustworthy "Yes"/"No" values to "CD" #12976

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="it-uses d-flex flex-column">
<h4 class="pni-product-smaller-body-bold">{{ title }}</h4>
<div class="explanation">
<p class="pni-product-smaller-body mb-0">{% trans "Device:" %} <span class="font-italic">{{ device|extended_yes_no }}</span></p>
<p class="pni-product-smaller-body mb-0">{% trans "App:" %} <span class="font-italic">{{ app|extended_yes_no }}</span></p>
<p class="pni-product-smaller-body mb-0">{% trans "Device:" %} <span class="font-italic">{{ device|extended_choice }}</span></p>
<p class="pni-product-smaller-body mb-0">{% trans "App:" %} <span class="font-italic">{{ app|extended_choice }}</span></p>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h3 class="tw-mb-8 tw-font-zilla tw-text-2xl tw-leading-7 tw-flex tw-items-cente
</div>
{% else %}
<p class="rating pni-product-smaller-body mb-0">
{% trans value|extended_yes_no context value|extended_yes_no %}
{% trans value|extended_choice context value|extended_choice %}
</p>
{% endif %}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<div class="signup-requirement d-flex flex-column">
<h4 class="pni-product-smaller-body-bold">{{ title }}</h4>
<div class="explanation">
<p class="pni-product-smaller-body mb-0">{{ value | extended_yes_no }}</p>
<p class="pni-product-smaller-body mb-0">{{ value | extended_choice }}</p>
</div>
</div>
28 changes: 28 additions & 0 deletions network-api/networkapi/wagtailpages/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,31 @@ def deconstruct(self):
del kwargs["default"]
del kwargs["max_length"]
return name, path, args, kwargs


class ExtendedChoiceField(models.CharField):
description = "Good, Bad, Average, Needs Improvement, Not Applicable, or Can’t Determine"

choice_list = [
("Good", "Good"),
("Bad", "Bad"),
("AVG", "Average"),
("NI", "Needs Improvement"),
("NA", "Not Applicable"),
("CD", "Can’t Determine"),
]

default_choice = "CD"

def __init__(self, *args, **kwargs):
kwargs["choices"] = self.choice_list
kwargs["default"] = self.default_choice
kwargs["max_length"] = 4
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
del kwargs["choices"]
del kwargs["default"]
del kwargs["max_length"]
return name, path, args, kwargs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.15 on 2024-10-07 21:41

from django.db import migrations

import networkapi.wagtailpages.fields


def map_yes_no_to_cant_determine(apps, schema_editor):
GeneralProductPage = apps.get_model("wagtailpages", "GeneralProductPage")

# Filter all instances where 'ai_is_untrustworthy' is 'Yes' or 'No'
pages_to_update = GeneralProductPage.objects.filter(ai_is_untrustworthy__in=["Yes", "No"])

# Iterate over the pages and update them
for page in pages_to_update:
print(
f"Product '{page.title}' with ID {page.id} has 'ai_is_untrustworthy' set as '{page.ai_is_untrustworthy}'. Updating to 'CD'."
)
page.ai_is_untrustworthy = "CD"
page.save()


class Migration(migrations.Migration):

dependencies = [
("wagtailpages", "0166_homepagehighlights_replace_homepagenewsyoucanuse"),
]

operations = [
migrations.AlterField(
model_name="generalproductpage",
name="ai_is_untrustworthy",
field=networkapi.wagtailpages.fields.ExtendedChoiceField(verbose_name="is this AI untrustworthy?"),
),
migrations.RunPython(map_yes_no_to_cant_determine),
]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from wagtail_localize.fields import SynchronizedField, TranslatableField

from networkapi.utility import orderables
from networkapi.wagtailpages.fields import ExtendedYesNoField
from networkapi.wagtailpages.fields import ExtendedChoiceField, ExtendedYesNoField
from networkapi.wagtailpages.pagemodels.base import BasePage
from networkapi.wagtailpages.pagemodels.buyersguide.categories import (
BuyersGuideProductCategory,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ class GeneralProductPage(ProductPage):
help_text="Helpful text around AI to show on the product page",
blank=True,
)
ai_is_untrustworthy = ExtendedYesNoField(
ai_is_untrustworthy = ExtendedChoiceField(
verbose_name="is this AI untrustworthy?",
)
ai_is_untrustworthy_ding = models.BooleanField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ def yes_no(value):


@register.filter
def extended_yes_no(value):
"""Converts quad-state to human readable string"""
def extended_choice(value):
Copy link
Collaborator Author

@danielfmiranda danielfmiranda Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this existing tag instead of creating a new one, because almost all of the GeneralProductPage's fields use the template "product_criterion_primary_info.html", which uses this tag, to render its selection.

"""Converts key from ExtendedChoiceField/ExtendedYesNoField to human readable string"""
if value == "Good":
return gettext("Good")
if value == "AVG":
return gettext("Average")
if value == "NI":
return gettext("Needs Improvement")
if value == "Bad":
return gettext("Bad")
if value == "CD":
return gettext("Can’t Determine")
if value == "NA":
Expand Down
Loading