Thursday, December 27, 2018

Re: Can I define classes in Django settings, and how can I override such settings in tests?

You need an instance to evaluate member variables.   These are mutable, so can be different per instance.

Settings should use a dictionary.

Sent from my iPhone

On Dec 27, 2018, at 3:15 AM, אורי <uri@speedy.net> wrote:

Hi,

We are using Django for Speedy Net and Speedy Match (currently Django 1.11.17, we can't upgrade to a newer version of Django because of one of our requirements, django-modeltranslation). I want to define some of our settings as classes. For example:

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',
},
]


from django.conf import settings as django_settings

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

(and then use attributes of `settings`, such as `settings.MIN_USERNAME_LENGTH`, in the class).

But it throws an exception

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

(but it doesn't throw an exception if I use there a constant which is not a class).

This is the first problem. In the meantime, I defined instead:

from speedy.net.settings import global_settings as speedy_net_global_settings

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

The second problem, is how do I override such settings in tests? For example, I use the following code:

from speedy.core.settings import tests as tests_settings

@override_settings(MAX_NUMBER_OF_FRIENDS_ALLOWED=tests_settings.OVERRIDE_MAX_NUMBER_OF_FRIENDS_ALLOWED)

in https://github.com/speedy-net/speedy-net/blob/uri_merge_with_master_2018-12-26_a/speedy/core/friends/tests/test_views.py. But if `MAX_NUMBER_OF_FRIENDS_ALLOWED` would be defined in the class `UserSettings`, how do I override it?

Thanks,
אורי (Uri)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABD5YeFfW-JbXawLmxX2aUjD41qS90CF7PbzUMwn0uqSWVwt7w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment