Skip to content

Commit

Permalink
Cast content type ids to chars in preperation for the switch to UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincarrogan committed Nov 28, 2024
1 parent e670b49 commit 2d97fec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
17 changes: 13 additions & 4 deletions api/audit_trail/managers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import List
from actstream.gfk import GFKQuerySet, GFKManager
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import Q, OuterRef
from django.db.models.functions import Cast

from api.cases.models import Case
from api.staticdata.statuses.libraries.case_status_validate import is_case_status_draft
Expand Down Expand Up @@ -52,8 +54,15 @@ def get_latest_activities(self, case_ids: List, number_of_results):
# iterate over audit records once and add max of 'number_of_results' matching
# action_object_content_type or target_content_type (up to 2x'number_of_results' total)
top_x_per_case[:number_of_results]
return self.get_queryset().filter(
Q(id__in=top_x_per_case.values("id")),
Q(target_object_id__in=case_ids, target_content_type=obj_type)
| Q(action_object_object_id__in=case_ids, action_object_content_type=obj_type),
return (
self.get_queryset()
.alias(
char_target_object_id=Cast("target_object_id", output_field=models.CharField()),
char_action_object_object_id=Cast("action_object_object_id", output_field=models.CharField()),
)
.filter(
Q(id__in=top_x_per_case.values("id")),
Q(char_target_object_id__in=case_ids, target_content_type=obj_type)
| Q(char_action_object_object_id__in=case_ids, action_object_content_type=obj_type),
)
)
15 changes: 11 additions & 4 deletions api/cases/views/search/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import Count, F, Value
from django.db.models.functions import Concat
from django.db.models.functions import (
Cast,
Concat,
)
from django.utils import timezone
from api.audit_trail.service import serialize_case_activity
from api.staticdata.countries.serializers import CountrySerializer
Expand Down Expand Up @@ -149,13 +153,16 @@ def populate_is_recently_updated(cases: List[Dict]):
"""
now = timezone.now()
recent_audits = (
Audit.objects.filter(
target_content_type=ContentType.objects.get_for_model(Case),
target_object_id__in=[
Audit.objects.alias(
char_target_object_id=Cast("target_object_id", output_field=models.CharField()),
)
.filter(
char_target_object_id__in=[
case["id"]
for case in cases
if working_days_in_range(case["submitted_at"], now) > settings.RECENTLY_UPDATED_WORKING_DAYS
],
target_content_type=ContentType.objects.get_for_model(Case),
actor_content_type=ContentType.objects.get_for_model(GovUser),
created_at__gt=now - timedelta(days=number_of_days_since(now, settings.RECENTLY_UPDATED_WORKING_DAYS)),
)
Expand Down

0 comments on commit 2d97fec

Please sign in to comment.