Skip to content

Commit

Permalink
Optional link requirements (django-cms#170)
Browse files Browse the repository at this point in the history
* allow no link, see: django-cms#140

* additional fixes for django-cms#141

* fix coding style

* added tests

* fix coding style

* reorder

* remove additional line
  • Loading branch information
FinalAngel authored Jun 19, 2019
1 parent 06df026 commit 932a406
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Changelog
=========


2.4.1 (unreleased)
==================

* Allow link requirement to be changed when another
CMS plugin inherits from AbstractLink


2.4.0 (2019-04-16)
==================

Expand Down
13 changes: 10 additions & 3 deletions djangocms_link/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class AbstractLink(CMSPlugin):
# used by django CMS search
search_fields = ('name', )

# allows link requirement to be changed when another
# CMS plugin inherits from AbstractLink
link_is_optional = False

url_validators = [
IntranetURLValidator(intranet_host_re=HOSTNAME),
]
Expand Down Expand Up @@ -132,9 +136,9 @@ def __str__(self):
return self.name or str(self.pk)

def get_short_description(self):
if self.name:
if self.name and self.get_link():
return '{} ({})'.format(self.name, self.get_link())
return self.get_link() or ugettext('<link is missing>')
return self.name or self.get_link() or ugettext('<link is missing>')

def get_link(self):
if self.internal_link:
Expand Down Expand Up @@ -213,7 +217,10 @@ def clean(self):
errors = {}.fromkeys(provided_link_fields.keys(), error_msg)
raise ValidationError(errors)

if len(provided_link_fields) == 0 and not self.anchor:
if (len(provided_link_fields) == 0
and not self.anchor
and not self.link_is_optional):

raise ValidationError(
_('Please provide a link.')
)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from unittest import skipIf, skipUnless

from django.utils.encoding import force_text
from django.core.exceptions import ValidationError

import cms
from cms.api import add_plugin, create_page

from djangocms_helper.base_test import BaseTestCase
from djangocms_link.models import AbstractLink


CMS_35 = LooseVersion(cms.__version__) >= LooseVersion('3.5')
Expand Down Expand Up @@ -109,3 +111,27 @@ def test_link_for_cms_34(self):
anchor='some-h1',
)
self.assertEqual(plugin.get_link(), '/en/#some-h1')

def test_optional_link(self):
AbstractLink.link_is_optional = True

plugin1 = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
)
self.assertIsNone(plugin1.clean())

AbstractLink.link_is_optional = False

self.assertEqual(AbstractLink.link_is_optional, False)

plugin2 = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
)

# should throw "Please provide a link." error
with self.assertRaises(ValidationError):
plugin2.clean()

0 comments on commit 932a406

Please sign in to comment.