From 6903c2723418fca74445c499a9616c5bcfdfbe26 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 08:17:39 +0300 Subject: [PATCH 01/12] Add view to display cumulative result count --- profiles/admin.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/profiles/admin.py b/profiles/admin.py index a882fc8..fc0f019 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -1,11 +1,13 @@ import csv from django.contrib import admin +from django.db.models import Sum from django.http import HttpResponse from profiles.models import ( Answer, AnswerOther, + CumulativeResultCount, Option, PostalCode, PostalCodeResult, @@ -202,6 +204,65 @@ class Meta: model = PostalCodeResult +@admin.register(CumulativeResultCount) +class CumulativeResultCountAdmin( + DisableDeleteAdminMixin, + DisableChangeAdminMixin, + DisableAddAdminMixin, + admin.ModelAdmin, +): + # Admin view that displays the sum of the counts for Home or Optional postal code results for every result (animal). + + def changelist_view(self, request, extra_context=None): + # Choose all rows, when selecting action. + try: + action = self.get_actions(request)[request.POST["action"]][0] + action_acts_on_all = action.acts_on_all + except (KeyError, AttributeError): + action_acts_on_all = False + if action_acts_on_all: + post = request.POST.copy() + post.setlist( + admin.helpers.ACTION_CHECKBOX_NAME, + self.model.objects.values_list("id", flat=True), + ) + request.POST = post + + return admin.ModelAdmin.changelist_view(self, request, extra_context) + + list_display = ("value", "topic", "sum_of_count", "selected_postal_code_type") + actions = ["home_postal_code_type", "optional_postal_code_type"] + postal_code_type = PostalCodeType.objects.get( + type_name=PostalCodeType.HOME_POSTAL_CODE + ) + + def home_postal_code_type(self, request, queryset): + self.postal_code_type = PostalCodeType.objects.get( + type_name=PostalCodeType.HOME_POSTAL_CODE + ) + + home_postal_code_type.acts_on_all = True + + def optional_postal_code_type(self, request, queryset): + self.postal_code_type = PostalCodeType.objects.get( + type_name=PostalCodeType.OPTIONAL_POSTAL_CODE + ) + + optional_postal_code_type.acts_on_all = True + + def selected_postal_code_type(self, obj): + return self.postal_code_type.type_name + + def sum_of_count(self, obj): + qs = PostalCodeResult.objects.filter( + postal_code_type=self.postal_code_type, result=obj + ) + return qs.aggregate(sum_of_count=Sum("count"))["sum_of_count"] + + class Meta: + model = CumulativeResultCount + + admin.site.register(Question, QuestionAdmin) admin.site.register(QuestionCondition, QuestionConditionAdmin) admin.site.register(SubQuestion, SubQuestionAdmin) From 7f98a6fd43e626db6f9cc0216b095544dfa84d0a Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 08:18:08 +0300 Subject: [PATCH 02/12] Add proxy model CumulativeResultcount Used in admin --- profiles/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/profiles/models.py b/profiles/models.py index 04486f1..adbded3 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -219,3 +219,8 @@ def __str__(self): return f"{self.postal_code_type} postal_code: {self.postal_code}, count: {self.count}" else: return f"count: {self.count}" + + +class CumulativeResultCount(Result): + class Meta: + proxy = True From e1ce48b15fa4ced6edc6bbd1a2b81ae9a80eea8b Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 08:18:47 +0300 Subject: [PATCH 03/12] Add translations to CumulativeResultCount model --- profiles/translation.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/profiles/translation.py b/profiles/translation.py index bb504f7..d116f34 100644 --- a/profiles/translation.py +++ b/profiles/translation.py @@ -1,6 +1,6 @@ from modeltranslation.translator import TranslationOptions, translator -from profiles.models import Option, Question, Result, SubQuestion +from profiles.models import CumulativeResultCount, Option, Question, Result, SubQuestion class QuestionTranslationOptions(TranslationOptions): @@ -35,3 +35,10 @@ class ResultTranslationOptions(TranslationOptions): translator.register(Result, ResultTranslationOptions) + + +class CumulativeResultCountTranslationOptions(ResultTranslationOptions): + pass + + +translator.register(CumulativeResultCount, CumulativeResultCountTranslationOptions) From fde6782b723e27945b59b81825f87c83c0883167 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 09:52:52 +0300 Subject: [PATCH 04/12] Add healthceck to postgres service --- docker-compose.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index f4dfa4c..0587c92 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,11 @@ services: - "5432:5432" volumes: - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + interval: 10s + timeout: 5s + retries: 5 memcached: image: memcached:latest From 7f81181be2ae09afa830a2ac23b14d5dd8960a9f Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 10:15:48 +0300 Subject: [PATCH 05/12] Add healtcheck to postgres service --- docker-compose.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index f4dfa4c..c442072 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,12 @@ services: ports: - "5432:5432" volumes: - - postgres-data:/var/lib/postgresql/data + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + interval: 10s + timeout: 5s + retries: 5 memcached: image: memcached:latest From 2d5fa24998dbf4b8bb02c937473e1598e12bb09b Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 10:16:24 +0300 Subject: [PATCH 06/12] Remove healthceck --- docker-compose.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0587c92..f4dfa4c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,11 +11,6 @@ services: - "5432:5432" volumes: - postgres-data:/var/lib/postgresql/data - healthcheck: - test: ["CMD-SHELL", "pg_isready"] - interval: 10s - timeout: 5s - retries: 5 memcached: image: memcached:latest From d74925164f9bcf4431fc0a40471de6854e49972b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 16 Apr 2024 07:18:27 +0000 Subject: [PATCH 07/12] Bump sqlparse from 0.4.4 to 0.5.0 Bumps [sqlparse](https://github.com/andialbrecht/sqlparse) from 0.4.4 to 0.5.0. - [Changelog](https://github.com/andialbrecht/sqlparse/blob/master/CHANGELOG) - [Commits](https://github.com/andialbrecht/sqlparse/compare/0.4.4...0.5.0) --- updated-dependencies: - dependency-name: sqlparse dependency-type: indirect ... Signed-off-by: dependabot[bot] --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 0f91efd..57a447f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -116,13 +116,14 @@ pyyaml==6.0 # via drf-spectacular six==1.16.0 # via python-dateutil -sqlparse==0.4.4 +sqlparse==0.5.0 # via django tomli==2.0.1 # via # black # build # coverage + # pyproject-hooks # pytest typing-extensions==4.5.0 # via django-modeltranslation From 077024d6c5b0d3d9031110f9e3f41784496aa56a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 16 Apr 2024 07:27:10 +0000 Subject: [PATCH 08/12] Bump django from 4.2.10 to 4.2.11 Bumps [django](https://github.com/django/django) from 4.2.10 to 4.2.11. - [Commits](https://github.com/django/django/compare/4.2.10...4.2.11) --- updated-dependencies: - dependency-name: django dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 0f91efd..250e26f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,7 +18,7 @@ click==8.1.3 # pip-tools coverage[toml]==7.2.3 # via pytest-cov -django==4.2.10 +django==4.2.11 # via # -r requirements.in # django-cors-headers @@ -123,6 +123,7 @@ tomli==2.0.1 # black # build # coverage + # pyproject-hooks # pytest typing-extensions==4.5.0 # via django-modeltranslation From 389f9f7c4c330e6a2cbef12ca90fdd940b331e7a Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 10:28:18 +0300 Subject: [PATCH 09/12] Remove healthcheck --- docker-compose.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c442072..0b22bb3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,12 +11,7 @@ services: - "5432:5432" volumes: - postgres-data:/var/lib/postgresql/data - healthcheck: - test: ["CMD-SHELL", "pg_isready"] - interval: 10s - timeout: 5s - retries: 5 - + memcached: image: memcached:latest command: ["-I", "4m"] From 6df3e568bc707782ad34f0c14c695eaa6118c10d Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 10:44:13 +0300 Subject: [PATCH 10/12] Get sum_of_count from model --- profiles/admin.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/profiles/admin.py b/profiles/admin.py index fc0f019..da9fe15 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -1,7 +1,6 @@ import csv from django.contrib import admin -from django.db.models import Sum from django.http import HttpResponse from profiles.models import ( @@ -232,32 +231,23 @@ def changelist_view(self, request, extra_context=None): list_display = ("value", "topic", "sum_of_count", "selected_postal_code_type") actions = ["home_postal_code_type", "optional_postal_code_type"] - postal_code_type = PostalCodeType.objects.get( - type_name=PostalCodeType.HOME_POSTAL_CODE - ) + postal_code_type_name = PostalCodeType.HOME_POSTAL_CODE def home_postal_code_type(self, request, queryset): - self.postal_code_type = PostalCodeType.objects.get( - type_name=PostalCodeType.HOME_POSTAL_CODE - ) + self.postal_code_type_name = PostalCodeType.HOME_POSTAL_CODE home_postal_code_type.acts_on_all = True def optional_postal_code_type(self, request, queryset): - self.postal_code_type = PostalCodeType.objects.get( - type_name=PostalCodeType.OPTIONAL_POSTAL_CODE - ) + self.postal_code_type_name = PostalCodeType.OPTIONAL_POSTAL_CODE optional_postal_code_type.acts_on_all = True def selected_postal_code_type(self, obj): - return self.postal_code_type.type_name + return self.postal_code_type_name def sum_of_count(self, obj): - qs = PostalCodeResult.objects.filter( - postal_code_type=self.postal_code_type, result=obj - ) - return qs.aggregate(sum_of_count=Sum("count"))["sum_of_count"] + return obj.get_sum_of_count(self.postal_code_type_name) class Meta: model = CumulativeResultCount From ca3ba7507a31809ba193eeee68c8544679f08ddf Mon Sep 17 00:00:00 2001 From: juuso-j Date: Tue, 16 Apr 2024 10:44:32 +0300 Subject: [PATCH 11/12] Add get_sum_of_count function --- profiles/models.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/profiles/models.py b/profiles/models.py index adbded3..3f43c45 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.db.models import Sum class Question(models.Model): @@ -224,3 +225,10 @@ def __str__(self): class CumulativeResultCount(Result): class Meta: proxy = True + + def get_sum_of_count(self, postal_code_type_name=PostalCodeType.HOME_POSTAL_CODE): + postal_code_type = PostalCodeType.objects.get(type_name=postal_code_type_name) + qs = PostalCodeResult.objects.filter( + postal_code_type=postal_code_type, result=self + ) + return qs.aggregate(sum_of_count=Sum("count"))["sum_of_count"] From 8c9cf52715ec51cb4a445fde74e23404c2276693 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 16 Apr 2024 07:48:46 +0000 Subject: [PATCH 12/12] Bump black from 23.3.0 to 24.3.0 Bumps [black](https://github.com/psf/black) from 23.3.0 to 24.3.0. - [Release notes](https://github.com/psf/black/releases) - [Changelog](https://github.com/psf/black/blob/main/CHANGES.md) - [Commits](https://github.com/psf/black/compare/23.3.0...24.3.0) --- updated-dependencies: - dependency-name: black dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0f91efd..5f3ee66 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ asgiref==3.6.0 # via django attrs==23.1.0 # via jsonschema -black==23.3.0 +black==24.3.0 # via -r requirements.in build==0.10.0 # via pip-tools @@ -123,9 +123,12 @@ tomli==2.0.1 # black # build # coverage + # pyproject-hooks # pytest typing-extensions==4.5.0 - # via django-modeltranslation + # via + # black + # django-modeltranslation tzdata==2023.3 # via pandas uritemplate==4.1.1