From e7670e0ab1fa60de69392de687b515097387aed9 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 17 Feb 2021 11:37:13 +0100 Subject: [PATCH] dataviz: refresh statistics list more frequently using spooler (#50891) --- combo/apps/dataviz/forms.py | 16 +++++++++++++++- combo/apps/dataviz/models.py | 3 +++ combo/utils/spooler.py | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/combo/apps/dataviz/forms.py b/combo/apps/dataviz/forms.py index c22f16f9..d0c3ce36 100644 --- a/combo/apps/dataviz/forms.py +++ b/combo/apps/dataviz/forms.py @@ -14,13 +14,15 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import sys from collections import OrderedDict from django import forms from django.conf import settings +from django.db import transaction, connection from django.db.models import Q -from combo.utils import requests +from combo.utils import requests, cache_during_request from .models import ChartCell, ChartNgCell @@ -45,6 +47,17 @@ class ChartForm(forms.ModelForm): self.fields['url'].widget = forms.Select(choices=available_charts) +@cache_during_request +def trigger_statistics_list_refresh(): + if 'uwsgi' in sys.modules: + from combo.utils.spooler import refresh_statistics_list + + tenant = getattr(connection, 'tenant', None) + transaction.on_commit( + lambda: refresh_statistics_list.spool(domain=getattr(tenant, 'domain_url', None)) + ) + + class ChartNgForm(forms.ModelForm): blank_choice = ('', '---------') @@ -67,6 +80,7 @@ class ChartNgForm(forms.ModelForm): } def __init__(self, *args, **kwargs): + trigger_statistics_list_refresh() super().__init__(*args, **kwargs) field_ids = list(self._meta.fields) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 55f59e9c..4f15e9bb 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -161,6 +161,9 @@ class ChartNgCell(CellBase): null=True, on_delete=models.SET_NULL, related_name='cells', + help_text=_( + 'This list may take a few seconds to be updated, please refresh the page if an item is missing.' + ), ) filter_params = JSONField(default=dict) title = models.CharField(_('Title'), max_length=150, blank=True) diff --git a/combo/utils/spooler.py b/combo/utils/spooler.py index 05235467..0e9e813c 100644 --- a/combo/utils/spooler.py +++ b/combo/utils/spooler.py @@ -13,3 +13,25 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . + +from django.db import connection + +from uwsgidecorators import spool + +from combo.apps.dataviz.utils import update_available_statistics + + +def set_connection(domain): + from hobo.multitenant.middleware import TenantMiddleware + + tenant = TenantMiddleware.get_tenant_by_hostname(domain) + connection.set_tenant(tenant) + + +@spool +def refresh_statistics_list(args): + if args.get('domain'): + # multitenant installation + set_connection(args['domain']) + + update_available_statistics()