Skip to content
This repository
Browse code

Picture plugin: validate that a user enters a Link or a Page but not …

…both.
  • Loading branch information...
commit a50456cde371e6e22f19c46aab9d8724f7a1b5a7 1 parent bf3cc87
Richard Barran richardbarran authored

Showing 2 changed files with 31 additions and 1 deletion. Show diff stats Hide diff stats

  1. +6 0 cms/plugins/picture/models.py
  2. +25 1 cms/tests/plugins.py
6 cms/plugins/picture/models.py
... ... @@ -1,6 +1,7 @@
1 1
2 2 from django.db import models
3 3 from django.utils.translation import ugettext_lazy as _
  4 +from django.core.exceptions import ValidationError
4 5 from cms.models import CMSPlugin, Page
5 6 from os.path import basename
6 7
@@ -43,3 +44,8 @@ def __unicode__(self):
43 44 except:
44 45 pass
45 46 return "<empty>"
  47 +
  48 + def clean(self):
  49 + if self.url and self.page_link:
  50 + raise ValidationError(
  51 + _("You can enter a Link or a Page, but not both."))
26 cms/tests/plugins.py
@@ -11,6 +11,7 @@
11 11 from cms.plugins.inherit.models import InheritPagePlaceholder
12 12 from cms.plugins.link.forms import LinkForm
13 13 from cms.plugins.link.models import Link
  14 +from cms.plugins.picture.models import Picture
14 15 from cms.plugins.text.models import Text
15 16 from cms.plugins.text.utils import (plugin_tags_to_id_list,
16 17 plugin_tags_to_admin_html)
@@ -25,7 +26,7 @@
25 26 from cms.utils.copy_plugins import copy_plugins_to
26 27 from django.conf import settings
27 28 from django.contrib.auth.models import User
28   -from django.core.exceptions import ImproperlyConfigured
  29 +from django.core.exceptions import ImproperlyConfigured, ValidationError
29 30 from django.core.files.uploadedfile import SimpleUploadedFile
30 31 from django.core.management import call_command
31 32 from django.forms.widgets import Media
@@ -975,3 +976,26 @@ class LeftMixin: pass
975 976 class RightMixin: pass
976 977 plugin_class = PluginModelBase('TestPlugin2', (LeftMixin, CMSPlugin, RightMixin), {'__module__': 'cms.tests.plugins'})
977 978 self.assertEqual(plugin_class._meta.db_table, 'cmsplugin_testplugin2')
  979 +
  980 +class PicturePluginTests(PluginsTestBaseCase):
  981 +
  982 + def test_link_or_page(self):
  983 + """Test a validator: you can enter a url or a page_link, but not both."""
  984 +
  985 + page_data = self.get_new_page_data()
  986 + response = self.client.post(URL_CMS_PAGE_ADD, page_data)
  987 + page = Page.objects.all()[0]
  988 +
  989 + picture = Picture(url="test")
  990 + # Note: don't call full_clean as it will check ALL fields - including
  991 + # the image, which we haven't defined. Call clean() instead which
  992 + # just validates the url and page_link fields.
  993 + picture.clean()
  994 +
  995 + picture.page_link = page
  996 + picture.url = None
  997 + picture.clean()
  998 +
  999 + picture.url = "test"
  1000 + self.assertRaises(ValidationError, picture.clean)
  1001 +

0 comments on commit a50456c

Please sign in to comment.
Something went wrong with that request. Please try again.