我可以在Django设置中定义类,如何在测试中覆盖此类设置?

时间:2018-12-27 09:51:05

标签: python django python-3.x override django-settings

我们将Django用于Speedy Net and Speedy Match(当前是Django 1.11.17,由于我们的要求之一django-modeltranslation,我们无法升级到较新版本的Django)。我想将某些设置定义为类。例如:

class UserSettings(object):
    MIN_USERNAME_LENGTH = 6
    MAX_USERNAME_LENGTH = 40

    MIN_SLUG_LENGTH = 6
    MAX_SLUG_LENGTH = 200

    # Users can register from age 0 to 180, but can't be kept on the site after age 250.
    MIN_AGE_ALLOWED_IN_MODEL = 0  # In years.
    MAX_AGE_ALLOWED_IN_MODEL = 250  # In years.

    MIN_AGE_ALLOWED_IN_FORMS = 0  # In years.
    MAX_AGE_ALLOWED_IN_FORMS = 180  # In years.

    MIN_PASSWORD_LENGTH = 8
    MAX_PASSWORD_LENGTH = 120

    PASSWORD_VALIDATORS = [
        {
            'NAME': 'speedy.core.accounts.validators.PasswordMinLengthValidator',
        },
        {
            'NAME': 'speedy.core.accounts.validators.PasswordMaxLengthValidator',
        },
    ]

(在https://github.com/speedy-net/speedy-net/blob/staging/speedy/net/settings/global_settings.py中定义)。然后在模型中,我尝试使用:

from django.conf import settings as django_settings

class User(ValidateUserPasswordMixin, PermissionsMixin, Entity, AbstractBaseUser):
    settings = django_settings.UserSettings

(然后在类中使用settings的属性,例如settings.MIN_USERNAME_LENGTH)。

但是会引发异常

AttributeError: 'Settings' object has no attribute 'UserSettings'

(但是如果我在那里使用不是类的常量,它不会引发异常)。

这是第一个问题。在此期间,我改为定义:

from speedy.net.settings import global_settings as speedy_net_global_settings

class User(ValidateUserPasswordMixin, PermissionsMixin, Entity, AbstractBaseUser):
    settings = speedy_net_global_settings.UserSettings

第二个问题是,如何在测试中覆盖此类设置?例如,我使用以下代码:

from speedy.core.settings import tests as tests_settings

@override_settings(MAX_NUMBER_OF_FRIENDS_ALLOWED=tests_settings.OVERRIDE_MAX_NUMBER_OF_FRIENDS_ALLOWED)
https://github.com/speedy-net/speedy-net/blob/staging/speedy/core/friends/tests/test_views.py

。但是,如果MAX_NUMBER_OF_FRIENDS_ALLOWED将在类UserSettings中定义,该如何覆盖呢?

2 个答案:

答案 0 :(得分:1)

Django不会期望您从其低级设计选择中偏离太多,并且通常很难解决Django不允许您自定义的问题。

  1. Django的设置对象explicitly skips over设置模块中具有非大写名称的任何对象。如果将您的班级重命名为USER_SETTINGS,它将起作用。如果您真的想保留对象的原始名称,那么一个可怕的解决方案就是欺骗Django:

    class UserSettings:
        ...
    
    class AlwaysUppercaseStr(str):
        def isupper(self):
            return True
    
    globals()[AlwaysUppercaseStr('UserSettings')] = globals().pop('UserSettings')
    

    我不知道它是否可以在Python实现中移植,但是可以与CPython的dir()一起使用。

  2. override_settings不支持您要执行的操作,因此您可能需要重写该类以允许全局settings对象是可配置的。

    < / li>

答案 1 :(得分:0)

感谢@Blender提供提示:

  

Django的设置对象显式跳过您的任何对象   具有非大写名称的设置模块。如果您将班级重命名为   USER_SETTINGS,它将正常工作。

我不知道所有设置都必须大写。因此,我将class UserSettings重命名为class USER_SETTINGS(尽管PyCharm不喜欢它),但是我检查了一下,还可以在文件末尾添加以下代码:

USER_SETTINGS = UserSettings

无需重命名课程。

关于第二个问题-如何在测试中覆盖此类设置?我添加了一个名为utils.py的文件:

def get_django_settings_class_with_override_settings(django_settings_class, **override_settings):
    class django_settings_class_with_override_settings(django_settings_class):
        pass

    for setting, value in override_settings.items():
        setattr(django_settings_class_with_override_settings, setting, value)

    return django_settings_class_with_override_settings

(您可以在https://github.com/speedy-net/speedy-net/blob/staging/speedy/core/base/test/utils.py上看到它)

然后在测试中:

from django.conf import settings as django_settings
from django.test import override_settings

from speedy.core.settings import tests as tests_settings
from speedy.core.base.test.utils import get_django_settings_class_with_override_settings

    @override_settings(USER_SETTINGS=get_django_settings_class_with_override_settings(django_settings_class=django_settings.USER_SETTINGS, MAX_NUMBER_OF_FRIENDS_ALLOWED=tests_settings.OVERRIDE_USER_SETTINGS.MAX_NUMBER_OF_FRIENDS_ALLOWED))
    def test_user_can_send_friend_request_if_not_maximum(self):
        self.assertEqual(first=django_settings.USER_SETTINGS.MAX_NUMBER_OF_FRIENDS_ALLOWED, second=4)

我检查了一下,然后必须定义另一个类(在这种情况下为class django_settings_class_with_override_settings,因为如果直接更改类django_settings_class,它也会影响其他未使用@override_settings的测试。