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

Test nodes with a html title #598

Merged
merged 2 commits into from
Dec 4, 2024
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
23 changes: 23 additions & 0 deletions example_project/django_mptt_example/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ def get_tree_mouse_delay(self):
return None


# Display the code for countries instead of the name.
class CountryCodeAdmin(CountryAdmin):
item_label_field_name = 'code_or_name'


# Display a title with html
class CountryWithHtmlAdmin(CountryAdmin):
autoescape = False
item_label_field_name = 'html_code_and_name'


class CountryCode(Country):
class Meta:
proxy = True


class CountryCodeAndName(Country):
class Meta:
proxy = True


admin.site.register(Country, CountryAdmin)
admin.site.register(CountryCode, CountryCodeAdmin)
admin.site.register(CountryCodeAndName, CountryWithHtmlAdmin)
15 changes: 15 additions & 0 deletions example_project/django_mptt_example/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils.html import format_html

from mptt.models import TreeForeignKey, MPTTModel

Expand All @@ -16,3 +17,17 @@ class Meta:

def __str__(self):
return self.name or self.code or ""

# Return the code if there is one, otherwise the name.
@property
def code_or_name(self):
return self.code or self.name

# Return the code and the name. Contains html.
# * Return only the name for continents.
@property
def html_code_and_name(self):
if self.code:
return format_html(f"<strong>{self.code}</strong> {self.name}")
else:
return format_html(f"<strong>{self.name}</strong>")
12 changes: 12 additions & 0 deletions example_project/django_mptt_example/tests/playwright_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ def visit_countries_page(self, expected_text="Select country to change"):
page.wait_for_selector(f"text={expected_text}")
page.wait_for_selector("css=#tree >> text=Oceania")

def visit_country_codes_page(self, expected_text="Select country code to change"):
page = self.page
page.goto(self.live_server_url + "/django_mptt_example/countrycode/")
page.wait_for_selector(f"text={expected_text}")
page.wait_for_selector("css=#tree >> text=Oceania")

def visit_country_codes_and_names_page(self, expected_text="Select country code and name to change"):
page = self.page
page.goto(self.live_server_url + "/django_mptt_example/countrycodeandname/")
page.wait_for_selector(f"text={expected_text}")
page.wait_for_selector("css=#tree >> text=Oceania")

def wait_for_node(self, title):
self.page.wait_for_selector(f'css=#tree .jqtree-title >> text="{title}"')

Expand Down
41 changes: 41 additions & 0 deletions example_project/django_mptt_example/tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,47 @@ def test_load_error(self):
page.wait_for_text("Error while loading the data from the server")


# Test the CountryCodeAdmin
# * Test the item_label_field_name feature
@override_settings(DJANGO_TESTING=True)
class PlaywrightCountryCodeAdminTestCase(BasePlaywrightTestCase):
fixtures = ["countries.json"]

def setUp(self):
User.objects.create_superuser("admin", "admin@admin.com", "password")
super().setUp()
self.page.visit_country_codes_page()

def test_display_tree(self):
page = self.page

page.find_title_element("Oceania")
page.open_node("Oceania")

page.find_title_element("AU")


# Test the CountryCodeAdmin
# * Test the item_label_field_name feature with html
@override_settings(DJANGO_TESTING=True)
class PlaywrightCountryCodeAndNameAdminTestCase(BasePlaywrightTestCase):
fixtures = ["countries.json"]

def setUp(self):
User.objects.create_superuser("admin", "admin@admin.com", "password")
super().setUp()
self.page.visit_country_codes_and_names_page()

def test_display_tree(self):
page = self.page

page.find_title_element("Oceania")
page.open_node("Oceania")

element = self.page.page.query_selector('css=strong >> text="AU"')
assert element


@override_settings(DJANGO_TESTING=True)
class PlaywrightReadonlyTestCase(BasePlaywrightTestCase):
fixtures = ["countries.json"]
Expand Down
Loading