Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!(settings): Simplify settings structure for pytest #140

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ master (unreleased)
_This is where upcoming changes will be._

- Remove django-south migrations folder (#138)
- Refactor settings for better (forthcoming) pytest compatibility (#140)

5.0 (2022-01-08)
----------------
Expand Down
53 changes: 37 additions & 16 deletions src/robots/settings.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import sys
import typing as t

from django.conf import settings
from django.test.signals import setting_changed

class Settings:
defaults = {
#: A list of one or more sitemaps to inform robots about:
"SITEMAP_URLS": ("ROBOTS_SITEMAP_URLS", []),
"USE_SITEMAP": ("ROBOTS_USE_SITEMAP", True),
"USE_HOST": ("ROBOTS_USE_HOST", True),
"CACHE_TIMEOUT": ("ROBOTS_CACHE_TIMEOUT", None),
"SITE_BY_REQUEST": ("ROBOTS_SITE_BY_REQUEST", False),
"USE_SCHEME_IN_HOST": ("ROBOTS_USE_SCHEME_IN_HOST", False),
"SITEMAP_VIEW_NAME": ("ROBOTS_SITEMAP_VIEW_NAME", False),
}
DEFAULT_ROBOTS_SETTINGS: t.Dict[str, t.Any] = {
"SITEMAP_URLS": [],
"USE_SITEMAP": True,
"USE_HOST": True,
"CACHE_TIMEOUT": None,
"SITE_BY_REQUEST": False,
"USE_SCHEME_IN_HOST": False,
"SITEMAP_VIEW_NAME": False,
}

def __getattr__(self, attribute):
from django.conf import settings

if attribute in self.defaults:
return getattr(settings, *self.defaults[attribute])
def getdefault(setting: str) -> t.Any:
return getattr(settings, f"ROBOTS_{setting}", DEFAULT_ROBOTS_SETTINGS[setting])


sys.modules[__name__] = Settings()
Copy link
Member Author

@tony tony Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.modules[__name__] = Settings()

This is what throws off pytest collection in #137 (as of pytest 7.1.3)

SITEMAP_URLS: t.List[str] = getdefault("SITEMAP_URLS")
USE_SITEMAP: bool = getdefault("USE_SITEMAP")
USE_HOST: bool = getdefault("USE_HOST")
CACHE_TIMEOUT: t.Optional[int] = getdefault("CACHE_TIMEOUT")
SITE_BY_REQUEST: bool = getdefault("SITE_BY_REQUEST")
USE_SCHEME_IN_HOST: bool = getdefault("USE_SCHEME_IN_HOST")
SITEMAP_VIEW_NAME: bool = getdefault("SITEMAP_VIEW_NAME")


def reload_robots_settings(
setting: str, value: t.Any, enter: bool, **kwargs: object
) -> None:
if not setting.startswith("ROBOTS_"): # pragma: no cover
return

setting = setting.replace("ROBOTS_", "")
if enter: # setting applied
setattr(sys.modules[__name__], setting, value)
else:
setattr(sys.modules[__name__], setting, getdefault(setting))


setting_changed.connect(reload_robots_settings)