Skip to content

Commit f079fd9

Browse files
committed
Add django-slug-preview support
This improves the appearance of slugs in the admin
1 parent 502499c commit f079fd9

File tree

7 files changed

+31
-2
lines changed

7 files changed

+31
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Changes in git
1010
* Added multiple fallback language support for django-parler_ 1.5.
1111
* Added ``make_language_redirects`` management command for redirecting an unmaintained language to another.
1212
* Added ``is_child_active`` variable in ``PageNavigationNode`` for menu templates.
13+
* Added django-slug-preview_ for nicer slug appearance in the admin.
1314
* Improve error messages when URLs can't be created.
1415
* Improve performance of ``PageSitemap`` for sites with a lot of pages.
1516
* Temporary fix: Block moving pages to untranslated sub nodes, until a design decision can be made how to handle this.
@@ -256,4 +257,5 @@ First public release
256257
.. _django-mptt: https://github.com/django-mptt/django-mptt
257258
.. _django-parler: https://github.com/edoburu/django-parler
258259
.. _django-polymorphic-tree: https://github.com/edoburu/django-polymorphic-tree
260+
.. _django-slug-preview: https://github.com/edoburu/django-slug-preview
259261
.. _django-tag-parser: https://github.com/edoburu/django-tag-parser

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ To have a standard setup with django-fluent-contents_ integrated, use::
7171
'parler',
7272
'polymorphic',
7373
'polymorphic_tree',
74+
'slug_preview',
7475

7576
# Optional widget pages via django-fluent-contents
7677
'fluent_pages.pagetypes.fluentpage',

example/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ django-mptt >= 0.6.0
88
django-parler >= 1.5
99
django-polymorphic-tree >= 1.1.1
1010
django-polymorphic >= 0.5.5
11+
django-slug-preview >= 0.9
1112
django-tag-parser >= 2.1
1213

1314
# For the `fluent_pages.plugins.fluentpage` plugin:

example/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'polymorphic',
108108
'polymorphic_tree',
109109
'parler',
110+
'slug_preview',
110111

111112
# Content for fluentpage, with plugins that have no extra configuration requirements
112113
'fluent_contents',

fluent_pages/adminui/urlnodechildadmin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
from fluent_pages import appsettings
88
from parler.admin import TranslatableAdmin
99
from parler.forms import TranslatableModelForm, TranslatedField
10+
from slug_preview.forms import SlugPreviewFormMixin
1011
from fluent_pages.models import UrlNode, UrlNode_Translation
1112
from fluent_pages.forms.fields import RelativeRootPathField
1213
from fluent_utils.dry.admin import MultiSiteAdminMixin
1314
import mptt
1415

1516

16-
class UrlNodeAdminForm(MPTTAdminForm, TranslatableModelForm):
17+
class UrlNodeAdminForm(MPTTAdminForm, SlugPreviewFormMixin, TranslatableModelForm):
1718
"""
1819
The admin form for the main fields (the ``UrlNode`` object).
1920
"""

fluent_pages/models/db.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from fluent_pages import appsettings
2727
from fluent_utils.django_compat import transaction_atomic, AUTH_USER_MODEL
2828
from parler.utils.context import switch_language
29+
from slug_preview.models import SlugPreviewField
2930
from future.utils import with_metaclass, itervalues, iteritems
3031

3132

@@ -151,6 +152,27 @@ def get_absolute_url(self):
151152
# is included at a sublevel, it needs to be prepended.
152153
return self.default_url
153154

155+
def get_absolute_url_format(self):
156+
if self.is_file:
157+
url_format = '/{slug}'
158+
else:
159+
url_format = '/{slug}/'
160+
161+
# Extra for django-slug-preview
162+
if self.parent_id:
163+
# TODO: optimize this call. In some cases this would also work..
164+
# that is, unless get_absolute_url() is redefined or ABSOLUTE_URL_OVERRIDES was used.
165+
#parent_url = self.get_translation(self.get_current_language()).get_parent_cached_url(self)
166+
167+
# Need to fetch the whole parent to make sure the URL matches the actual URL being used.
168+
parent = self.parent
169+
with switch_language(parent, self.get_current_language()):
170+
parent_url = parent.get_absolute_url()
171+
172+
return parent_url.rstrip('/') + url_format
173+
else:
174+
return url_format
175+
154176
@property
155177
def default_url(self):
156178
"""
@@ -530,7 +552,7 @@ class UrlNode_Translation(TranslatedFieldsModel):
530552
"""
531553
# Translated fields
532554
title = models.CharField(_("title"), max_length=255)
533-
slug = models.SlugField(_("slug"), max_length=100, help_text=_("The slug is used in the URL of the page"))
555+
slug = SlugPreviewField(_("slug"), max_length=100, help_text=_("The slug is used in the URL of the page"))
534556
override_url = models.CharField(_('Override URL'), editable=True, max_length=255, blank=True, help_text=_('Override the target URL. Be sure to include slashes at the beginning and at the end if it is a local URL. This affects both the navigation and subpages\' URLs.'))
535557
_cached_url = models.CharField(max_length=255, db_index=True, null=True, blank=True, editable=False)
536558

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def find_version(*parts):
4444
'django-parler>=1.6', # Needed for Django 1.9 compatibility
4545
'django-polymorphic>=0.7', # Needed for Django 1.8 compatibility
4646
'django-polymorphic-tree>=1.2', # Needed for Django 1.9 compatibility
47+
'django-slug-preview>=0.9',
4748
'django-tag-parser>=2.1',
4849
'future>=0.12.2',
4950
'six>=1.5.2',

0 commit comments

Comments
 (0)