From e829ae4943b48db970c45e54501e0abff4d1961a Mon Sep 17 00:00:00 2001 From: AlexCLeduc Date: Thu, 27 Jun 2024 08:50:57 -0400 Subject: [PATCH] chore: upgrade dependencies (#348) * upgrade dependencies * update phac-helpers and use new excel API, also add n+1 tests --------- Co-authored-by: fluxbot --- server/cpho/views/infobase_export.py | 68 ++++++++++++---------------- server/requirements.txt | 10 ++-- server/requirements_dev.txt | 4 +- server/server/fields.py | 4 +- server/server/settings.py | 4 +- server/tests/test_infobase_export.py | 23 +++++++++- 6 files changed, 62 insertions(+), 51 deletions(-) diff --git a/server/cpho/views/infobase_export.py b/server/cpho/views/infobase_export.py index 1de3ef9d..d801deaf 100644 --- a/server/cpho/views/infobase_export.py +++ b/server/cpho/views/infobase_export.py @@ -67,11 +67,10 @@ ] -class IndicatorSheetWriter(AbstractSheetWriter): +class IndicatorSheetWriter(ModelToSheetWriter): sheet_name = "indicator" - - def get_column_configs(self): - return indicator_columns + columns = indicator_columns + queryset = Indicator.objects.all().order_by("name") indicator_data_columns = [ @@ -124,9 +123,14 @@ def get_column_configs(self): class IndicatorDatumSheetWriter(ModelToSheetWriter): sheet_name = "indicator_data" - - def get_column_configs(self): - return indicator_data_columns + queryset = ( + IndicatorDatum.objects.all() + .select_related( + "indicator", "period", "dimension_type", "dimension_value" + ) + .order_by("indicator_id", "period_id", "dimension_type_id") + ) + column = indicator_data_columns benchmarking_columns = [ @@ -151,8 +155,13 @@ def get_column_configs(self): class BenchmarkingSheetWriter(ModelToSheetWriter): - def get_column_configs(self): - return benchmarking_columns + columns = benchmarking_columns + sheet_name = "benchmarking" + queryset = ( + Benchmarking.objects.all() + .select_related("indicator", "oecd_country") + .order_by("indicator_id", "labels", "value") + ) trend_columns = [ @@ -178,8 +187,13 @@ def get_column_configs(self): class TrendSheetWriter(ModelToSheetWriter): - def get_column_configs(self): - return trend_columns + columns = trend_columns + sheet_name = "trend analysis" + queryset = ( + TrendAnalysis.objects.all() + .select_related("indicator") + .order_by("indicator_id", "year") + ) class InfobaseExportView(View): @@ -189,43 +203,19 @@ def workbook(self): def write_indicators(self): # todo: use submitted indicators instead - writer = IndicatorSheetWriter( - workbook=self.workbook, - iterator=Indicator.objects.all().order_by("name"), - ) + writer = IndicatorSheetWriter(workbook=self.workbook) writer.write() def write_indicator_data(self): - qs = ( - IndicatorDatum.objects.all() - .select_related( - "indicator", "period", "dimension_type", "dimension_value" - ) - .order_by("indicator_id", "period_id", "dimension_type_id") - ) - writer = IndicatorDatumSheetWriter( - workbook=self.workbook, iterator=qs.all() - ) + writer = IndicatorDatumSheetWriter(workbook=self.workbook) writer.write() def write_trends(self): - qs = ( - TrendAnalysis.objects.all() - .select_related("indicator") - .order_by("indicator_id", "year") - ) - writer = TrendSheetWriter(workbook=self.workbook, iterator=qs.all()) + writer = TrendSheetWriter(workbook=self.workbook) writer.write() def write_benchmarking(self): - qs = ( - Benchmarking.objects.all() - .select_related("indicator", "oecd_country") - .order_by("indicator_id", "labels", "value") - ) - writer = BenchmarkingSheetWriter( - workbook=self.workbook, iterator=qs.all() - ) + writer = BenchmarkingSheetWriter(workbook=self.workbook) writer.write() def get(self, request, *args, **kwargs): diff --git a/server/requirements.txt b/server/requirements.txt index 050b64ab..93b7c5b2 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,11 +1,11 @@ beautifulsoup4==4.12.0 -django-debug-toolbar==3.8.1 +django-debug-toolbar==4.4.2 django-graphiql-debug-toolbar==0.2.0 -django-extensions==3.2.1 +django-extensions==3.2.3 django-htmx-autocomplete==0.8.2 -django-phac_aspc-helpers==2.1.0 +django-phac_aspc-helpers==3.1.1 django-versionator==1.0.0 -Django==4.1.13 +Django==5.0.6 Faker==15.1.1 freezegun==1.2.2 gunicorn==20.1.0 @@ -20,7 +20,7 @@ pytz==2022.2.1 requests==2.31.0 whitenoise==6.4.0 rules==3.3 -django-ckeditor==6.5.1 +django-ckeditor==6.7.1 PyYAML==6.0.1 # necessary to apply fixtures in prod diff --git a/server/requirements_dev.txt b/server/requirements_dev.txt index a7f5854b..c8b04e5d 100644 --- a/server/requirements_dev.txt +++ b/server/requirements_dev.txt @@ -1,7 +1,7 @@ -r requirements_formatting.txt coverage==7.1.0 -pytest==7.1.2 -pytest-django==4.5.2 +pytest===7.2.1 +pytest-django===4.8.0 pytest-watch==4.2.0 pytest-cov factory-boy===3.2.1 diff --git a/server/server/fields.py b/server/server/fields.py index a82b5bd9..a4c59127 100644 --- a/server/server/fields.py +++ b/server/server/fields.py @@ -18,7 +18,6 @@ migration_ignored_attrs = [ "help_text", "is_searchable", - "description", "verbose_name", "verbose_name_plural", "ordering", @@ -37,9 +36,8 @@ def __init__(self, *args, is_searchable=False, **kwargs): class DescriptionMixin: - def __init__(self, *args, extra_options=None, description="", **kwargs): + def __init__(self, *args, extra_options=None, **kwargs): self.extra_options = extra_options or {} - self.description = description super().__init__(*args, **kwargs) def deconstruct(self): diff --git a/server/server/settings.py b/server/server/settings.py index e487242b..50ac9f25 100644 --- a/server/server/settings.py +++ b/server/server/settings.py @@ -45,7 +45,9 @@ "ENABLE_DEBUG_TOOLBAR", default=False, cast=bool ) INTERNAL_IPS = ( - config("INTERNAL_IPS", default="") if ENABLE_DEBUG_TOOLBAR else "" + config("INTERNAL_IPS", default="", cast=Csv()) + if ENABLE_DEBUG_TOOLBAR + else "" ) # Disable session timeout diff --git a/server/tests/test_infobase_export.py b/server/tests/test_infobase_export.py index 683c8405..9a766ef9 100644 --- a/server/tests/test_infobase_export.py +++ b/server/tests/test_infobase_export.py @@ -2,7 +2,12 @@ from phac_aspc.rules import patch_rules -from cpho.model_factories import IndicatorDatumFactory, IndicatorFactory +from cpho.model_factories import ( + BenchmarkingFactory, + IndicatorDatumFactory, + IndicatorFactory, + TrendAnalysisFactory, +) from cpho.models import Benchmarking, Country, TrendAnalysis @@ -38,3 +43,19 @@ def test_infobase_export(vanilla_user_client): assert response.status_code == 200 assert response["Content-Type"] == "application/vnd.ms-excel" assert response.content + + +def test_infobase_export_queries( + vanilla_user_client, django_assert_max_num_queries +): + url = reverse("infobase_export") + + IndicatorFactory.create_batch(15) + IndicatorDatumFactory.create_batch(15) + TrendAnalysisFactory.create_batch(15) + BenchmarkingFactory.create_batch(15) + + with patch_rules(is_admin_or_hso=True), django_assert_max_num_queries(10): + response = vanilla_user_client.get(url) + assert response.status_code == 200 + assert response["Content-Type"] == "application/vnd.ms-excel"