Skip to content

Commit

Permalink
Merge pull request #3 from CodeForAfrica/feat/value_display_format
Browse files Browse the repository at this point in the history
Separate denominator from display format.
  • Loading branch information
kilemensi committed May 27, 2022
2 parents e2030bc + 7c5ccad commit 97c106a
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Local development is normally done inside docker-compose so that the supporting
Make docker-compose start the supporting services

docker-compose run --rm web python wait_for_postgres.py
docker-compose run --rm web python manage.py collectstatic --no-input

Migrate, load development data and start the app

Expand Down
6 changes: 4 additions & 2 deletions tests/profile/admin_history/test_highlights_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ def test_history_for_highlight_edit_from_admin(
"subindicator": profile_highlight.subindicator,
"denominator": "absolute_value",
"label": "new label",
"display_format": "absolute_value",
"change_reason": "Changed Label",
}

res = client.post(url, data, follow=True)
assert res.status_code == 200

assert profile_highlight.history.all().count() == 2
assert profile_highlight.history.count() == 2
history = profile_highlight.history.first()
assert history.history_user_id == superuser.id
changed_data = json.loads(history.history_change_reason)
assert changed_data["reason"] == "Changed Label"
assert "label" in changed_data["changed_fields"]
assert "display_format" in changed_data["changed_fields"]
assert "denominator" in changed_data["changed_fields"]
assert history.history_type == "~"
assert history.history_type == "~"
4 changes: 3 additions & 1 deletion tests/profile/admin_history/test_key_metrics_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ def test_history_for_key_metrics_edit_from_admin(
"variable_variable_type": "public",
"denominator": "absolute_value",
"label": "new label",
"display_format": "absolute_value",
"change_reason": "Changed Label",
}

res = client.post(url, data, follow=True)
assert res.status_code == 200

assert profile_key_metric.history.all().count() == 2
assert profile_key_metric.history.count() == 2
history = profile_key_metric.history.first()
assert history.history_user_id == superuser.id
changed_data = json.loads(history.history_change_reason)
assert changed_data["reason"] == "Changed Label"
assert "label" in changed_data["changed_fields"]
assert "display_format" in changed_data["changed_fields"]
assert "denominator" in changed_data["changed_fields"]
assert history.history_type == "~"
21 changes: 21 additions & 0 deletions tests/profile/serializers/test_highlights_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ def profile_highlight_without_data(profile, indicator):
profile=profile, indicator=indicator, subindicator=FEMALE_GROUP_INDEX
)

@pytest.fixture
def profile_highlight_with_decimal_display_format(profile, indicatordata):
FEMALE_GROUP_INDEX = 1
indicator = indicatordata[0].indicator
return ProfileHighlightFactory(
profile=profile, indicator=indicator, subindicator=FEMALE_GROUP_INDEX,
display_format='decimal',
);

@pytest.mark.django_db
def test_absolute_value(profile_highlight, indicatordata_json, geography, version):
expected_value = sum(el["count"] for el in indicatordata_json if el["gender"] == "female")
Expand Down Expand Up @@ -61,3 +70,15 @@ def test_sibling_without_indicatordata(profile_highlight_without_data, version,
expected_value = None
actual_value = sibling(profile_highlight_without_data, geography, version)
assert expected_value == actual_value

@pytest.mark.django_db
def test_data_format(profile_highlight):
expected_value = 'percentage'
actual_value = profile_highlight.display_format;
assert expected_value == actual_value

@pytest.mark.django_db
def test_data_format_with_decimal_display_format(profile_highlight_with_decimal_display_format):
expected_value = 'decimal'
actual_value = profile_highlight_with_decimal_display_format.display_format;
assert expected_value == actual_value
22 changes: 22 additions & 0 deletions tests/profile/serializers/test_metrics_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from wazimap_ng.profile.serializers.metrics_serializer import absolute_value, subindicator, sibling

from tests.datasets.factories import GeographyFactory, IndicatorDataFactory
from tests.profile.factories import ProfileKeyMetricsFactory

@pytest.fixture
def additional_data_json():
Expand All @@ -21,6 +22,15 @@ def additional_data_json():
]
]

@pytest.fixture
def profile_key_metric_with_decimal_display_format(profile, indicatordata, subcategory):
FEMALE_GROUP_INDEX = 1
indicator = indicatordata[0].indicator
return ProfileKeyMetricsFactory(
profile=profile, variable=indicator, subindicator=FEMALE_GROUP_INDEX,
subcategory=subcategory, display_format="decimal",
)

@pytest.mark.django_db
class TestAbsoluteValue:
def test_absolute_value(self, profile_key_metric, indicatordata_json, geography):
Expand Down Expand Up @@ -169,3 +179,15 @@ def test_sibling_not_none(profile_key_metric, geography, other_geographies, vers
with patch.object(geography, "get_version_siblings", side_effect=lambda _version: other_geographies):
sibling_data = sibling(profile_key_metric, geography, version)
assert sibling_data != None

@pytest.mark.django_db
def test_data_format(profile_highlight):
expected_value = 'percentage'
actual_value = profile_highlight.display_format;
assert expected_value == actual_value

@pytest.mark.django_db
def test_data_format_with_decimal_display_format(profile_key_metric_with_decimal_display_format):
expected_value = 'decimal'
actual_value = profile_key_metric_with_decimal_display_format.display_format;
assert expected_value == actual_value
6 changes: 6 additions & 0 deletions wazimap_ng/config/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ def get_env_value(env_variable):
('sibling', 'Sibling'),
)

