Skip to content

Commit

Permalink
Merge pull request #53 from City-of-Turku/feature/admin-view-for-cumu…
Browse files Browse the repository at this point in the history
…lative-result-count

Feature/admin view for cumulative result count
  • Loading branch information
juuso-j authored Apr 22, 2024
2 parents 2d5fa24 + ca3ba75 commit f4ebb98
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ services:
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data

- postgres-data:/var/lib/postgresql/data
memcached:
image: memcached:latest
command: ["-I", "4m"]
Expand Down
51 changes: 51 additions & 0 deletions profiles/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from profiles.models import (
Answer,
AnswerOther,
CumulativeResultCount,
Option,
PostalCode,
PostalCodeResult,
Expand Down Expand Up @@ -202,6 +203,56 @@ 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_name = PostalCodeType.HOME_POSTAL_CODE

def home_postal_code_type(self, request, queryset):
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_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_name

def sum_of_count(self, obj):
return obj.get_sum_of_count(self.postal_code_type_name)

class Meta:
model = CumulativeResultCount


admin.site.register(Question, QuestionAdmin)
admin.site.register(QuestionCondition, QuestionConditionAdmin)
admin.site.register(SubQuestion, SubQuestionAdmin)
Expand Down
13 changes: 13 additions & 0 deletions profiles/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.db.models import Sum


class Question(models.Model):
Expand Down Expand Up @@ -219,3 +220,15 @@ 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

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"]
9 changes: 8 additions & 1 deletion profiles/translation.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -35,3 +35,10 @@ class ResultTranslationOptions(TranslationOptions):


translator.register(Result, ResultTranslationOptions)


class CumulativeResultCountTranslationOptions(ResultTranslationOptions):
pass


translator.register(CumulativeResultCount, CumulativeResultCountTranslationOptions)

0 comments on commit f4ebb98

Please sign in to comment.