Skip to content

Commit

Permalink
feat: Streamlined periods (#292)
Browse files Browse the repository at this point in the history
* Add is_current bool to periods

* use is_current to filter years available for programs
  • Loading branch information
AlexCLeduc authored Apr 26, 2024
1 parent 258fc02 commit ac43bc2
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 39 deletions.
6 changes: 5 additions & 1 deletion server/cpho/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

for model in this_app.values():
if hasattr(model, "__add_to_admin"):
admin.site.register(model)

if hasattr(model, "adminClass"):
admin.site.register(model, model.adminClass)
else:
admin.site.register(model)
3 changes: 3 additions & 0 deletions server/cpho/fixtures/periods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
fields:
year: 2023
year_type: calendar
is_current: True
- model: cpho.period
pk: 4
fields:
Expand All @@ -28,6 +29,7 @@
fields:
year: 2023
year_type: fiscal
is_current: True
- model: cpho.period
pk: 7
fields:
Expand Down Expand Up @@ -82,6 +84,7 @@
year: 2023
quarter: 1
year_type: fiscal
is_current: True
- model: cpho.period
pk: 16
fields:
Expand Down
39 changes: 24 additions & 15 deletions server/cpho/jinja2/indicators/view_indicator.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
<span aria-hidden="true" class="bi bi-clipboard-data"></span>
{{ tm("benchmarking") }}
</a>
{% else %}
{% if respects_rule('can_view_benchmarking', indicator) %}
{% else %}
{% if respects_rule('can_view_benchmarking', indicator) %}
<a href="{{ url('manage_benchmarking_data', args=[indicator.id]) }}"
class="btn w-100 btn-outline-dark my-1">
class="btn w-100 btn-outline-dark my-1">
<span aria-hidden="true" class="bi bi-clipboard-data"></span>
{{ tm("benchmarking") }}
</a>
Expand Down Expand Up @@ -98,7 +98,7 @@

{% if respects_rule('can_access_indicator', indicator) %}
<div class='card my-4'>
<div class="card-header h3">{{ tm("periods_with_data") }}</div>
<div class="card-header h3">{{ tm("periodic_data") }}</div>
<div class="card-body">
<table class="table table-hover">
<thead>
Expand All @@ -112,12 +112,20 @@
</thead>
<tbody>
{% for period,data_count in data_counts_by_period.items() %}
{% if data_count > 0 %}
{% if period.is_current or respects_rule("can_view_non_current_periods") %}
{% set submission_statuses = submission_statuses_by_period[period] %}
{% set progress_in_table = true %}
{% set metadata_submission = false %}
<tr class="text-center">
<th>{{ period }}</th>
<th>
{{ period }}
{% if not period.is_current %}
<div>
<div class="badge bg-success" ">{{ tm("hso_only") }}
</div>
</div>
{% endif %}
</th>
<td>{{ data_count }}</td>
<td>{{ submission_status_badge(submission_statuses['hso_global_status']) }}</td>
<td>{{ submission_status_badge(submission_statuses['program_global_status']) }}</td>
Expand All @@ -133,16 +141,17 @@
</div>
</div>
{% endif %}
{% if respects_rule('can_access_indicator', indicator) %}
<div class="h3">{{ tm("add_data_for_new_periods") }}</div>
{% if respects_rule('can_view_non_current_periods', indicator) %}
<div>
<span class="h3">{{ tm("add_data_for_new_periods") }}</span>
<span class="badge bg-success" style="transform:translate(0,-0.75em);">{{ tm("hso_only") }}</span>
</div>
<div>
<ul aria-label="{{ tm('add_data_for_new_periods') }}">
{% for period,data_count in data_counts_by_period.items() %}
{% if data_count == 0 %}
<li>
<a href="{{ url('view_indicator_for_period', args=[indicator.id, period.pk]) }}">{{ tm("add_data") }} {{ tm("for") }} {{ period }}</a>
</li>
{% endif %}
<ul aria-label="{{ tm("add_data_for_new_periods") }}">
{% for period in alternate_periods %}
<li>
<a href="{{ url('view_indicator_for_period', args=[indicator.id, period.pk]) }}">{{ tm("add_data") }} {{ tm("for") }} {{ period }}</a>
</li>
{% endfor %}
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.1.13 on 2024-04-25 17:16

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("cpho", "0033_alter_benchmarking_year_and_more"),
]

