Move setting namespace initalization code to the class itself. Add method to invalidate all setting caches. Use smart setting cache invalidation for properly test advanced search pagination.

This commit is contained in:
Roberto Rosario
2015-09-06 02:49:27 -04:00
parent e92269e2d8
commit bbfd158e28
3 changed files with 32 additions and 14 deletions

View File

@@ -12,6 +12,7 @@ from documents.tests import (
TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME, TEST_ADMIN_EMAIL,
TEST_DOCUMENT_TYPE, TEST_SMALL_DOCUMENT_PATH
)
from smart_settings import Setting
class Issue46TestCase(TestCase):
@@ -60,10 +61,13 @@ class Issue46TestCase(TestCase):
self.assertEqual(len(result_set), self.document_count)
with self.settings(COMMON_PAGINATE_BY=2):
Setting.invalidate_cache()
# Funcitonal test for the first page of advanced results
response = self.client.get(
reverse('search:results'), {'label': 'test'}
)
self.assertContains(
response, 'Total (1 - 2 out of 4) (Page 1 of 2)',
status_code=200

View File

@@ -1,9 +1,7 @@
from __future__ import unicode_literals
from importlib import import_module
import logging
from django.apps import apps
from django.utils.translation import ugettext_lazy as _
from common import MayanAppConfig, menu_setup, menu_object
@@ -14,8 +12,6 @@ from .classes import Namespace, Setting
from .links import link_namespace_detail, link_namespace_list
from .widgets import setting_widget
logger = logging.getLogger(__name__)
class SmartSettingsApp(MayanAppConfig):
app_namespace = 'settings'
@@ -26,6 +22,8 @@ class SmartSettingsApp(MayanAppConfig):
def ready(self):
super(SmartSettingsApp, self).ready()
Namespace.initialize()
SourceColumn(
source=Namespace, label=_('Setting count'),
func=lambda context: len(context['object'].settings)
@@ -48,13 +46,3 @@ class SmartSettingsApp(MayanAppConfig):
links=(link_namespace_detail,), sources=(Namespace,)
)
menu_setup.bind_links(links=(link_namespace_list,))
for app in apps.get_app_configs():
try:
import_module('{}.settings'.format(app.name))
except ImportError:
logger.debug('App %s has not settings.py file', app.name)
else:
logger.debug(
'Imported settings.py file for app %s', app.name
)

View File

@@ -1,11 +1,17 @@
from __future__ import unicode_literals
from importlib import import_module
import logging
import yaml
from django.apps import apps
from django.conf import settings
from django.utils.functional import Promise
from django.utils.encoding import force_text
logger = logging.getLogger(__name__)
class Namespace(object):
_registry = {}
@@ -18,6 +24,18 @@ class Namespace(object):
def get(cls, name):
return cls._registry[name]
@staticmethod
def initialize():
for app in apps.get_app_configs():
try:
import_module('{}.settings'.format(app.name))
except ImportError:
logger.debug('App %s has not settings.py file', app.name)
else:
logger.debug(
'Imported settings.py file for app %s', app.name
)
def __unicode__(self):
return unicode(self.label)
@@ -36,6 +54,8 @@ class Namespace(object):
class Setting(object):
_registry = []
@staticmethod
def serialize_value(value):
if isinstance(value, Promise):
@@ -47,6 +67,11 @@ class Setting(object):
def deserialize_value(value):
return yaml.safe_load(value)
@classmethod
def invalidate_cache(cls):
for setting in cls._registry:
setting.yaml = None
def __init__(self, namespace, global_name, default, help_text=None, is_path=False):
self.global_name = global_name
self.default = default
@@ -54,6 +79,7 @@ class Setting(object):
self.is_path = is_path
self.yaml = None
namespace.settings.append(self)
self.__class__._registry.append(self)
def __unicode__(self):
return unicode(self.global_name)