Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1656] Added CKEditor widget to category description field #789

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/open_inwoner/pdc/admin/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory

from open_inwoner.ckeditor5.widgets import CKEditorWidget
from open_inwoner.utils.logentry import system_action

from ..models import Category, CategoryProduct
Expand All @@ -31,6 +32,7 @@ class CategoryAdminForm(movenodeform_factory(Category)):
class Meta:
model = Category
fields = "__all__"
widgets = {"description": CKEditorWidget}

def clean(self, *args, **kwargs):
cleaned_data = super().clean(*args, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/pdc/models/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from treebeard.exceptions import InvalidMoveToDescendant
from treebeard.mp_tree import MP_MoveHandler, MP_Node

from ...utils.ckeditor import get_rendered_content
from ..managers import CategoryPublishedQueryset


Expand Down
31 changes: 24 additions & 7 deletions src/open_inwoner/pdc/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class TestCategoryListView(TestCase):
class CategoryListViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
cls.user.set_password("12345")
cls.user.email = "[email protected]"
cls.user.save()

def test_access_restricted(self):
def test_category_list_view_access_restricted(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = True
config.save()
Expand All @@ -36,7 +36,7 @@ def test_access_restricted(self):

self.assertEqual(response.status_code, 200)

def test_access_not_restricted(self):
def test_category_list_view_access_not_restricted(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = False
config.save()
Expand All @@ -50,17 +50,20 @@ def test_access_not_restricted(self):


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class TestCategoryDetailView(TestCase):
class CategoryDetailViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
cls.user.set_password("12345")
cls.user.email = "[email protected]"
cls.user.save()

cls.category = CategoryFactory.create(name="test cat")
cls.category = CategoryFactory.create(
name="test cat",
description="A <em>descriptive</em> description",
)

def test_access_restricted(self):
def test_category_detail_view_access_restricted(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = True
config.save()
Expand All @@ -80,7 +83,7 @@ def test_access_restricted(self):

self.assertEqual(response.status_code, 200)

def test_access_not_restricted(self):
def test_category_detail_view_access_not_restricted(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = False
config.save()
Expand All @@ -91,3 +94,17 @@ def test_access_not_restricted(self):
response = self.client.get(url)

self.assertEqual(response.status_code, 200)

def test_category_detail_description_rendered(self):
url = reverse("products:category_detail", kwargs={"slug": self.category.slug})

response = self.client.get(url)

self.assertIn(
'<p class="p">A <em>descriptive</em> description</p>',
response.rendered_content,
)
self.assertNotIn(
'[<p class="p">A <em>descriptive</em> description</p>, <em>descriptive</em>]',
response.rendered_content,
)
4 changes: 4 additions & 0 deletions src/open_inwoner/pdc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from open_inwoner.questionnaire.models import QuestionnaireStep
from open_inwoner.utils.views import LoginMaybeRequiredMixin

from ..utils.ckeditor import get_rendered_content
from ..utils.views import CommonPageMixin
from .choices import YesNo
from .forms import ProductFinderForm
Expand Down Expand Up @@ -173,6 +174,9 @@ def get_context_data(self, **kwargs):
context["questionnaire_roots"] = QuestionnaireStep.get_root_nodes().filter(
category=self.object
)
context["category_rendered_description"] = get_rendered_content(
self.object.description
)
return context

def get_breadcrumb_name(self):
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/templates/pages/category/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1 class="h1">
{% button icon="edit" text=_("Open in admin") hide_text=True href="admin:pdc_category_change" object_id=object.pk %}
{% endif %}
</h1>
<p class="p">{{ object.description|linebreaksbr }}</p>
{{ category_rendered_description|safe }}

{% if subcategories %}
{% card_container subcategories=subcategories parent_category=object %}
Expand Down
4 changes: 2 additions & 2 deletions src/open_inwoner/utils/ckeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
]


def get_rendered_content(content):
def get_rendered_content(content: str) -> str:
"""
Takes object's content as an input and returns the rendered one.
"""
Expand All @@ -37,7 +37,7 @@ def get_rendered_content(content):
if element.name == "a" and element.attrs.get("href", "").startswith("http"):
element.attrs["target"] = "_blank"

return soup
return str(soup)


def get_product_rendered_content(product):
Expand Down