operations = [
migrations.AddField(
model_name="period",
name="is_current",
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name="periodhistory",
name="is_current",
field=models.BooleanField(default=False),
),
]
20 changes: 18 additions & 2 deletions server/cpho/models/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,29 @@ def get_relevant_periods(self):
from .lookups import Period

globally_relevant = Period.get_currently_relevant_periods()
return self._filter_irrelevant_periods(globally_relevant)

def get_adjacent_periods(self):
from .lookups import Period

globally_relevant = Period.get_currently_relevant_periods()
min_year = min([x.year for x in globally_relevant])
max_year = max([x.year for x in globally_relevant])
adjacent_periods = Period.objects.filter(
year__lte=max_year + 2, year__gte=min_year - 2
)
adjacent_periods = set(adjacent_periods) - set(globally_relevant)
return self._filter_irrelevant_periods(adjacent_periods)

def _filter_irrelevant_periods(self, periods):
# if there isn't a "preference" set, return all relevant periods
from .lookups import Period

if not self.relevant_period_types:
return globally_relevant
return periods

relevant = []
for period in globally_relevant:
for period in periods:
if (
period.quarter
and "fiscal_quarters" in self.relevant_period_types
Expand Down
9 changes: 6 additions & 3 deletions server/cpho/models/lookups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.contrib import admin
from django.db import models

from server import fields
Expand Down Expand Up @@ -83,6 +84,7 @@ class Period(models.Model):
year_type = fields.CharField(
max_length=50, choices=YEAR_TYPE_CHOICES, null=True, blank=True
)
is_current = models.BooleanField(default=False)

class Meta:
ordering = ["year", "quarter"]
Expand Down Expand Up @@ -118,9 +120,10 @@ def __str__(self):

@staticmethod
def get_currently_relevant_periods():
return Period.objects.filter(
year__in=[settings.CURRENT_YEAR - 1, settings.CURRENT_YEAR]
)
return Period.objects.filter(is_current=True)

class adminClass(admin.ModelAdmin):
list_display = ["year", "year_type", "quarter", "is_current"]


class Country(models.Model):
Expand Down
5 changes: 5 additions & 0 deletions server/cpho/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,8 @@ def can_view_benchmarking(user, indicator):
@auto_rule
def can_edit_trend_analysis(user, indicator):
return can_edit_indicator(user, indicator)


@auto_rule
def can_view_non_current_periods(user):
return is_admin_or_hso(user)
6 changes: 3 additions & 3 deletions server/cpho/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,9 @@
"en": "Actions",
"fr": "Actions",
},
"periods_with_data": {
"en": "Periods with data",
"fr": "Périodes avec des données",
"periodic_data": {
"en": "Periodic data",
"fr": "Données périodiques",
},
"indicator_information": {
"en": "Indicator Information",
Expand Down
25 changes: 14 additions & 11 deletions server/cpho/views/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,25 @@ def get_context_data(self, **kwargs):
indicator = Indicator.objects.get(pk=self.kwargs["pk"])
relevant_periods = indicator.get_relevant_periods()

data_for_periods = indicator.data.filter(
period__in=relevant_periods, is_deleted=False
all_data = indicator.data.filter(is_deleted=False).prefetch_related(
"period"
)
periods_with_data = set(d.period for d in all_data)

all_shown_periods = periods_with_data | set(relevant_periods)

data_counts_by_period = {
p: len(
[
datum
for datum in data_for_periods
if datum.period_id == p.id
]
)
for p in relevant_periods
p: len([datum for datum in all_data if datum.period_id == p.id])
for p in all_shown_periods
}
submission_statuses_by_period = {
p: get_submission_statuses(indicator, p) for p in relevant_periods
p: get_submission_statuses(indicator, p) for p in all_shown_periods
}

alternate_periods = (
set(indicator.get_adjacent_periods()) - periods_with_data
)

return {
**super().get_context_data(**kwargs),
"dimension_types": DimensionType.objects.all(),
Expand All @@ -401,6 +403,7 @@ def get_context_data(self, **kwargs):
"metadata_submission_statuses": get_metadata_submission_statuses(
indicator
),
"alternate_periods": alternate_periods,
}


Expand Down
4 changes: 0 additions & 4 deletions server/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,3 @@
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# BUSINESS LOGIC CONFIGURATION

CURRENT_YEAR = 2023

0 comments on commit ac43bc2

Please sign in to comment.