Skip to content

Commit

Permalink
Add django-slug-preview support
Browse files Browse the repository at this point in the history
This improves the appearance of slugs in the admin
  • Loading branch information
vdboor committed Dec 30, 2015
1 parent 502499c commit f079fd9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -10,6 +10,7 @@ Changes in git
* Added multiple fallback language support for django-parler_ 1.5.
* Added ``make_language_redirects`` management command for redirecting an unmaintained language to another.
* Added ``is_child_active`` variable in ``PageNavigationNode`` for menu templates.
* Added django-slug-preview_ for nicer slug appearance in the admin.
* Improve error messages when URLs can't be created.
* Improve performance of ``PageSitemap`` for sites with a lot of pages.
* Temporary fix: Block moving pages to untranslated sub nodes, until a design decision can be made how to handle this.
Expand Down Expand Up @@ -256,4 +257,5 @@ First public release
.. _django-mptt: https://github.com/django-mptt/django-mptt
.. _django-parler: https://github.com/edoburu/django-parler
.. _django-polymorphic-tree: https://github.com/edoburu/django-polymorphic-tree
.. _django-slug-preview: https://github.com/edoburu/django-slug-preview
.. _django-tag-parser: https://github.com/edoburu/django-tag-parser
1 change: 1 addition & 0 deletions README.rst
Expand Up @@ -71,6 +71,7 @@ To have a standard setup with django-fluent-contents_ integrated, use::
'parler',
'polymorphic',
'polymorphic_tree',
'slug_preview',

# Optional widget pages via django-fluent-contents
'fluent_pages.pagetypes.fluentpage',
Expand Down
1 change: 1 addition & 0 deletions example/requirements.txt
Expand Up @@ -8,6 +8,7 @@ django-mptt >= 0.6.0
django-parler >= 1.5
django-polymorphic-tree >= 1.1.1
django-polymorphic >= 0.5.5
django-slug-preview >= 0.9
django-tag-parser >= 2.1

# For the `fluent_pages.plugins.fluentpage` plugin:
Expand Down
1 change: 1 addition & 0 deletions example/settings.py
Expand Up @@ -107,6 +107,7 @@
'polymorphic',
'polymorphic_tree',
'parler',
'slug_preview',

# Content for fluentpage, with plugins that have no extra configuration requirements
'fluent_contents',
Expand Down
3 changes: 2 additions & 1 deletion fluent_pages/adminui/urlnodechildadmin.py
Expand Up @@ -7,13 +7,14 @@
from fluent_pages import appsettings
from parler.admin import TranslatableAdmin
from parler.forms import TranslatableModelForm, TranslatedField
from slug_preview.forms import SlugPreviewFormMixin
from fluent_pages.models import UrlNode, UrlNode_Translation
from fluent_pages.forms.fields import RelativeRootPathField
from fluent_utils.dry.admin import MultiSiteAdminMixin
import mptt


class UrlNodeAdminForm(MPTTAdminForm, TranslatableModelForm):
class UrlNodeAdminForm(MPTTAdminForm, SlugPreviewFormMixin, TranslatableModelForm):
"""
The admin form for the main fields (the ``UrlNode`` object).
"""
Expand Down
24 changes: 23 additions & 1 deletion fluent_pages/models/db.py
Expand Up @@ -26,6 +26,7 @@
from fluent_pages import appsettings
from fluent_utils.django_compat import transaction_atomic, AUTH_USER_MODEL
from parler.utils.context import switch_language
from slug_preview.models import SlugPreviewField
from future.utils import with_metaclass, itervalues, iteritems


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

def get_absolute_url_format(self):
if self.is_file:
url_format = '/{slug}'
else:
url_format = '/{slug}/'

# Extra for django-slug-preview
if self.parent_id:
# TODO: optimize this call. In some cases this would also work..
# that is, unless get_absolute_url() is redefined or ABSOLUTE_URL_OVERRIDES was used.
#parent_url = self.get_translation(self.get_current_language()).get_parent_cached_url(self)

# Need to fetch the whole parent to make sure the URL matches the actual URL being used.
parent = self.parent
with switch_language(parent, self.get_current_language()):
parent_url = parent.get_absolute_url()

return parent_url.rstrip('/') + url_format
else:
return url_format

@property
def default_url(self):
"""
Expand Down Expand Up @@ -530,7 +552,7 @@ class UrlNode_Translation(TranslatedFieldsModel):
"""
# Translated fields
title = models.CharField(_("title"), max_length=255)
slug = models.SlugField(_("slug"), max_length=100, help_text=_("The slug is used in the URL of the page"))
slug = SlugPreviewField(_("slug"), max_length=100, help_text=_("The slug is used in the URL of the page"))
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.'))
_cached_url = models.CharField(max_length=255, db_index=True, null=True, blank=True, editable=False)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -44,6 +44,7 @@ def find_version(*parts):
'django-parler>=1.6', # Needed for Django 1.9 compatibility
'django-polymorphic>=0.7', # Needed for Django 1.8 compatibility
'django-polymorphic-tree>=1.2', # Needed for Django 1.9 compatibility
'django-slug-preview>=0.9',
'django-tag-parser>=2.1',
'future>=0.12.2',
'six>=1.5.2',
Expand Down

0 comments on commit f079fd9

Please sign in to comment.