Skip to content

Commit

Permalink
Fixed #35867, Refs django#2411 -- Allowed links in admindocs view det…
Browse files Browse the repository at this point in the history
…ails summary.
  • Loading branch information
sai-ganesh-03 authored and sarahboyce committed Nov 4, 2024
1 parent 4fcbdb1 commit 9683972
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<h1>{{ name }}</h1>

<h2 class="subhead">{{ summary|striptags }}</h2>
<h2 class="subhead">{{ summary }}</h2>

{{ body }}

Expand Down
4 changes: 4 additions & 0 deletions django/contrib/admindocs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,7 @@ def remove_non_capturing_groups(pattern):
final_pattern += pattern[prev_end:start]
prev_end = end
return final_pattern + pattern[prev_end:]


def strip_p_tags(value):
return mark_safe(value.replace("<p>", "").replace("</p>", ""))
6 changes: 3 additions & 3 deletions django/contrib/admindocs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from django.utils.translation import gettext as _
from django.views.generic import TemplateView

from .utils import get_view_name
from .utils import get_view_name, strip_p_tags

# Exclude methods starting with these strings from documentation
MODEL_METHODS_EXCLUDE = ("_", "add_", "delete", "save", "set_")
Expand Down Expand Up @@ -195,7 +195,7 @@ def get_context_data(self, **kwargs):
**{
**kwargs,
"name": view,
"summary": title,
"summary": strip_p_tags(title),
"body": body,
"meta": metadata,
}
Expand Down Expand Up @@ -384,7 +384,7 @@ def get_context_data(self, **kwargs):
**{
**kwargs,
"name": opts.label,
"summary": title,
"summary": strip_p_tags(title),
"description": body,
"fields": fields,
"methods": methods,
Expand Down
16 changes: 14 additions & 2 deletions tests/admin_docs/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def test_view_detail(self):
# View docstring
self.assertContains(response, "Base view for admindocs views.")

def testview_docstring_links(self):
summary = (
'<h2 class="subhead">This is a view for '
'<a class="reference external" href="/admindocs/models/myapp.company/">'
"myapp.Company</a></h2>"
)
url = reverse(
"django-admindocs-views-detail", args=["admin_docs.views.CompanyView"]
)
response = self.client.get(url)
self.assertContains(response, summary, html=True)

@override_settings(ROOT_URLCONF="admin_docs.namespace_urls")
def test_namespaced_view_detail(self):
url = reverse(
Expand Down Expand Up @@ -408,9 +420,9 @@ def test_model_with_no_backward_relations_render_only_relevant_fields(self):

def test_model_docstring_renders_correctly(self):
summary = (
'<h2 class="subhead"><p>Stores information about a person, related to '
'<h2 class="subhead">Stores information about a person, related to '
'<a class="reference external" href="/admindocs/models/myapp.company/">'
"myapp.Company</a>.</p></h2>"
"myapp.Company</a>.</h2>"
)
subheading = "<p><strong>Notes</strong></p>"
body = (
Expand Down
1 change: 1 addition & 0 deletions tests/admin_docs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
path("admin/", admin.site.urls),
path("admindocs/", include("django.contrib.admindocs.urls")),
path("", include(ns_patterns, namespace="test")),
path("company/", views.CompanyView.as_view()),
path("xview/func/", views.xview_dec(views.xview)),
path("xview/class/", views.xview_dec(views.XViewClass.as_view())),
path("xview/callable_object/", views.xview_dec(views.XViewCallableObject())),
Expand Down
9 changes: 9 additions & 0 deletions tests/admin_docs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ def get(self, request):
class XViewCallableObject(View):
def __call__(self, request):
return HttpResponse()


class CompanyView(View):
"""
This is a view for :model:`myapp.Company`
"""

def get(self, request):
return HttpResponse()

0 comments on commit 9683972

Please sign in to comment.