Skip to content

Commit

Permalink
add reference link for snippet (#55)
Browse files Browse the repository at this point in the history
* add reference link for snippet

* fix code format issue.

* fix code format issue. change quote style and ectract variable.

* extract variable

* rewording
  • Loading branch information
FreemanPancake authored Apr 1, 2024
1 parent ff2963b commit 09f3a96
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased
* Python 3.7 support removed
* Django 4.2 support added
* Dropped Python 3.7 and below version support
* Add reference link for djangocms-snippet app

1.4.3 (2024-02-07)
==========
Expand Down
3 changes: 2 additions & 1 deletion djangocms_references/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cms.plugin_base import CMSPlugin

from djangocms_alias.models import AliasPlugin
from djangocms_snippet.models import SnippetPtr as SnippetPlugin

from .datastructures import ExtraColumn
from .helpers import (
Expand Down Expand Up @@ -153,7 +154,7 @@ class ReferencesCMSAppConfig(CMSAppConfig):
djangocms_versioning_enabled = getattr(
settings, "DJANGOCMS_REFERENCES_VERSIONING_ENABLED", True
)
reference_fields = [(AliasPlugin, 'alias')]
reference_fields = [(AliasPlugin, "alias"), (SnippetPlugin, "snippet_grouper")]
reference_list_extra_columns = [
(version_attr(lambda v: v.get_state_display()), _("Status")),
(version_attr(lambda v: v.created_by), _("Author")),
Expand Down
38 changes: 24 additions & 14 deletions djangocms_references/monkeypatch/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
from django.template.loader import render_to_string
from django.urls import reverse_lazy

from djangocms_alias import admin
from djangocms_alias import admin as AliasOriginalAdmin
from djangocms_snippet import admin as SnippetOriginalAdmin


def _get_references_link(self, obj, request):
alias_content_type = ContentType.objects.get(
app_label=obj.alias._meta.app_label,
model=obj.alias._meta.model_name,
)
def generate_get_references_link(content_grouper):
def _get_references_link(self, obj, request):
obj_grouper = getattr(obj, content_grouper)
_obj_grouper_meta = obj_grouper._meta
content_type = ContentType.objects.get(
app_label=_obj_grouper_meta.app_label,
model=_obj_grouper_meta.model_name,
)

url = reverse_lazy(
"djangocms_references:references-index",
kwargs={"content_type_id": alias_content_type.id, "object_id": obj.alias.id}
)
url = reverse_lazy(
"djangocms_references:references-index",
kwargs={"content_type_id": content_type.id, "object_id": obj_grouper.id}
)

return render_to_string("djangocms_references/references_icon.html", {"url": url})
return render_to_string("djangocms_references/references_icon.html", {"url": url})
return _get_references_link


def get_list_actions(func):
Expand All @@ -30,7 +35,12 @@ def inner(self, *args, **kwargs):
return inner


admin.AliasContentAdmin._get_references_link = _get_references_link
admin.AliasContentAdmin.get_list_actions = get_list_actions(
admin.AliasContentAdmin.get_list_actions
AliasOriginalAdmin.AliasContentAdmin._get_references_link = generate_get_references_link('alias')
AliasOriginalAdmin.AliasContentAdmin.get_list_actions = get_list_actions(
AliasOriginalAdmin.AliasContentAdmin.get_list_actions
)

SnippetOriginalAdmin.SnippetAdmin._get_references_link = generate_get_references_link('snippet_grouper')
SnippetOriginalAdmin.SnippetAdmin.get_list_actions = get_list_actions(
SnippetOriginalAdmin.SnippetAdmin.get_list_actions
)
3 changes: 2 additions & 1 deletion tests/requirements/requirements_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ flake8
isort
tox

https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms
https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms
https://github.com/django-cms/djangocms-snippet/tarball/support/django-cms-4.0.x#egg=djangocms-snippet
2 changes: 2 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"SECRET_KEY": "djangocmsreferencestestsuitekey",
"INSTALLED_APPS": [
"djangocms_alias.apps.AliasConfig",
"djangocms_snippet.apps.SnippetConfig",
"djangocms_references",
"djangocms_versioning",
"djangocms_references.test_utils.app_1",
Expand All @@ -16,6 +17,7 @@
"djangocms_references": None,
"djangocms_versioning": None,
"djangocms_alias": None,
"djangocms_snippet": None,
"djangocms_references.test_utils.polls": None,
},
"DEFAULT_AUTO_FIELD": "django.db.models.AutoField",
Expand Down
31 changes: 31 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from djangocms_alias.admin import AliasContentAdmin
from djangocms_alias.models import Alias, AliasContent, Category
from djangocms_snippet.admin import SnippetAdmin as SnippetContentAdmin
from djangocms_snippet.models import Snippet as SnippetContent, SnippetGrouper
from djangocms_versioning.models import Version


Expand Down Expand Up @@ -37,3 +39,32 @@ def test_list_display(self):

self.assertIn(str(references_url), list_display_icons)
self.assertIn("Show References", list_display_icons)


class SnippetAdminReferencesMonkeyPatchTestCase(CMSTestCase):
def test_list_display(self):
"""
The monkeypatch extends the snippet admin, adding the show references link
"""
request = self.get_request("/")
request.user = self.get_superuser()
snippet = SnippetGrouper.objects.create()
snippet_content = SnippetContent.objects.create(
name="Snippet Reference Monkey Patch Content",
snippet_grouper=snippet,
slug="snippet_reference_link_monkeypatch_slug",
)

Version.objects.create(content=snippet_content, created_by=request.user)
content_type = ContentType.objects.get(app_label=snippet._meta.app_label, model=snippet._meta.model_name)
snippet_admin = SnippetContentAdmin(SnippetContent, admin.AdminSite())
func = snippet_admin.get_list_display(request)[-1]
references_url = reverse_lazy(
"djangocms_references:references-index",
kwargs={"content_type_id": content_type.id, "object_id": snippet.id}
)

list_display_icons = func(snippet_content)

self.assertIn(str(references_url), list_display_icons)
self.assertIn("Show References", list_display_icons)
3 changes: 2 additions & 1 deletion tests/test_cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cms.utils.setup import configure_cms_apps

from djangocms_alias.models import Alias
from djangocms_snippet.models import SnippetGrouper

from djangocms_references import cms_config
from djangocms_references.test_utils import factories
Expand Down Expand Up @@ -131,7 +132,7 @@ def test_config_with_multiple_apps(self):
reference_models = cms_extension.reference_models
reference_plugins = cms_extension.reference_plugins
expected_models = [Parent, Poll]
expected_plugins = [Alias, Poll]
expected_plugins = [Alias, Poll, SnippetGrouper]

self.assertCountEqual(reference_models.keys(), expected_models)
self.assertCountEqual(reference_plugins.keys(), expected_plugins)
Expand Down
50 changes: 50 additions & 0 deletions tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from djangocms_alias.models import Alias as AliasModel, AliasContent, Category
from djangocms_alias.utils import is_versioning_enabled
from djangocms_snippet.models import Snippet as SnippetContent, SnippetGrouper

from djangocms_references.test_utils.factories import PollContentFactory
from djangocms_references.test_utils.nested_references_app.models import (
Expand Down Expand Up @@ -72,6 +73,55 @@ def test_aliases_references_integration(self):
self.assertContains(response, page_content.versions.first().state)


class SnippetReferencesIntegrationTestCase(CMSTestCase):
def test_snippets_references_integration(self):
"""
When opening the references for a given snippet, the objects which reference it should be listed
"""
user = self.get_superuser()
snippet = SnippetGrouper.objects.create()
SnippetContent.objects.create(
name="Snippet Content",
snippet_grouper=snippet,
slug="snippet_reference_link_slug",
)
kwargs = {"created_by": user}
page = create_page(
title="References Integration Page for Snippet",
template="page.html",
language="en",
menu_title="",
in_navigation=True,
**kwargs
)
page_content = create_title("en", "Draft Page", page, created_by=user)
placeholder = page_content.get_placeholders().get(
slot="content"
)
add_plugin(
placeholder,
"SnippetPlugin",
language="en",
snippet_grouper=snippet,
)
snippet_content_type = ContentType.objects.get(
app_label=snippet._meta.app_label,
model=snippet._meta.model_name
)

references_endpoint = reverse(
"djangocms_references:references-index",
kwargs={"content_type_id": snippet_content_type.id, "object_id": snippet.id}
)
with self.login_user_context(user):
response = self.client.get(references_endpoint)

self.assertContains(response, snippet.name)
self.assertContains(response, "pagecontent")
self.assertContains(response, get_object_preview_url(page_content))
self.assertContains(response, page_content.versions.first().state)


class NestedAppIntegrationTestCase(CMSTestCase):
def test_nested_app_references(self):
"""
Expand Down

0 comments on commit 09f3a96

Please sign in to comment.