Skip to content

Commit

Permalink
#126 use optimized sql by default for CAST_REPOSITORY + docs for setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ephes committed Apr 26, 2024
1 parent 7c0735b commit c0062ab
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cast/appsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
CAST_GALLERY_IMAGE_SLOT_DIMENSIONS: list[tuple[int, int]] = getattr(
settings, "CAST_GALLERY_IMAGE_SLOT_DIMENSIONS", [(1110, 740), (120, 80)]
)
CAST_BLOG_INDEX_REPOSITORY: str = getattr(settings, "CAST_BLOG_INDEX_REPOSITORY", "normal")
CAST_REPOSITORY: str = getattr(settings, "CAST_REPOSITORY", "default")

SettingValue = Union[str, bool, int]

Expand Down
6 changes: 4 additions & 2 deletions cast/models/index_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,12 @@ def get_context(self, request: HtmxHttpRequest, *args, **kwargs) -> ContextDict:
def get_repository(self, request: HtmxHttpRequest, kwargs: dict[str, Any]) -> BlogIndexRepository:
if "repository" in kwargs:
return kwargs["repository"]
if appsettings.CAST_BLOG_INDEX_REPOSITORY == "raw":
if appsettings.CAST_REPOSITORY == "default":
data = BlogIndexRepository.data_for_blog_index_cachable(request=request, blog=self)
return BlogIndexRepository.create_from_cachable_data(data=data)
return BlogIndexRepository.create_from_django_models(request=request, blog=self)
else:
# fetch data using Django models as a fall back
return BlogIndexRepository.create_from_django_models(request=request, blog=self)

def serve(self, request: HtmxHttpRequest, *args, **kwargs) -> TemplateResponse:
kwargs["repository"] = repository = self.get_repository(request, kwargs)
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/0.2.30.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0.2.30 (2024-04-25)
0.2.30 (2024-04-26)
-------------------

Render feed, blog index and post detail without hitting the database
Expand Down
14 changes: 14 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ use tags, you can do it like this:
CAST_FILTERSET_FACETS = [
"search", "date", "date_facets", "category_facets"
]
**********
Repository
**********

How to fetch data from the database. By default, the repository fetches
all the data using optimized sql. If you want to fetch data using the
Django ORM, you can set the ``CAST_REPOSITORY`` variable in your settings
to ``"django"``.

.. code-block:: python
CAST_REPOSITORY = "django"
12 changes: 6 additions & 6 deletions tests/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def test_get_all_file_paths(self, file_instance):


@pytest.fixture()
def use_raw_blog_index_repo():
previous = appsettings.CAST_BLOG_INDEX_REPOSITORY
appsettings.CAST_BLOG_INDEX_REPOSITORY = "raw"
yield appsettings.CAST_BLOG_INDEX_REPOSITORY
appsettings.CAST_BLOG_INDEX_REPOSITORY = previous
def use_django_blog_index_repo():
previous = appsettings.CAST_REPOSITORY
appsettings.CAST_REPOSITORY = "django"
yield appsettings.CAST_REPOSITORY
appsettings.CAST_REPOSITORY = previous


class TestBlogModel:
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_template_base_dir_is_none(self, blog, simple_request):
template = blog.get_template(simple_request, template_base_dir="foobar")
assert template == "cast/foobar/blog_list_of_posts.html"

def test_get_repository(self, blog, simple_request, use_raw_blog_index_repo):
def test_get_django_repository(self, blog, simple_request, use_django_blog_index_repo):
repository = blog.get_repository(simple_request, {})
assert isinstance(repository, BlogIndexRepository)

Expand Down
20 changes: 19 additions & 1 deletion tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django.urls import reverse
from wagtail.images.models import Image, Rendition

from cast.devdata import create_python_body, generate_blog_with_media
from cast.devdata import create_post, create_python_body, generate_blog_with_media
from cast.feeds import LatestEntriesFeed
from cast.filters import PostFilterset
from cast.models import Audio, Blog, Post, Video
Expand Down Expand Up @@ -611,3 +611,21 @@ def test_create_from_cachable_data_use_audio_player_false():
}
repository = BlogIndexRepository.create_from_cachable_data(data=data)
assert repository.use_audio_player is False


@pytest.mark.django_db
def test_blog_index_repository_via_django_models_site_is_none(rf):
"""Make sure the repository can be created from django models when site is None."""
blog = Blog(id=1, title="Some blog", template_base_dir="plain")
request = rf.get("/foobar/")
repository = BlogIndexRepository.create_from_django_models(request=request, blog=blog)
assert repository.root_nav_links == []


@pytest.mark.django_db
def test_blog_index_repository_via_django_models_no_audio_player(rf, blog):
"""Make sure has_audio is False if there's no post with audio."""
request = rf.get("/foobar/")
create_post(blog=blog)
repository = BlogIndexRepository.create_from_django_models(request=request, blog=blog)
assert repository.use_audio_player is False

0 comments on commit c0062ab

Please sign in to comment.