Skip to content

Commit

Permalink
Skip redundant queries to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Oct 24, 2023
1 parent f2328a5 commit 8842358
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from onadata.apps.logger.signals import pre_delete_attachment
from onadata.libs.utils.logger_tools import get_soft_deleted_attachments
from onadata.apps.viewer.models.parsed_instance import datetime_from_str


class Command(BaseCommand):
Expand Down Expand Up @@ -62,7 +61,9 @@ def handle(self, *args, **kwargs):
'to run on big databases'
)

instance_ids = Attachment.objects.values_list('instance_id', flat=True).distinct()
instance_ids = Attachment.objects.values_list(
'instance_id', flat=True
).distinct()

if start_id:
instance_ids = instance_ids.filter(instance_id__gte=start_id)
Expand Down Expand Up @@ -112,6 +113,8 @@ def handle(self, *args, **kwargs):
continue

for soft_deleted_attachment in soft_deleted_attachments:
# Avoid fetching Instance object once again
soft_deleted_attachment.instance = instance
pre_delete_attachment(
soft_deleted_attachment, only_update_counters=True
)
Expand Down
14 changes: 10 additions & 4 deletions onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import re
from copy import deepcopy
from io import BytesIO
from xml.sax.saxutils import escape as xml_escape

Expand Down Expand Up @@ -129,10 +130,15 @@ def url(self):
}
)

def data_dictionary(self):
from onadata.apps.viewer.models.data_dictionary import\
DataDictionary
return DataDictionary.all_objects.get(pk=self.pk)
def data_dictionary(self, use_cache: bool = False):
from onadata.apps.viewer.models.data_dictionary import DataDictionary

if not use_cache:
return DataDictionary.all_objects.get(pk=self.pk)

xform_dict = deepcopy(self.__dict__)
xform_dict.pop('_state', None)
return DataDictionary(**xform_dict)

@property
def has_instances_with_geopoints(self):
Expand Down
3 changes: 1 addition & 2 deletions onadata/apps/logger/xform_instance_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,9 @@ def get_xform_media_question_xpaths(
xform: 'onadata.apps.logger.models.XForm',
) -> list:
logger = logging.getLogger('console_logger')
parser = XFormInstanceParser(xform.xml, xform.data_dictionary())
parser = XFormInstanceParser(xform.xml, xform.data_dictionary(use_cache=True))
all_attributes = _get_all_attributes(parser.get_root_node())
media_field_xpaths = []

# This code expects that the attributes from Enketo Express are **always**
# sent in the same order.
# For example:
Expand Down

0 comments on commit 8842358

Please sign in to comment.