Skip to content

Commit

Permalink
chore: refactor values query to managers
Browse files Browse the repository at this point in the history
  • Loading branch information
MyPyDavid authored and jochenklar committed Nov 16, 2023
1 parent c51b4f1 commit 2c2afef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 7 additions & 0 deletions rdmo/projects/managers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):

Expand Down
13 changes: 5 additions & 8 deletions rdmo/projects/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2c2afef

Please sign in to comment.