Skip to content

Commit

Permalink
Merge pull request #33 from tumb1er/fix_import_storage_by_string
Browse files Browse the repository at this point in the history
Fix initializing storage and sitemaps from symbol
  • Loading branch information
tumb1er authored Aug 4, 2021
2 parents 24be706 + c5545eb commit 566e824
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Django==3.2.6
dj-inmemorystorage==2.1.0
django-testing-utils==0.3.1
14 changes: 10 additions & 4 deletions sitemap_generate/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ def __init__(self,
cls = self.__class__
self.logger = getLogger(f'{cls.__module__}.{cls.__name__}')
self.sitemap_root = media_path or defaults.SITEMAP_MEDIA_PATH
storage = storage or defaults.SITEMAP_STORAGE
self.storage = import_string(storage)

if storage is None:
storage = import_string(defaults.SITEMAP_STORAGE)

self.storage = 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)

if sitemaps is None:
sitemaps = import_string(getattr(settings, 'SITEMAP_MAPPING'))
self.sitemaps = sitemaps

self.recorder = ResponseRecorder(
basehttp.get_internal_wsgi_application())

Expand Down
2 changes: 2 additions & 0 deletions testproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
}
}

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
Expand Down
38 changes: 36 additions & 2 deletions testproject/testapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@
import django
from django.core.files.storage import default_storage, Storage
from django.core.management import call_command
from django.test import TestCase
from django.test import TestCase, override_settings
from django_testing_utils.utils import override_defaults

from sitemap_generate import defaults
from testproject.testapp import models
from sitemap_generate.generator import SitemapGenerator
from testproject.testapp import models, sitemaps


# noinspection PyAbstractClass
class TestStorage(Storage):
pass


test_storage = TestStorage()


sitemap_mapping = {'videos': sitemaps.VideoSitemap}


class GenerateSitemapCommandTestCase(TestCase):
Expand Down Expand Up @@ -78,3 +91,24 @@ def test_generate_single_sitemap(self):
self.assertTrue(self.storage.exists('sitemaps/sitemap.xml'))
self.assertTrue(self.storage.exists('sitemaps/sitemap-video.xml'))
self.assertFalse(self.storage.exists('sitemaps/sitemap-articles.xml'))


class SitemapGeneratorTestCase(TestCase):

@override_defaults('sitemap_generate', SITEMAP_STORAGE='testproject.testapp.tests.test_storage')
def test_init_storage_from_settings(self):
sg = SitemapGenerator()
self.assertIsInstance(sg.storage, TestStorage)

def test_init_storage_from_args(self):
sg = SitemapGenerator(storage=test_storage)
self.assertIs(sg.storage, test_storage)

@override_settings(SITEMAP_MAPPING='testproject.testapp.tests.sitemap_mapping')
def test_init_sitemaps_from_settings(self):
sg = SitemapGenerator()
self.assertIs(sg.sitemaps['videos'], sitemaps.VideoSitemap)

def test_init_sitemaps_from_args(self):
sg = SitemapGenerator(sitemaps=sitemap_mapping)
self.assertIs(sg.sitemaps['videos'], sitemaps.VideoSitemap)

0 comments on commit 566e824

Please sign in to comment.