DISPLAY_FORMAT_CHOICES = (
('absolute_value', 'Integer'),
('decimal', 'Decimal'),
('percentage', 'Percentage'),
)

PERMISSION_TYPES = (
('private', 'Private'),
('public', 'Public'),
Expand Down
2 changes: 1 addition & 1 deletion wazimap_ng/profile/admin/admins/profile_highlight_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProfileHighlightAdmin(SortableAdminMixin, BaseAdminModel, HistoryAdmin):
"fields": ("profile", "indicator")
}),
("Profile fields", {
"fields": ("label", "subindicator", "denominator")
"fields": ("label", "subindicator", "denominator", "display_format")
})
)
form = ProfileHighlightForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ProfileKeyMetricsAdmin(SortableAdminMixin, BaseAdminModel, HistoryAdmin):

help_texts = ["denominator", ]

fields = ["profile", "variable", "subindicator", "subcategory", "denominator", "label"]
fields = ["profile", "variable", "subindicator", "subcategory", "denominator", "label", "display_format"]
search_fields = ("label", )


Expand Down
33 changes: 33 additions & 0 deletions wazimap_ng/profile/migrations/0053_value_display_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.2.24 on 2022-05-27 06:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profile', '0052_auto_20220111_1315'),
]

operations = [
migrations.AddField(
model_name='historicalprofilehighlight',
name='display_format',
field=models.CharField(choices=[('absolute_value', 'Integer'), ('decimal', 'Decimal'), ('percentage', 'Percentage')], default='percentage', help_text='Method for displaying the value to the user.', max_length=32),
),
migrations.AddField(
model_name='historicalprofilekeymetrics',
name='display_format',
field=models.CharField(choices=[('absolute_value', 'Integer'), ('decimal', 'Decimal'), ('percentage', 'Percentage')], default='percentage', help_text='Method for displaying the value to the user.', max_length=32),
),
migrations.AddField(
model_name='profilehighlight',
name='display_format',
field=models.CharField(choices=[('absolute_value', 'Integer'), ('decimal', 'Decimal'), ('percentage', 'Percentage')], default='percentage', help_text='Method for displaying the value to the user.', max_length=32),
),
migrations.AddField(
model_name='profilekeymetrics',
name='display_format',
field=models.CharField(choices=[('absolute_value', 'Integer'), ('decimal', 'Decimal'), ('percentage', 'Percentage')], default='percentage', help_text='Method for displaying the value to the user.', max_length=32),
),
]
4 changes: 3 additions & 1 deletion wazimap_ng/profile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from wazimap_ng.datasets.models import Indicator, GeographyHierarchy
from wazimap_ng.general.models import BaseModel, SimpleHistory
from wazimap_ng.config.common import (
DENOMINATOR_CHOICES, PERMISSION_TYPES, PI_CONTENT_TYPE
DENOMINATOR_CHOICES, DISPLAY_FORMAT_CHOICES, PERMISSION_TYPES, PI_CONTENT_TYPE
)

class Profile(BaseModel, SimpleHistory):
Expand Down Expand Up @@ -76,6 +76,7 @@ class ProfileKeyMetrics(BaseModel, SimpleHistory):
subindicator = models.PositiveSmallIntegerField()
denominator = models.CharField(choices=DENOMINATOR_CHOICES, max_length=32, help_text="Method for calculating the denominator that will normalise this value.")
label = models.CharField(max_length=255, help_text="Text used for display to users.")
display_format = models.CharField(choices=DISPLAY_FORMAT_CHOICES, default="percentage", max_length=32, help_text="Method for displaying the value to the user.")
order = models.PositiveIntegerField(default=0, blank=False, null=False)

@property
Expand All @@ -96,6 +97,7 @@ class ProfileHighlight(BaseModel, SimpleHistory):
subindicator = models.PositiveSmallIntegerField(null=True, blank=True)
denominator = models.CharField(choices=DENOMINATOR_CHOICES, max_length=32, help_text="Method for calculating the denominator that will normalise this value.")
label = models.CharField(max_length=255, null=False, blank=True, help_text="Label for the indicator displayed on the front-end")
display_format = models.CharField(choices=DISPLAY_FORMAT_CHOICES, default="percentage", max_length=32, help_text="Method for displaying the value to the user.")
order = models.PositiveIntegerField(default=0, blank=False, null=False)

def __str__(self):
Expand Down
8 changes: 7 additions & 1 deletion wazimap_ng/profile/serializers/highlights_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ def HighlightsSerializer(profile, geography, version):
val = method(highlight, geography, version)

if val is not None:
highlights.append({"label": highlight.label, "value": val, "method": denominator})
highlights.append({
"label": highlight.label,
"value": val,
"value_display_format": highlight.display_format,
"method": denominator
})

return highlights
1 change: 1 addition & 0 deletions wazimap_ng/profile/serializers/metrics_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def MetricsSerializer(profile, geography, version):
"key_metrics": [{
"label": profile_key_metric.label,
"value": val,
"value_display_format": profile_key_metric.display_format,
"method": denominator,
"metadata": {
"description": profile_key_metric.variable.dataset.metadata.description,
Expand Down

0 comments on commit 97c106a

Please sign in to comment.