Skip to content

Commit

Permalink
fix get_context_data if role has no specific context data method (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas committed Jul 29, 2024
1 parent 9108604 commit 00a6041
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/

## [Unreleased]

### Fixed

- Fixed a bug in `CRUDView.get_context_data` if the role had no specific context data method defined.

## [0.12.0]

### Added
Expand Down
7 changes: 6 additions & 1 deletion src/django_twc_toolbox/crud/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,17 @@ def get_active_filters(filterset: object):
@override
def get_context_data(self, **kwargs: object) -> dict[str, object]:
context = super().get_context_data(**kwargs)

context["list_view_url"] = Role.LIST.maybe_reverse(self)
if self.object is not None:
context["delete_view_url"] = Role.DELETE.maybe_reverse(self, self.object)
context["detail_view_url"] = Role.DETAIL.maybe_reverse(self, self.object)
context["update_view_url"] = Role.UPDATE.maybe_reverse(self, self.object)
context.update(self.get_role_context_data(context, **kwargs))

role_context = self.get_role_context_data(context, **kwargs)
if role_context:
context.update(role_context)

return context

def get_role_context_data(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_crud/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ def test_get_role_context_data_nonexistent(role):
assert result == {}


@pytest.mark.parametrize(
"role",
[
Role.DETAIL,
Role.LIST,
Role.CREATE,
Role.UPDATE,
Role.DELETE,
],
)
def test_get_context_data_role_method_nonexistent(role):
view = BookmarkView()
view.role = role

view.get_context_data()

assert True


@pytest.mark.parametrize(
"role",
[
Expand Down

0 comments on commit 00a6041

Please sign in to comment.