From 2c2afefbdac0643892571f1afa18bc2e03a5b198 Mon Sep 17 00:00:00 2001 From: David Wallace Date: Wed, 15 Nov 2023 12:11:39 +0100 Subject: [PATCH] chore: refactor values query to managers --- rdmo/projects/managers.py | 7 +++++++ rdmo/projects/progress.py | 13 +++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/rdmo/projects/managers.py b/rdmo/projects/managers.py index 8ee22b0da2..72e7c21a0b 100644 --- a/rdmo/projects/managers.py +++ b/rdmo/projects/managers.py @@ -1,5 +1,6 @@ from django.conf import settings from django.db import models +from django.db.models import Q from mptt.models import TreeManager from mptt.querysets import TreeQuerySet @@ -141,6 +142,12 @@ def filter_user(self, user): else: return self.none() + def exclude_empty(self): + return self.exclude((Q(text='') | Q(text=None)) & Q(option=None) & (Q(file='') | Q(file=None))) + + def distinct_list(self): + return self.order_by('attribute').values_list('attribute', 'set_prefix', 'set_index').distinct() + class ProjectManager(CurrentSiteManagerMixin, TreeManager): diff --git a/rdmo/projects/progress.py b/rdmo/projects/progress.py index 40d2b08b92..ef082fa507 100644 --- a/rdmo/projects/progress.py +++ b/rdmo/projects/progress.py @@ -40,13 +40,11 @@ def compute_navigation(section, project, snapshot=None): # compute sets from values (including empty values) sets = defaultdict(lambda: defaultdict(list)) - for attribute, set_prefix, set_index in values.order_by('attribute') \ - .values_list('attribute', 'set_prefix', 'set_index').distinct(): + for attribute, set_prefix, set_index in values.distinct_list(): sets[attribute][set_prefix].append(set_index) # query distinct, non empty set values - values_list = values.exclude((Q(text='') | Q(text=None)) & Q(option=None) & (Q(file='') | Q(file=None))) \ - .order_by('attribute').values_list('attribute', 'set_prefix', 'set_index').distinct() + values_list = values.exclude_empty().distinct_list() navigation = [] for catalog_section in project.catalog.elements: @@ -92,13 +90,12 @@ def compute_progress(project, snapshot=None): # compute sets from values (including empty values) sets = defaultdict(lambda: defaultdict(list)) - for attribute, set_prefix, set_index in values.order_by('attribute') \ - .values_list('attribute', 'set_prefix', 'set_index').distinct(): + for attribute, set_prefix, set_index in values.distinct_list(): sets[attribute][set_prefix].append(set_index) # query distinct, non empty set values - values_list = values.exclude((Q(text='') | Q(text=None)) & Q(option=None) & (Q(file='') | Q(file=None))) \ - .order_by('attribute').values_list('attribute', 'set_prefix', 'set_index').distinct() + values_list = values.exclude_empty().distinct_list() + # count the total number of questions, taking sets and conditions into account counts = count_questions(project.catalog, sets, conditions)