Skip to content

Commit

Permalink
Merge pull request #23 from tumb1er/unite_defaults_implementation
Browse files Browse the repository at this point in the history
Unite defaults ans default_settings after #22
  • Loading branch information
tumb1er authored Mar 19, 2021
2 parents bb7384d + ff9d646 commit b007e48
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 57 deletions.
14 changes: 0 additions & 14 deletions sitemap_generate/default_settings.py

This file was deleted.

33 changes: 32 additions & 1 deletion sitemap_generate/defaults.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
from os import getenv as e
from os import getenv

from django.conf import settings


def e(name, default):
"""
Returns settings value from django settings or environment.
:param name: setting name
:param default: default value if setting is not specified in django
settings module and environment.
"""
try:
return getattr(settings, name)
except AttributeError:
return getenv(name, default)


# Hostname used in sitemap links
SITEMAP_HOST = e('SITEMAP_HOST', 'localhost')
# Port used in sitemap links
SITEMAP_PORT = e('SITEMAP_PORT', '443')
# Protocol used in sitemap links
SITEMAP_PROTO = e('SITEMAP_PROTO', 'https')

# Default directory in media storage where sitemaps are stored
SITEMAP_MEDIA_PATH = e('SITEMAP_MEDIA_PATH', 'sitemaps')

# Default media storage for sitemaps
SITEMAP_STORAGE = e('SITEMAP_STORAGE',
'django.core.files.storage.default_storage')

# Default name of sitemap index view
SITEMAP_INDEX_NAME = e('SITEMAP_INDEX_NAME', 'sitemap-index')

# Default name of sitemaps view
SITEMAPS_VIEW_NAME = e('SITEMAPS_VIEW_NAME',
'django.contrib.sitemaps.views.sitemap')
50 changes: 8 additions & 42 deletions sitemap_generate/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.utils.module_loading import import_string

from sitemap_generate import defaults
from sitemap_generate import default_settings


class SitemapError(Exception):
Expand Down Expand Up @@ -97,47 +96,14 @@ def __init__(self,
"""
cls = self.__class__
self.logger = getLogger(f'{cls.__module__}.{cls.__name__}')
self.sitemap_root = (
media_path
if media_path is not None
else getattr(
settings,
'SITEMAP_MEDIA_PATH',
default_settings.SITEMAP_MEDIA_PATH,
)
)
self.storage = (
storage
if storage is not None
else getattr(
settings,
'SITEMAP_STORAGE',
default_settings.SITEMAP_STORAGE,
)
)
self.index_url_name = (
index_url_name
if index_url_name is not None
else getattr(
settings,
'SITEMAP_INDEX_NAME',
default_settings.SITEMAP_INDEX_NAME
)
)
self.sitemaps_view_name = (
sitemaps_view_name
if sitemaps_view_name is not None
else getattr(
settings,
'SITEMAPS_VIEW_NAME',
default_settings.SITEMAPS_VIEW_NAME,
)
)
self.sitemaps = (
sitemaps
if sitemaps is not None
else import_string(settings.SITEMAP_MAPPING)
)
self.sitemap_root = media_path or defaults.SITEMAP_MEDIA_PATH
storage = storage or defaults.SITEMAP_STORAGE
self.storage = import_string(storage)
self.index_url_name = index_url_name or defaults.SITEMAP_INDEX_NAME
sitemaps_view_name = sitemaps_view_name or defaults.SITEMAPS_VIEW_NAME
self.sitemaps_view_name = sitemaps_view_name
sitemaps = sitemaps or getattr(settings, 'SITEMAP_MAPPING')
self.sitemaps = import_string(sitemaps)
self.recorder = ResponseRecorder(
basehttp.get_internal_wsgi_application())

Expand Down

0 comments on commit b007e48

Please sign in to comment.